Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: signup fails when password is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 14, 2022
1 parent 9bde8a4 commit 27c2f0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
46 changes: 23 additions & 23 deletions internal/usecase/interactor/user.go
Expand Up @@ -133,13 +133,18 @@ func (i *User) Fetch(ctx context.Context, ids []id.UserID, operator *usecase.Ope

func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.User, _ *user.Team, err error) {
var team *user.Team
var email, name string
var auth *user.Auth
var email, name string
var tx repo.Tx
isOidc := inp.Secret != nil && inp.Sub != nil

isOidc := inp.Sub != nil && inp.Password == nil
isAuth := inp.Name != nil && inp.Email != nil && inp.Password != nil
if !isAuth && !isOidc {
return
return nil, nil, errors.New("invalid params")
}

if i.signupSecret != "" && *inp.Secret != i.signupSecret {
return nil, nil, interfaces.ErrSignupInvalidSecret
}

tx, err = i.transaction.Begin()
Expand All @@ -152,20 +157,25 @@ func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.
}
}()

if isOidc {
// Auth0
if i.signupSecret != "" && *inp.Secret != i.signupSecret {
return nil, nil, interfaces.ErrSignupInvalidSecret
// Check if team already exists
if inp.TeamID != nil {
existed, err := i.teamRepo.FindByID(ctx, *inp.TeamID)
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return nil, nil, err
}
if existed != nil {
return nil, nil, errors.New("existed team")
}
}

if isOidc {
if len(*inp.Sub) == 0 {
return nil, nil, errors.New("sub is required")
}
name, email, auth, err = i.oidcSignup(ctx, inp)
if err != nil {
return
}

} else if isAuth {
if *inp.Name == "" {
return nil, nil, interfaces.ErrSignupInvalidName
Expand All @@ -188,41 +198,31 @@ func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.
}
}

// Check if team already exists
if inp.TeamID != nil {
existed, err := i.teamRepo.FindByID(ctx, *inp.TeamID)
if err != nil && !errors.Is(err, rerror.ErrNotFound) {
return nil, nil, err
}
if existed != nil {
return nil, nil, errors.New("existed team")
}
}

// Initialize user and team
u, team, err = user.Init(user.InitParams{
Email: email,
Name: name,
Sub: auth,
Password: *inp.Password,
Password: inp.Password,
Lang: inp.Lang,
Theme: inp.Theme,
UserID: inp.UserID,
TeamID: inp.TeamID,
})

if err != nil {
return nil, nil, err
}

if err := i.userRepo.Save(ctx, u); err != nil {
return nil, nil, err
}

if err := i.teamRepo.Save(ctx, team); err != nil {
return nil, nil, err
}
if tx != nil {
tx.Commit()
}

tx.Commit()
return u, team, nil
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/user/initializer.go
Expand Up @@ -8,7 +8,7 @@ type InitParams struct {
Email string
Name string
Sub *Auth
Password string
Password *string
Lang *language.Tag
Theme *Theme
UserID *ID
Expand All @@ -33,15 +33,17 @@ func Init(p InitParams) (*User, *Team, error) {
p.Sub = GenReearthSub(p.UserID.String())
}

u, err := New().
b := New().
ID(*p.UserID).
Name(p.Name).
Email(p.Email).
Auths([]Auth{*p.Sub}).
Lang(*p.Lang).
PasswordPlainText(p.Password).
Theme(*p.Theme).
Build()
Theme(*p.Theme)
if p.Password != nil {
b = b.PasswordPlainText(*p.Password)
}
u, err := b.Build()
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 27c2f0c

Please sign in to comment.