fix: make email verification col nullable
This commit is contained in:
parent
3ee79c3937
commit
969395ccdb
|
@ -18,7 +18,7 @@ type User struct {
|
||||||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
||||||
|
|
||||||
Email string `gorm:"unique" json:"email" bson:"email"`
|
Email string `gorm:"unique" json:"email" bson:"email"`
|
||||||
EmailVerifiedAt int64 `json:"email_verified_at" bson:"email_verified_at"`
|
EmailVerifiedAt *int64 `json:"email_verified_at" bson:"email_verified_at"`
|
||||||
Password *string `gorm:"type:text" json:"password" bson:"password"`
|
Password *string `gorm:"type:text" json:"password" bson:"password"`
|
||||||
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
|
SignupMethods string `json:"signup_methods" bson:"signup_methods"`
|
||||||
GivenName *string `json:"given_name" bson:"given_name"`
|
GivenName *string `json:"given_name" bson:"given_name"`
|
||||||
|
|
|
@ -206,7 +206,8 @@ func OAuthCallbackHandler() gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
user.Roles = strings.Join(inputRoles, ",")
|
user.Roles = strings.Join(inputRoles, ",")
|
||||||
user.EmailVerifiedAt = time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
user.EmailVerifiedAt = &now
|
||||||
user, _ = db.Mgr.AddUser(user)
|
user, _ = db.Mgr.AddUser(user)
|
||||||
} else {
|
} else {
|
||||||
// user exists in db, check if method was google
|
// user exists in db, check if method was google
|
||||||
|
|
|
@ -46,8 +46,9 @@ func VerifyEmailHandler() gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// update email_verified_at in users table
|
// update email_verified_at in users table
|
||||||
if user.EmailVerifiedAt <= 0 {
|
if user.EmailVerifiedAt == nil {
|
||||||
user.EmailVerifiedAt = time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
user.EmailVerifiedAt = &now
|
||||||
db.Mgr.UpdateUser(user)
|
db.Mgr.UpdateUser(user)
|
||||||
}
|
}
|
||||||
// delete from verification table
|
// delete from verification table
|
||||||
|
|
|
@ -36,7 +36,7 @@ func Login(ctx context.Context, params model.LoginInput) (*model.AuthResponse, e
|
||||||
return res, fmt.Errorf(`user has not signed up email & password`)
|
return res, fmt.Errorf(`user has not signed up email & password`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.EmailVerifiedAt <= 0 {
|
if user.EmailVerifiedAt == nil {
|
||||||
return res, fmt.Errorf(`email not verified`)
|
return res, fmt.Errorf(`email not verified`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,10 +54,10 @@ func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse,
|
||||||
log.Println("user with email " + params.Email + " not found")
|
log.Println("user with email " + params.Email + " not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if existingUser.EmailVerifiedAt > 0 {
|
if existingUser.EmailVerifiedAt != nil {
|
||||||
// email is verified
|
// email is verified
|
||||||
return res, fmt.Errorf(`%s has already signed up`, params.Email)
|
return res, fmt.Errorf(`%s has already signed up`, params.Email)
|
||||||
} else if existingUser.ID != "" && existingUser.EmailVerifiedAt <= 0 {
|
} else if existingUser.ID != "" && existingUser.EmailVerifiedAt == nil {
|
||||||
return res, fmt.Errorf("%s has already signed up. please complete the email verification process or reset the password", params.Email)
|
return res, fmt.Errorf("%s has already signed up. please complete the email verification process or reset the password", params.Email)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,8 @@ func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse,
|
||||||
|
|
||||||
user.SignupMethods = enum.BasicAuth.String()
|
user.SignupMethods = enum.BasicAuth.String()
|
||||||
if constants.DISABLE_EMAIL_VERIFICATION {
|
if constants.DISABLE_EMAIL_VERIFICATION {
|
||||||
user.EmailVerifiedAt = time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
user.EmailVerifiedAt = &now
|
||||||
}
|
}
|
||||||
user, err = db.Mgr.AddUser(user)
|
user, err = db.Mgr.AddUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -123,7 +123,7 @@ func UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model
|
||||||
utils.DeleteCookie(gc)
|
utils.DeleteCookie(gc)
|
||||||
|
|
||||||
user.Email = newEmail
|
user.Email = newEmail
|
||||||
user.EmailVerifiedAt = 0
|
user.EmailVerifiedAt = nil
|
||||||
hasEmailChanged = true
|
hasEmailChanged = true
|
||||||
// insert verification request
|
// insert verification request
|
||||||
verificationType := enum.UpdateEmail.String()
|
verificationType := enum.UpdateEmail.String()
|
||||||
|
|
|
@ -84,7 +84,7 @@ func UpdateUser(ctx context.Context, params model.UpdateUserInput) (*model.User,
|
||||||
utils.DeleteCookie(gc)
|
utils.DeleteCookie(gc)
|
||||||
|
|
||||||
user.Email = newEmail
|
user.Email = newEmail
|
||||||
user.EmailVerifiedAt = 0
|
user.EmailVerifiedAt = nil
|
||||||
// insert verification request
|
// insert verification request
|
||||||
verificationType := enum.UpdateEmail.String()
|
verificationType := enum.UpdateEmail.String()
|
||||||
token, err := utils.CreateVerificationToken(newEmail, verificationType)
|
token, err := utils.CreateVerificationToken(newEmail, verificationType)
|
||||||
|
|
|
@ -37,7 +37,8 @@ func VerifyEmail(ctx context.Context, params model.VerifyEmailInput) (*model.Aut
|
||||||
}
|
}
|
||||||
|
|
||||||
// update email_verified_at in users table
|
// update email_verified_at in users table
|
||||||
user.EmailVerifiedAt = time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
user.EmailVerifiedAt = &now
|
||||||
db.Mgr.UpdateUser(user)
|
db.Mgr.UpdateUser(user)
|
||||||
// delete from verification table
|
// delete from verification table
|
||||||
db.Mgr.DeleteVerificationRequest(verificationRequest)
|
db.Mgr.DeleteVerificationRequest(verificationRequest)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetResUser(user db.User) *model.User {
|
func GetResUser(user db.User) *model.User {
|
||||||
isEmailVerified := user.EmailVerifiedAt > 0
|
isEmailVerified := user.EmailVerifiedAt != nil
|
||||||
isPhoneVerified := user.PhoneNumberVerifiedAt != nil
|
isPhoneVerified := user.PhoneNumberVerifiedAt != nil
|
||||||
return &model.User{
|
return &model.User{
|
||||||
ID: user.ID,
|
ID: user.ID,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user