42 lines
736 B
Go
42 lines
736 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/gorilla/sessions"
|
||
|
"github.com/labstack/echo-contrib/session"
|
||
|
"github.com/labstack/echo/v4"
|
||
|
"go.neonxp.ru/framework/pkg/middleware"
|
||
|
"go.neonxp.ru/framework/pkg/model"
|
||
|
)
|
||
|
|
||
|
func GetUserCtx(ctx context.Context) *model.User {
|
||
|
u := ctx.Value(middleware.ContextKey("user"))
|
||
|
if u == nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
if u, ok := u.(model.User); ok {
|
||
|
return &u
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func SetUser(c echo.Context, u *model.User, maxage int) error {
|
||
|
sess, err := session.Get("user", c)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
sess.Values["user"] = u
|
||
|
sess.Options = &sessions.Options{
|
||
|
Path: "/",
|
||
|
MaxAge: maxage,
|
||
|
HttpOnly: true,
|
||
|
Secure: true,
|
||
|
}
|
||
|
|
||
|
return sess.Save(c.Request(), c.Response())
|
||
|
}
|