Signup function
(params map[string]interface{}, signupWithActivate bool)
| 56 | |
| 57 | // Signup function |
| 58 | func (authService *AuthService) Signup(params map[string]interface{}, signupWithActivate bool) (response map[string]string, err error) { |
| 59 | account := &models.Account{} |
| 60 | accountColl := mgm.CollectionByName("account") |
| 61 | account.Subdomain = strcase.ToKebab(params["subdomain"].(string)) |
| 62 | |
| 63 | // check if subdomain unique |
| 64 | existentAccount := &models.Account{} |
| 65 | accountColl.First(bson.M{"subdomain": account.Subdomain}, existentAccount) |
| 66 | if existentAccount.ID != primitive.NilObjectID { |
| 67 | return nil, errors.New("subdomain is invalid or already taken") |
| 68 | } |
| 69 | |
| 70 | user := &models.User{} |
| 71 | userColl := mgm.CollectionByName("user") |
| 72 | user.Email = strings.TrimSpace(strings.ToLower(params["email"].(string))) |
| 73 | |
| 74 | // check if email unique |
| 75 | existentUser := &models.User{} |
| 76 | userColl.First(bson.M{"email": user.Email}, existentUser) |
| 77 | if existentUser.ID != primitive.NilObjectID { |
| 78 | return nil, errors.New("email is invalid or already taken") |
| 79 | } |
| 80 | |
| 81 | // create account |
| 82 | trialDays, _ := strconv.Atoi(os.Getenv("TRIAL_DAYS")) |
| 83 | account.TrialPeriodEndsAt = time.Now().AddDate(0, 0, trialDays) |
| 84 | account.PrivacyAccepted = params["privacyAccepted"].(bool) |
| 85 | account.MarketingAccepted = params["marketingAccepted"].(bool) |
| 86 | account.PlanType = os.Getenv("STARTER_PLAN_TYPE") |
| 87 | err = accountColl.Create(account) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | |
| 92 | // create user |
| 93 | user.Role = models.AdminRole |
| 94 | user.AccountOwner = true |
| 95 | user.Active = signupWithActivate |
| 96 | if params["language"] != nil { |
| 97 | user.Language = params["language"].(string) |
| 98 | } else { |
| 99 | user.Language = os.Getenv("LOCALE") |
| 100 | } |
| 101 | ssoUUID, _ := uuid.NewRandom() |
| 102 | user.Sso = ssoUUID.String() |
| 103 | hash, _ := hashPassword(params["password"].(string)) |
| 104 | user.Password = hash |
| 105 | user.AccountID = account.ID |
| 106 | err = userColl.Create(user) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | |
| 111 | go emailService.SendNotificationEmail(os.Getenv("NOTIFIED_ADMIN_EMAIL"), i18n.Tr("en", "authService.signup.subject"), i18n.Tr("en", "authService.signup.messageAdmin", map[string]string{"Subdomain": account.Subdomain, "Email": user.Email}), os.Getenv("LOCALE")) |
| 112 | |
| 113 | if !signupWithActivate { |
| 114 | go emailService.SendActivationEmail(bson.M{"_id": user.ID}) |
| 115 | return nil, err |
nothing calls this directly
no test coverage detected