feat(web/auth): add IsLoginWithUserID for request without exit
This commit is contained in:
parent
1fd4f64a9f
commit
d7a9f17792
|
@ -12,15 +12,12 @@ import (
|
|||
"dev.sum7.eu/genofire/golang-lib/web"
|
||||
)
|
||||
|
||||
// GetCurrentUserID get UserID of session in golang-gin
|
||||
func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) {
|
||||
// IsLoginWithUserID get UserID of session in golang-gin
|
||||
func IsLoginWithUserID(c *gin.Context) (uuid.UUID, bool) {
|
||||
session := sessions.Default(c)
|
||||
|
||||
v := session.Get("user_id")
|
||||
if v == nil {
|
||||
c.JSON(http.StatusUnauthorized, web.HTTPError{
|
||||
Message: ErrAPINoSession.Error(),
|
||||
})
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
|
@ -28,6 +25,17 @@ func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) {
|
|||
return id, true
|
||||
}
|
||||
|
||||
// GetCurrentUserID get UserID of session in golang-gin
|
||||
func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) {
|
||||
id, ok := IsLoginWithUserID(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, web.HTTPError{
|
||||
Message: ErrAPINoSession.Error(),
|
||||
})
|
||||
}
|
||||
return id, ok
|
||||
}
|
||||
|
||||
// GetCurrentUser get User of session from database in golang-gin
|
||||
func GetCurrentUser(c *gin.Context, ws *web.Service) (*User, bool) {
|
||||
id, ok := GetCurrentUserID(c)
|
||||
|
|
Loading…
Reference in New Issue