51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package resolvers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
func ForgotPassword(ctx context.Context, params model.ForgotPasswordInput) (*model.Response, error) {
|
|
var res *model.Response
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
return res, fmt.Errorf("invalid email")
|
|
}
|
|
|
|
_, err := db.Mgr.GetUserByEmail(params.Email)
|
|
if err != nil {
|
|
return res, fmt.Errorf(`user with this email not found`)
|
|
}
|
|
|
|
token, err := utils.CreateVerificationToken(params.Email, enum.ForgotPassword.String())
|
|
if err != nil {
|
|
log.Println(`Error generating token`, err)
|
|
}
|
|
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)
|
|
}()
|
|
|
|
res = &model.Response{
|
|
Message: `Please check your inbox! We have sent a password reset link.`,
|
|
}
|
|
|
|
return res, nil
|
|
}
|