authorizer/server/resolvers/resetPassword.go

60 lines
1.5 KiB
Go
Raw Normal View History

package resolvers
import (
"context"
"fmt"
"strings"
"github.com/authorizerdev/authorizer/server/constants"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/graph/model"
"github.com/authorizerdev/authorizer/server/utils"
)
2021-08-07 08:41:26 +00:00
func ResetPassword(ctx context.Context, params model.ResetPasswordInput) (*model.Response, error) {
var res *model.Response
if constants.DISABLE_BASIC_AUTHENTICATION == "true" {
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
}
verificationRequest, err := db.Mgr.GetVerificationByToken(params.Token)
if err != nil {
return res, fmt.Errorf(`invalid token`)
}
if params.Password != params.ConfirmPassword {
return res, fmt.Errorf(`passwords don't match`)
}
// verify if token exists in db
claim, err := utils.VerifyVerificationToken(params.Token)
if err != nil {
return res, fmt.Errorf(`invalid token`)
}
user, err := db.Mgr.GetUserByEmail(claim.Email)
if err != nil {
return res, err
}
password, _ := utils.HashPassword(params.Password)
user.Password = password
signupMethod := user.SignupMethod
if !strings.Contains(signupMethod, enum.BasicAuth.String()) {
signupMethod = signupMethod + "," + enum.BasicAuth.String()
}
user.SignupMethod = signupMethod
// delete from verification table
db.Mgr.DeleteVerificationRequest(verificationRequest)
db.Mgr.UpdateUser(user)
res = &model.Response{
Message: `Password updated successfully.`,
}
return res, nil
}