-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_login_post.go
56 lines (45 loc) · 1.36 KB
/
auth_login_post.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
53
54
55
56
package admin
import (
"context"
"fmt"
"net/http"
"github.com/gaydin/journey/admin/generated"
"github.com/gaydin/journey/authentication"
"github.com/gaydin/journey/date"
)
func (h *Handler) AdminV1APIAuthLoginPost(ctx context.Context, req *generated.ParamsAuthLogin) (generated.AdminV1APIAuthLoginPostRes, error) {
if !authentication.LoginIsCorrect(ctx, h.db, req.Login, req.Password) {
return &generated.Unauthorized{}, nil
}
authCookie, err := h.newAuthCookie(ctx, req.Login)
if err != nil {
return &generated.AdminV1APIAuthLoginPostOK{}, err
}
return &generated.AdminV1APIAuthLoginPostOK{
SetCookie: generated.NewOptString(authCookie),
}, nil
}
func (h *Handler) newAuthCookie(ctx context.Context, login string) (string, error) {
value := map[string]string{
"name": login,
}
encoded, err := authentication.CookieHandler.Encode("session", value)
if err != nil {
return "", fmt.Errorf("encode %s, err: %w", encoded, err)
}
cookie := &http.Cookie{
Name: "session",
Value: encoded,
Path: "/admin/",
//HttpOnly: true,
}
user, err := h.db.RetrieveUserByName(ctx, login)
if err != nil {
return "", fmt.Errorf("couldn't get user ID %w", err)
}
err = h.db.UpdateLastLogin(ctx, date.GetCurrentTime(), user.Id)
if err != nil {
return "", fmt.Errorf("couldn't update last login date of a user %w", err)
}
return cookie.String(), nil
}