Skip to content

Commit

Permalink
Merge pull request #53 from openhacku-team-a/fix-middleware-auth-userid
Browse files Browse the repository at this point in the history
🐛 (middleware) Set UserID to AuthorizedUserIDField
  • Loading branch information
Shion1305 authored Mar 16, 2024
2 parents 2252b4b + 64e17e3 commit 0ab5117
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions svc/pkg/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,34 @@ func (a auth) VerifyUser() gin.HandlerFunc {
c.AbortWithStatusJSON(400, gin.H{"error": err.Error()})
return
}
result, err := a.loginUC.Do(c, uc.LoginInput{JWT: jwt})
if result == nil || err != nil {
firebaseAuth, err := a.loginUC.Do(c, uc.LoginInput{JWT: jwt})
if firebaseAuth == nil || err != nil {
c.AbortWithError(401, err)
return
}

// Userの存在チェック
if result.UserID == "" {
if firebaseAuth.UserID == "" {
c.AbortWithStatusJSON(500, "user_id is null")
return
}
_, err = a.q.User.WithContext(c).Where(a.q.User.FirebaseUID.Eq(result.UserID)).First()
u, err := a.q.User.WithContext(c).Where(a.q.User.FirebaseUID.Eq(firebaseAuth.UserID)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// 存在しなければ、作成する
err := a.q.User.WithContext(c).Create(&gModel.User{
u = &gModel.User{
UserID: uuid.New().String(),
FirebaseUID: result.UserID,
})
FirebaseUID: firebaseAuth.UserID,
}
err := a.q.User.WithContext(c).Create(u)
if err != nil {
c.AbortWithStatusJSON(500, gin.H{"error": err.Error()})
return
}
}
}

c.Set(AuthorizedUserIDField, result.UserID)
c.Set(AuthorizedUserIDField, u.UserID)
c.Next()
}
}
Expand Down

0 comments on commit 0ab5117

Please sign in to comment.