-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
66 lines (59 loc) · 1.38 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
66
package api
import (
"encoding/json"
"net/http"
"os"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
func SessionSet(c echo.Context, user *User) {
secure := true
if os.Getenv("COOKIE_SECURE") == "false" {
secure = false
}
sameSite := http.SameSiteNoneMode
if os.Getenv("SAME_SITE") == "lax" {
sameSite = http.SameSiteLaxMode
}
if os.Getenv("SAME_SITE") == "none" {
sameSite = http.SameSiteNoneMode
}
if os.Getenv("SAME_SITE") == "strict" {
sameSite = http.SameSiteStrictMode
}
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7,
HttpOnly: true,
SameSite: sameSite,
Secure: secure,
}
sessionUser, _ := json.Marshal(user)
sess.Values["valid"] = true
sess.Values["user"] = &sessionUser
sess.Save(c.Request(), c.Response())
}
func SessionUserGet(c echo.Context) (u *User, contains bool) {
sess, _ := session.Get("session", c)
val := sess.Values["user"]
var user []byte
user, contains = val.([]byte)
if contains == false {
return nil, contains
}
json.Unmarshal(user, &u)
return u, true
}
func SessionDelete(c echo.Context) {
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: -1,
HttpOnly: true,
}
sess.Values["valid"] = nil
sess.Values["user"] = nil
sess.Save(c.Request(), c.Response())
}