Skip to content

Commit

Permalink
fix(server): fix signup panic error
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Aug 24, 2023
1 parent faf5f3f commit ace22da
Showing 1 changed file with 38 additions and 57 deletions.
95 changes: 38 additions & 57 deletions server/internal/adapter/http/user.go
Expand Up @@ -2,10 +2,7 @@ package http

import (
"context"
"errors"

"github.com/reearth/reearth/server/internal/adapter"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearthx/account/accountdomain"
"github.com/reearth/reearthx/account/accountdomain/user"
"github.com/reearth/reearthx/account/accountusecase/accountinterfaces"
Expand All @@ -32,14 +29,14 @@ type SignupInput struct {
Sub *string `json:"sub"`
Secret *string `json:"secret"`
UserID *accountdomain.UserID `json:"userId"`
WorkspaceID *accountdomain.WorkspaceID `json:"teamId"`
Name *string `json:"name"`
// Username is an alias of Name
Username *string `json:"username"`
Email *string `json:"email"`
Password *string `json:"password"`
Theme *user.Theme `json:"theme"`
Lang *language.Tag `json:"lang"`
WorkspaceID *accountdomain.WorkspaceID `json:"workspaceId"`
TeamID *accountdomain.WorkspaceID `json:"teamId"` // TeamID is an alias of WorkspaceID
Name string `json:"name"`
Username string `json:"username"` // ysername is an alias of Name
Email string `json:"email"`
Password string `json:"password"`
Theme *user.Theme `json:"theme"`
Lang *language.Tag `json:"lang"`
}

type CreateVerificationInput struct {
Expand All @@ -51,66 +48,50 @@ type VerifyUserOutput struct {
Verified bool `json:"verified"`
}

type CreateUserInput struct {
Sub string `json:"sub"`
Secret string `json:"secret"`
UserID *id.UserID `json:"userId"`
WorkspaceID *accountdomain.WorkspaceID `json:"teamId"`
}

type SignupOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}

func (c *UserController) Signup(ctx context.Context, input SignupInput) (SignupOutput, error) {
var u *user.User
var err error

name := input.Name
if name == nil {
name = input.Username
if input.Name == "" && input.Username != "" {
input.Name = input.Username
}
if name == nil {
name = input.Email
if input.WorkspaceID == nil && input.TeamID != nil {
input.WorkspaceID = input.TeamID
}

if au := adapter.GetAuthInfo(ctx); au != nil {
var name2 string
if name != nil {
name2 = *name
if input.Sub != nil && *input.Sub != "" && input.Email != "" && input.Name != "" {
u, err := c.usecase.SignupOIDC(ctx, accountinterfaces.SignupOIDCParam{
Name: input.Name,
Email: input.Email,
Sub: *input.Sub,
Secret: input.Secret,
})

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

u, err = c.usecase.SignupOIDC(ctx, accountinterfaces.SignupOIDCParam{
Sub: au.Sub,
AccessToken: au.Token,
Issuer: au.Iss,
Email: au.Email,
Name: name2,
Secret: input.Secret,
User: accountinterfaces.SignupUserParam{
UserID: input.UserID,
WorkspaceID: input.WorkspaceID,
Lang: input.Lang,
Theme: input.Theme,
},
})
} else if name != nil && input.Email != nil {
u, err = c.usecase.Signup(ctx, accountinterfaces.SignupParam{
Name: *name,
Email: *input.Email,
Password: *input.Password,
Secret: input.Secret,
UserID: input.UserID,
WorkspaceID: input.WorkspaceID,
Lang: input.Lang,
Theme: input.Theme,
})
} else {
err = errors.New("invalid params")
return SignupOutput{
ID: u.ID().String(),
Name: u.Name(),
Email: u.Email(),
}, nil
}

u, err := c.usecase.Signup(ctx, accountinterfaces.SignupParam{
Name: input.Name,
Email: input.Email,
Password: input.Password,
Secret: input.Secret,
UserID: input.UserID,
WorkspaceID: input.WorkspaceID,
Lang: input.Lang,
Theme: input.Theme,
})

if err != nil {
return SignupOutput{}, err
}
Expand Down

0 comments on commit ace22da

Please sign in to comment.