2021-07-18 09:56:29 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-07-21 08:06:26 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2021-07-28 09:52:11 +00:00
|
|
|
"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"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-18 09:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ForgotPassword(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) {
|
|
|
|
var res *model.Response
|
2021-07-28 09:52:11 +00:00
|
|
|
if constants.DISABLE_BASIC_AUTHENTICATION == "true" {
|
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
|
|
|
|
2021-07-21 08:06:26 +00:00
|
|
|
params.Email = strings.ToLower(params.Email)
|
2021-07-18 09:56:29 +00:00
|
|
|
|
2021-07-21 08:06:26 +00:00
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
|
|
return res, fmt.Errorf("invalid email")
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 08:06:26 +00:00
|
|
|
_, err := db.Mgr.GetUserByEmail(params.Email)
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
2021-07-21 08:06:26 +00:00
|
|
|
return res, fmt.Errorf(`user with this email not found`)
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 08:06:26 +00:00
|
|
|
token, err := utils.CreateVerificationToken(params.Email, enum.ForgotPassword.String())
|
2021-07-18 09:56:29 +00:00
|
|
|
if err != nil {
|
2021-07-21 08:06:26 +00:00
|
|
|
log.Println(`Error generating token`, err)
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
2021-07-21 08:06:26 +00:00
|
|
|
db.Mgr.AddVerification(db.VerificationRequest{
|
|
|
|
Token: token,
|
|
|
|
Identifier: enum.ForgotPassword.String(),
|
|
|
|
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.SendForgotPasswordMail(params.Email, token)
|
|
|
|
}()
|
2021-07-18 09:56:29 +00:00
|
|
|
|
|
|
|
res = &model.Response{
|
2021-07-21 08:06:26 +00:00
|
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
2021-07-18 09:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|