Skip to content

Commit

Permalink
[ONE-288] fix: ソーシャルログインすると別のアカウントにログインされる
Browse files Browse the repository at this point in the history
  • Loading branch information
ken109 committed Mar 29, 2022
1 parent 644bd91 commit 01f1b59
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
28 changes: 3 additions & 25 deletions doc/postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,6 @@
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"firebase_uid\": \"{{firebase_uid}}\",\n \"email\": \"{{email}}\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{base}}/users",
"host": [
Expand Down Expand Up @@ -1050,14 +1041,6 @@
"if (pm.collectionVariables.get(\"token\") && ",
" pm.collectionVariables.get(\"expire\") > now()) return;",
"",
"const env = pm.collectionVariables.get(\"env\")",
"",
"const tenantId = {",
" \"local\": \"local-mdch7\",",
" \"dev\": \"development-4hnty\",",
" \"stg\": \"staging-n5vnx\"",
"}",
"",
"pm.sendRequest({",
" url: 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyDt6GF7spx2fU8r8x-hNiACJSN_P_oE5UM',",
" method: 'POST',",
Expand All @@ -1067,8 +1050,7 @@
" {",
" \"email\": pm.collectionVariables.get(\"email\"),",
" \"password\": pm.collectionVariables.get(\"password\"),",
" \"returnSecureToken\": true,",
" ...(env === \"prd\" ? {} : {\"tenantId\": tenantId[env]})",
" \"returnSecureToken\": true",
" }",
" )}",
"}, (err, res) => {",
Expand All @@ -1090,11 +1072,6 @@
}
],
"variable": [
{
"key": "env",
"value": "local",
"type": "string"
},
{
"key": "base",
"value": "https://api.dev.createone.me"
Expand All @@ -1105,7 +1082,8 @@
},
{
"key": "password",
"value": ""
"value": "",
"type": "string"
},
{
"key": "token",
Expand Down
6 changes: 3 additions & 3 deletions domain/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func generateUsername() string {
return string(b)
}

func NewUser(_ context.Context, dto *request.UserCreate) (*User, error) {
func NewUser(ctx context.Context, email string) (*User, error) {
stripeUserID := ""
var user = User{
FirebaseUID: dto.FirebaseUID,
Email: dto.Email,
FirebaseUID: ctx.FirebaseUID(),
Email: email,
Username: generateUsername(),
PaypalConnected: false,
StripeUserID: &stripeUserID,
Expand Down
12 changes: 12 additions & 0 deletions infrastructure/gcp/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type IFirebase interface {
AuthClient() gcp.FirebaseAuthClient
GetUserEmail(firebaseUID string) (string, error)
SetClaimsUID(firebaseUID string, uid uint) error
}

Expand All @@ -30,6 +31,17 @@ func (i firebase) AuthClient() gcp.FirebaseAuthClient {
return i.client
}

func (i firebase) GetUserEmail(firebaseUID string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()

user, err := i.client.GetUser(ctx, firebaseUID)
if err != nil {
return "", errors.NewUnexpected(err)
}
return user.Email, nil
}

func (i firebase) SetClaimsUID(firebaseUID string, uid uint) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
Expand Down
12 changes: 11 additions & 1 deletion infrastructure/persistence/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ func NewUser() repository.IUser {
func (u user) Create(ctx context.Context, user *entity.User) (uint, error) {
db := ctx.DB()

if err := db.FirstOrCreate(user).Error; err != nil {
var current entity.User
err := db.Where(&entity.User{Email: user.Email}).First(&current).Error
if err != nil && err != gorm.ErrRecordNotFound {
return 0, dbError(err)
}

if current.ID != 0 {
return current.ID, nil
}

if err := db.Create(user).Error; err != nil {
return 0, dbError(err)
}
return user.ID, nil
Expand Down
8 changes: 1 addition & 7 deletions interface/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ func NewUser(uuc usecase.IUser) *User {
}

func (u User) Create(ctx context.Context, c *gin.Context) error {
var req request.UserCreate

if !bind(c, &req) {
return nil
}

id, err := u.userUseCase.Create(ctx, &req)
id, err := u.userUseCase.Create(ctx)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions resource/request/user.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package request

type UserCreate struct {
FirebaseUID string `json:"firebase_uid"`
Email string `json:"email"`
Email string `json:"email"`
}

// profile
Expand Down
11 changes: 8 additions & 3 deletions usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

type IUser interface {
Create(ctx context.Context, req *request.UserCreate) (uint, error)
Create(ctx context.Context) (uint, error)

GetByID(ctx context.Context, id uint) (*entity.User, error)
GetByUsername(ctx context.Context, username string) (*entity.User, error)
Expand Down Expand Up @@ -72,8 +72,13 @@ func NewUser(ur repository.IUser, tr repository.ITransaction, ar repository.IArt
}
}

func (u user) Create(ctx context.Context, req *request.UserCreate) (uint, error) {
newUser, err := entity.NewUser(ctx, req)
func (u user) Create(ctx context.Context) (uint, error) {
email, err := u.firebase.GetUserEmail(ctx.FirebaseUID())
if err != nil {
return 0, err
}

newUser, err := entity.NewUser(ctx, email)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 01f1b59

Please sign in to comment.