-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
65 lines (50 loc) · 1.47 KB
/
session.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
57
58
59
60
61
62
63
64
65
package common
import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
core "github.com/kode-magic/eco-bowl-api/core/entities"
"github.com/kode-magic/eco-bowl-api/utils"
"time"
)
func ChangePasswordSession(session *session.Store, ctx *fiber.Ctx, user *core.User) error {
sess, sessErr := session.Get(ctx)
if sessErr != nil {
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "something went wrong: " + sessErr.Error(),
})
}
sess.Set(utils.KEY, true)
sess.Set(utils.USERID, user.ID)
sess.SetExpiry(time.Minute * 5)
sess.Set(utils.NAME, fmt.Sprintf("%s %s", user.FirstName, user.LastName))
err := sess.Save()
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "something went wrong: " + err.Error(),
})
}
return nil
}
func LoginSession(session *session.Store, ctx *fiber.Ctx, user *core.User) error {
sess, sessErr := session.Get(ctx)
if sessErr != nil {
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": "something went wrong: " + sessErr.Error(),
})
}
sess.Set(utils.KEY, true)
sess.Set(utils.USERID, user.ID)
sess.Set(utils.NAME, fmt.Sprintf("%s %s", user.FirstName, user.LastName))
sessSaveErr := sess.Save()
if sessSaveErr != nil {
ctxErr := ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": sessSaveErr.Error(),
})
if ctxErr != nil {
return ctxErr
}
return nil
}
return nil
}