Add login/logout/register routes

This commit is contained in:
Daniel Mason 2021-03-24 23:07:46 +13:00
parent 671254223a
commit 28d8d3491a
Signed by: idanoo
GPG Key ID: 387387CDBC02F132
2 changed files with 26 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package goscrobble package goscrobble
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -19,32 +20,46 @@ type spaHandler struct {
// HandleRequests - Boot HTTP! // HandleRequests - Boot HTTP!
func HandleRequests() { func HandleRequests() {
// Create a new router // Create a new router
httpRouter := mux.NewRouter().StrictSlash(true) r := mux.NewRouter().StrictSlash(true)
v1 := r.PathPrefix("/api/v1").Subrouter()
// STATIC TOKEN AUTH // STATIC TOKEN AUTH
httpRouter.HandleFunc("/api/v1/ingress/jellyfin", serveEndpoint) // httpRouter.HandleFunc("/api/v1/ingress/jellyfin", serveEndpoint)
// JWT AUTH? // JWT SESSION AUTH?
httpRouter.HandleFunc("/api/v1/profile/{id}", serveEndpoint) // httpRouter.HandleFunc("/api/v1/profile/{id}", serveEndpoint)
// NO AUTH // NO AUTH
httpRouter.HandleFunc("/api/v1/login", serveEndpoint) v1.HandleFunc("/register", serveEndpoint).Methods("POST")
httpRouter.HandleFunc("/api/v1/logout", serveEndpoint) v1.HandleFunc("/login", serveEndpoint).Methods("POST")
httpRouter.HandleFunc("/api/v1/register", serveEndpoint) v1.HandleFunc("/logout", serveEndpoint).Methods("POST")
// This just prevents it serving frontend over /api
r.PathPrefix("/api")
// SERVE FRONTEND - NO AUTH // SERVE FRONTEND - NO AUTH
spa := spaHandler{staticPath: "web/build", indexPath: "index.html"} spa := spaHandler{staticPath: "web/build", indexPath: "index.html"}
httpRouter.PathPrefix("/").Handler(spa) r.PathPrefix("/").Handler(spa)
// Serve it up! // Serve it up!
log.Fatal(http.ListenAndServe(":42069", httpRouter)) log.Fatal(http.ListenAndServe(":42069", r))
} }
func serveEndpoint(w http.ResponseWriter, r *http.Request) { func serveEndpoint(w http.ResponseWriter, r *http.Request) {
// Lets trick 'em for now var jsonInput map[string]interface{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&jsonInput)
if err != nil {
// If we can't decode. Lets tell them nicely.
http.Error(w, "{\"error\":\"Invalid JSON\"}", http.StatusBadRequest)
return
}
// Lets trick 'em for now ;) ;)
fmt.Fprintf(w, "{}") fmt.Fprintf(w, "{}")
} }
// ServerHTTP - Frontend server
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Get the absolute path to prevent directory traversal // Get the absolute path to prevent directory traversal
path, err := filepath.Abs(r.URL.Path) path, err := filepath.Abs(r.URL.Path)

View File

@ -7,7 +7,7 @@ function App() {
<header className="App-header"> <header className="App-header">
<img src={logo} className="App-logo" alt="logo" /> <img src={logo} className="App-logo" alt="logo" />
<p> <p>
go-scrobble goscrobble.com
</p> </p>
<a <a
className="App-link" className="App-link"