Add login endpoint and generate a valid token

This commit is contained in:
Miguel de la Cruz 2021-09-13 12:46:43 +02:00
parent 83a2d2a31f
commit 9b6f7cbdcc
6 changed files with 89 additions and 11 deletions

View file

@ -1,10 +1,24 @@
package api
import (
"fmt"
"net/http"
)
func (a *API) Login(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "FROM API")
body := ParseBody(r)
username := body.String("username")
password := body.String("password")
token, err := a.App.Login(username, password)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if token == "" {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
w.Write([]byte(token))
}