fix sessions
This commit is contained in:
+1
-8
@@ -57,14 +57,7 @@ func (a *APIHandler) postHandler(c *gin.Context) {
|
||||
logger.Infof("Unable to get session's max age from DB")
|
||||
}
|
||||
|
||||
if sessionMaxAge > 0 {
|
||||
err = SetMaxAge(c, sessionMaxAge*60)
|
||||
if err != nil {
|
||||
logger.Infof("Unable to set session's max age")
|
||||
}
|
||||
}
|
||||
|
||||
err = SetLoginUser(c, loginUser)
|
||||
err = SetLoginUser(c, loginUser, sessionMaxAge)
|
||||
if err == nil {
|
||||
logger.Info("user ", loginUser, " login success")
|
||||
} else {
|
||||
|
||||
+14
-5
@@ -4,7 +4,7 @@ import (
|
||||
"encoding/gob"
|
||||
"s-ui/database/model"
|
||||
|
||||
sessions "github.com/Calidity/gin-sessions"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -16,17 +16,26 @@ func init() {
|
||||
gob.Register(model.User{})
|
||||
}
|
||||
|
||||
func SetLoginUser(c *gin.Context, userName string) error {
|
||||
func SetLoginUser(c *gin.Context, userName string, maxAge int) error {
|
||||
options := sessions.Options{
|
||||
Path: "/",
|
||||
Secure: false,
|
||||
}
|
||||
if maxAge > 0 {
|
||||
options.MaxAge = maxAge * 60
|
||||
}
|
||||
|
||||
s := sessions.Default(c)
|
||||
s.Set(loginUser, userName)
|
||||
s.Options(options)
|
||||
|
||||
return s.Save()
|
||||
}
|
||||
|
||||
func SetMaxAge(c *gin.Context, maxAge int) error {
|
||||
func SetMaxAge(c *gin.Context) error {
|
||||
s := sessions.Default(c)
|
||||
s.Options(sessions.Options{
|
||||
Path: "/",
|
||||
MaxAge: maxAge,
|
||||
Path: "/",
|
||||
})
|
||||
return s.Save()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user