Fix for login

This commit is contained in:
Aurélie Delhaie
2022-05-15 21:26:35 +02:00
parent 22ea6b0e8d
commit 246fe2fe6c
6 changed files with 57 additions and 25 deletions

View File

@@ -14,6 +14,10 @@ type Credential struct {
Password string `json:"password"`
}
type TokenValidation struct {
Valid bool `json:"valid"`
}
func Login(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
@@ -62,3 +66,31 @@ func Register(w http.ResponseWriter, r *http.Request) {
}
ok(payload, w, r)
}
func CheckToken(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
credential := new(authentication.AccessToken)
err = json.Unmarshal(body, credential)
if err != nil {
internalServerError(w, r)
log.Println(err)
return
}
_, err = authentication.ParseToken(credential.Token)
if err != nil {
payload := TokenValidation{
Valid: false,
}
ok(payload, w, r)
return
}
payload := TokenValidation{
Valid: true,
}
ok(payload, w, r)
}