-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
40 lines (32 loc) · 965 Bytes
/
auth.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
package handler
import (
"encoding/json"
"net/http"
"github.com/faelp22/tcs_curso/stoq/entity"
)
func authLogin() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := entity.User{}
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse User from JSON", "codigo": 500}`))
return
}
admin := entity.NewAdmin()
token := entity.Token{
Token: entity.USER_TOKEN,
}
if user.Username == admin.Username && user.Password == admin.Password {
err = json.NewEncoder(w).Encode(token)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse Product to JSON", "codigo": 500}`))
return
}
} else {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"MSG": "Error to login, check username and password", "codigo": 401}`))
}
})
}