mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-05 16:56:22 +00:00
⚡ refactor user service methods, implement OIDC login and user management features, and enhance token handling
This commit is contained in:
22
internal/ctxutils/token.go
Normal file
22
internal/ctxutils/token.go
Normal file
@ -0,0 +1,22 @@
|
||||
package ctxutils
|
||||
|
||||
import (
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"github.com/snowykami/neo-blog/pkg/utils"
|
||||
)
|
||||
|
||||
func SetTokenCookie(c *app.RequestContext, token string) {
|
||||
c.SetCookie("token", token, utils.Env.GetAsInt(constant.EnvKeyTokenDuration, constant.EnvKeyTokenDurationDefault), "/", "", protocol.CookieSameSiteLaxMode, true, true)
|
||||
}
|
||||
|
||||
func SetTokenAndRefreshTokenCookie(c *app.RequestContext, token, refreshToken string) {
|
||||
c.SetCookie("token", token, utils.Env.GetAsInt(constant.EnvKeyTokenDuration, constant.EnvKeyTokenDurationDefault), "/", "", protocol.CookieSameSiteLaxMode, true, true)
|
||||
c.SetCookie("refresh_token", refreshToken, -1, "/", "", protocol.CookieSameSiteLaxMode, true, true)
|
||||
}
|
||||
|
||||
func ClearTokenAndRefreshTokenCookie(c *app.RequestContext) {
|
||||
c.SetCookie("token", "", -1, "/", "", protocol.CookieSameSiteLaxMode, true, true)
|
||||
c.SetCookie("refresh_token", "", -1, "/", "", protocol.CookieSameSiteLaxMode, true, true)
|
||||
}
|
19
internal/ctxutils/user.go
Normal file
19
internal/ctxutils/user.go
Normal file
@ -0,0 +1,19 @@
|
||||
package ctxutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/internal/repo"
|
||||
)
|
||||
|
||||
func GetCurrentUser(ctx context.Context) *model.User {
|
||||
userIDValue := ctx.Value("user_id").(uint)
|
||||
if userIDValue <= 0 {
|
||||
return nil
|
||||
}
|
||||
user, err := repo.User.GetUserByID(userIDValue)
|
||||
if err != nil || user == nil || user.ID == 0 {
|
||||
return nil
|
||||
}
|
||||
return user
|
||||
}
|
Reference in New Issue
Block a user