-
Notifications
You must be signed in to change notification settings - Fork 4
/
login.go
52 lines (45 loc) · 1.22 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package routers
import (
"encoding/json"
"net/http"
"time"
"github.com/SaiKumarMalve/Gator-News/bd"
"github.com/SaiKumarMalve/Gator-News/jwt"
"github.com/SaiKumarMalve/Gator-News/models"
)
/* Login function is defined to perform the login*/
func Login(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
var t models.User
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
http.Error(w, "Username and/or password invalid"+err.Error(), 400)
return
}
if len(t.Email) == 0 {
http.Error(w, "The user Email is required ", 400)
return
}
document, exists := bd.TryLogin(t.Email, t.Password)
if exists == false {
http.Error(w, "Username and/or password invalid", 400)
return
}
jwtKey, err := jwt.GenerateJWT(document)
if err != nil {
http.Error(w, "An error occured while trying to generate the corresponding token"+err.Error(), 400)
return
}
resp := models.AnswerLogin{
Token: jwtKey,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(resp)
expirationTime := time.Now().Add(24 * time.Hour)
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: jwtKey,
Expires: expirationTime,
})
}