-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
31 lines (29 loc) · 940 Bytes
/
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
package middleware
import (
"github.com/dominiclet/golang-base/handler/session"
ctxwrapper "github.com/dominiclet/golang-base/lib/ctx_wrapper"
"github.com/dominiclet/golang-base/lib/httpresp"
"github.com/dominiclet/golang-base/lib/resperror"
"github.com/gin-gonic/gin"
)
// Check if user is authenticated (has a valid ongoing session)
func (m *Middleware) AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
token, err := c.Cookie(session.CookieKey)
if err != nil {
m.logger.Error("Session cookie not found")
httpresp.SendError(c, resperror.NewError(resperror.Unauthorized))
c.Abort()
return
}
user, err := m.sessionService.GetSessionByToken(token)
if err != nil {
m.logger.WithField("err", err).Error("Failed to get session")
httpresp.SendError(c, resperror.NewError(resperror.Unauthorized))
c.Abort()
return
}
// Inject user object into context
ctxwrapper.SetUser(c, *user)
}
}