2021-07-17 21:59:50 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-18 09:25:20 +05:30
|
|
|
"fmt"
|
2021-07-17 21:59:50 +05:30
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-07-28 15:43:08 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
|
|
"github.com/authorizerdev/authorizer/server/enum"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2021-07-28 15:43:08 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/session"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-17 21:59:50 +05:30
|
|
|
)
|
|
|
|
|
2021-07-28 15:43:08 +05:30
|
|
|
func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse, error) {
|
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
var res *model.AuthResponse
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:33:11 +05:30
|
|
|
if constants.DISABLE_BASIC_AUTHENTICATION {
|
2021-07-28 15:43:08 +05:30
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
2021-07-21 03:34:03 +05:30
|
|
|
if params.ConfirmPassword != params.Password {
|
2021-12-20 23:21:27 +05:30
|
|
|
return res, fmt.Errorf(`password and confirm password does not match`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
2021-07-18 09:25:20 +05:30
|
|
|
return res, fmt.Errorf(`invalid email address`)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// find user with email
|
|
|
|
existingUser, err := db.Mgr.GetUserByEmail(params.Email)
|
|
|
|
if err != nil {
|
2021-12-17 21:25:07 +05:30
|
|
|
log.Println("user with email " + params.Email + " not found")
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-12-22 15:38:51 +05:30
|
|
|
if existingUser.EmailVerifiedAt != nil {
|
2021-07-17 21:59:50 +05:30
|
|
|
// email is verified
|
2021-12-20 23:21:27 +05:30
|
|
|
return res, fmt.Errorf(`%s has already signed up`, params.Email)
|
2021-12-22 15:38:51 +05:30
|
|
|
} else if existingUser.ID != "" && existingUser.EmailVerifiedAt == nil {
|
2021-12-20 23:21:27 +05:30
|
|
|
return res, fmt.Errorf("%s has already signed up. please complete the email verification process or reset the password", params.Email)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
2021-12-20 23:21:27 +05:30
|
|
|
|
2021-12-24 18:42:32 +05:30
|
|
|
inputRoles := []string{}
|
|
|
|
|
|
|
|
if len(params.Roles) > 0 {
|
|
|
|
// check if roles exists
|
|
|
|
if !utils.IsValidRoles(constants.ROLES, params.Roles) {
|
|
|
|
return res, fmt.Errorf(`invalid roles`)
|
|
|
|
} else {
|
|
|
|
inputRoles = params.Roles
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inputRoles = constants.DEFAULT_ROLES
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:59:50 +05:30
|
|
|
user := db.User{
|
2021-07-17 22:39:50 +05:30
|
|
|
Email: params.Email,
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-09-20 10:36:26 +05:30
|
|
|
user.Roles = strings.Join(inputRoles, ",")
|
|
|
|
|
2021-07-17 22:39:50 +05:30
|
|
|
password, _ := utils.HashPassword(params.Password)
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Password = &password
|
2021-07-17 22:39:50 +05:30
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
if params.GivenName != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.GivenName = params.GivenName
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
if params.FamilyName != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.FamilyName = params.FamilyName
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
if params.MiddleName != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.MiddleName = params.MiddleName
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Nickname != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Nickname = params.Nickname
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Gender != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Gender = params.Gender
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Birthdate != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Birthdate = params.Birthdate
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.PhoneNumber != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.PhoneNumber = params.PhoneNumber
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if params.Picture != nil {
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Picture = params.Picture
|
2021-12-22 10:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
user.SignupMethods = enum.BasicAuth.String()
|
2021-12-20 17:33:11 +05:30
|
|
|
if constants.DISABLE_EMAIL_VERIFICATION {
|
2021-12-22 15:38:51 +05:30
|
|
|
now := time.Now().Unix()
|
|
|
|
user.EmailVerifiedAt = &now
|
2021-07-28 15:43:08 +05:30
|
|
|
}
|
2021-12-17 21:25:07 +05:30
|
|
|
user, err = db.Mgr.AddUser(user)
|
2021-07-17 21:59:50 +05:30
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2021-08-07 14:11:26 +05:30
|
|
|
userIdStr := fmt.Sprintf("%v", user.ID)
|
2021-10-13 22:11:41 +05:30
|
|
|
roles := strings.Split(user.Roles, ",")
|
2021-12-24 06:35:02 +05:30
|
|
|
userToReturn := utils.GetResponseUserData(user)
|
2021-07-28 15:43:08 +05:30
|
|
|
|
2021-12-20 17:33:11 +05:30
|
|
|
if !constants.DISABLE_EMAIL_VERIFICATION {
|
2021-07-28 15:43:08 +05:30
|
|
|
// insert verification request
|
|
|
|
verificationType := enum.BasicAuthSignup.String()
|
2021-08-04 15:55:13 +05:30
|
|
|
token, err := utils.CreateVerificationToken(params.Email, verificationType)
|
2021-07-28 15:43:08 +05:30
|
|
|
if err != nil {
|
2021-12-17 21:25:07 +05:30
|
|
|
log.Println(`error generating token`, err)
|
2021-07-28 15:43:08 +05:30
|
|
|
}
|
|
|
|
db.Mgr.AddVerification(db.VerificationRequest{
|
|
|
|
Token: token,
|
|
|
|
Identifier: verificationType,
|
|
|
|
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
|
|
|
|
Email: params.Email,
|
|
|
|
})
|
|
|
|
|
|
|
|
// exec it as go routin so that we can reduce the api latency
|
|
|
|
go func() {
|
|
|
|
utils.SendVerificationMail(params.Email, token)
|
|
|
|
}()
|
|
|
|
|
|
|
|
res = &model.AuthResponse{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
User: userToReturn,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2021-10-13 22:11:41 +05:30
|
|
|
refreshToken, _, _ := utils.CreateAuthToken(user, enum.RefreshToken, roles)
|
2021-07-28 15:43:08 +05:30
|
|
|
|
2021-10-13 22:11:41 +05:30
|
|
|
accessToken, expiresAt, _ := utils.CreateAuthToken(user, enum.AccessToken, roles)
|
2021-07-28 15:43:08 +05:30
|
|
|
|
2021-10-27 23:15:38 +05:30
|
|
|
session.SetToken(userIdStr, accessToken, refreshToken)
|
2021-12-23 10:31:52 +05:30
|
|
|
utils.CreateSession(user.ID, gc)
|
2021-07-28 15:43:08 +05:30
|
|
|
res = &model.AuthResponse{
|
2021-12-22 10:51:12 +05:30
|
|
|
Message: `Signed up successfully.`,
|
|
|
|
AccessToken: &accessToken,
|
|
|
|
ExpiresAt: &expiresAt,
|
|
|
|
User: userToReturn,
|
2021-07-28 15:43:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
utils.SetCookie(gc, accessToken)
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|