Add tokenkey to the gin context
This commit is contained in:
parent
8dfb5c7a69
commit
8e8705c6e5
|
|
@ -104,7 +104,7 @@ func (a *Auth) requireToken(auth authenticate) gin.HandlerFunc {
|
|||
|
||||
if user != nil || token != "" {
|
||||
if ok, userID := auth(token, user); ok {
|
||||
RegisterAuthentication(ctx, user, userID)
|
||||
RegisterAuthentication(ctx, user, userID, token)
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
|
|
|
|||
10
auth/util.go
10
auth/util.go
|
|
@ -5,10 +5,11 @@ import (
|
|||
"github.com/jmattheis/memo/model"
|
||||
)
|
||||
|
||||
// RegisterAuthentication registers the user or the user id; The id can later be obtained by GetUserID.
|
||||
func RegisterAuthentication(ctx *gin.Context, user *model.User, userID uint) {
|
||||
// RegisterAuthentication registers the user id, user and or token.
|
||||
func RegisterAuthentication(ctx *gin.Context, user *model.User, userID uint, tokenID string) {
|
||||
ctx.Set("user", user)
|
||||
ctx.Set("userid", userID)
|
||||
ctx.Set("tokenid", tokenID)
|
||||
}
|
||||
|
||||
// GetUserID returns the user id which was previously registered by RegisterAuthentication.
|
||||
|
|
@ -24,3 +25,8 @@ func GetUserID(ctx *gin.Context) uint {
|
|||
|
||||
return user.ID
|
||||
}
|
||||
|
||||
// GetTokenID returns the tokenID
|
||||
func GetTokenID(ctx *gin.Context) string {
|
||||
return ctx.MustGet("tokenid").(string)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,16 @@ func (s *UtilSuite) Test_getID() {
|
|||
})
|
||||
}
|
||||
|
||||
func (s *UtilSuite) expectUserIDWith(user *model.User, tokenID uint, expectedID uint) {
|
||||
func (s *UtilSuite) Test_getToken() {
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
RegisterAuthentication(ctx, user, tokenID)
|
||||
RegisterAuthentication(ctx, nil, 1, "asdasda")
|
||||
actualID := GetTokenID(ctx)
|
||||
assert.Equal(s.T(), "asdasda", actualID)
|
||||
}
|
||||
|
||||
func (s *UtilSuite) expectUserIDWith(user *model.User, tokenUserID uint, expectedID uint) {
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
RegisterAuthentication(ctx, user, tokenUserID, "")
|
||||
actualID := GetUserID(ctx)
|
||||
assert.Equal(s.T(), expectedID, actualID)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue