2021-07-17 16:29:50 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-18 03:55:20 +00:00
|
|
|
"fmt"
|
2021-07-17 16:29:50 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/yauthdev/yauth/server/db"
|
|
|
|
"github.com/yauthdev/yauth/server/enum"
|
|
|
|
"github.com/yauthdev/yauth/server/graph/model"
|
|
|
|
"github.com/yauthdev/yauth/server/utils"
|
|
|
|
)
|
|
|
|
|
2021-07-18 03:55:20 +00:00
|
|
|
func Signup(ctx context.Context, params model.SignUpInput) (*model.Response, error) {
|
|
|
|
var res *model.Response
|
2021-07-17 16:29:50 +00:00
|
|
|
if params.CofirmPassword != params.Password {
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`passowrd and confirm password does not match`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`invalid email address`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find user with email
|
|
|
|
existingUser, err := db.Mgr.GetUserByEmail(params.Email)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("User with email " + params.Email + " not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingUser.EmailVerifiedAt > 0 {
|
|
|
|
// email is verified
|
2021-07-18 03:55:20 +00:00
|
|
|
return res, fmt.Errorf(`you have already signed up. Please login`)
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
user := db.User{
|
2021-07-17 17:09:50 +00:00
|
|
|
Email: params.Email,
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 17:09:50 +00:00
|
|
|
password, _ := utils.HashPassword(params.Password)
|
|
|
|
user.Password = password
|
|
|
|
|
2021-07-17 16:29:50 +00:00
|
|
|
if params.FirstName != nil {
|
|
|
|
user.FirstName = *params.FirstName
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.LastName != nil {
|
|
|
|
user.LastName = *params.LastName
|
|
|
|
}
|
|
|
|
|
|
|
|
user.SignupMethod = enum.BasicAuth.String()
|
|
|
|
_, err = db.Mgr.SaveUser(user)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert verification request
|
2021-07-18 03:55:20 +00:00
|
|
|
verificationType := enum.BasicAuthSignup.String()
|
2021-07-17 16:29:50 +00:00
|
|
|
token, err := utils.CreateVerificationToken(params.Email, verificationType)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`Error generating token`, err)
|
|
|
|
}
|
2021-07-18 04:22:54 +00:00
|
|
|
db.Mgr.AddVerification(db.VerificationRequest{
|
2021-07-17 16:29:50 +00:00
|
|
|
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)
|
|
|
|
}()
|
|
|
|
|
2021-07-18 03:55:20 +00:00
|
|
|
res = &model.Response{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|