44 lines
1010 B
Go
44 lines
1010 B
Go
package session
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
var store = sessions.NewCookieStore([]byte("f0q0qew0!)§(ds9713lsda231"))
|
|
|
|
const LoginSessionName = "session"
|
|
|
|
func GetUsername(c *gin.Context) (string, bool) {
|
|
session, _ := getSession(c)
|
|
username, ok := session.Values["username"].(string)
|
|
return username, ok
|
|
}
|
|
|
|
func GetAuthenticated(c *gin.Context) (bool, bool) {
|
|
session, _ := getSession(c)
|
|
auth, ok := session.Values["authenticated"].(bool)
|
|
return auth, ok
|
|
}
|
|
|
|
func getSession(c *gin.Context) (*sessions.Session, error) {
|
|
return store.Get(c.Request, LoginSessionName)
|
|
}
|
|
|
|
func InvalidateSession(c *gin.Context) error {
|
|
session, _ := getSession(c)
|
|
session.Options.MaxAge = -1
|
|
err := session.Save(c.Request, c.Writer)
|
|
|
|
return err
|
|
}
|
|
|
|
func SetLoginSession(username string, c *gin.Context) error {
|
|
session, _ := getSession(c)
|
|
session.Values["authenticated"] = true
|
|
session.Values["username"] = username
|
|
err := session.Save(c.Request, c.Writer)
|
|
|
|
return err
|
|
}
|