-
Notifications
You must be signed in to change notification settings - Fork 99
/
utils.go
53 lines (43 loc) · 1.27 KB
/
utils.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
package auth
import (
"net/http"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/auth/claims"
"github.com/qor/qor/utils"
)
// CurrentUser context key to get current user from Request
const CurrentUser utils.ContextKey = "current_user"
// GetCurrentUser get current user from request
func (auth *Auth) GetCurrentUser(req *http.Request) interface{} {
if currentUser := req.Context().Value(CurrentUser); currentUser != nil {
return currentUser
}
claims, err := auth.SessionStorer.Get(req)
if err == nil {
context := &Context{Auth: auth, Claims: claims, Request: req}
if user, err := auth.UserStorer.Get(claims, context); err == nil {
return user
}
}
return nil
}
// GetDB get db from request
func (auth *Auth) GetDB(request *http.Request) *gorm.DB {
db := request.Context().Value(utils.ContextDBName)
if tx, ok := db.(*gorm.DB); ok {
return tx
}
return auth.Config.DB
}
// Login sign user in
func (auth *Auth) Login(w http.ResponseWriter, req *http.Request, claimer claims.ClaimerInterface) error {
claims := claimer.ToClaims()
now := time.Now()
claims.LastLoginAt = &now
return auth.SessionStorer.Update(w, req, claims)
}
// Logout sign current user out
func (auth *Auth) Logout(w http.ResponseWriter, req *http.Request) {
auth.SessionStorer.Delete(w, req)
}