2021-07-18 07:26:17 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-01-21 08:04:04 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/email"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-18 07:26:17 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// ResendVerifyEmailResolver is a resolver for resend verify email mutation
|
|
|
|
func ResendVerifyEmailResolver(ctx context.Context, params model.ResendVerifyEmailInput) (*model.Response, error) {
|
2021-07-18 07:26:17 +00:00
|
|
|
var res *model.Response
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
|
|
return res, fmt.Errorf("invalid email")
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
if !utils.IsValidVerificationIdentifier(params.Identifier) {
|
|
|
|
return res, fmt.Errorf("invalid identifier")
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:04:04 +00:00
|
|
|
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(params.Email, params.Identifier)
|
2021-07-18 07:26:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf(`verification request not found`)
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
// delete current verification and create new one
|
2022-01-21 08:04:04 +00:00
|
|
|
err = db.Provider.DeleteVerificationRequest(verificationRequest)
|
2021-12-23 05:01:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error deleting verification request:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := utils.CreateVerificationToken(params.Email, params.Identifier)
|
2021-07-18 07:26:17 +00:00
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println(`error generating token`, err)
|
2021-07-18 07:26:17 +00:00
|
|
|
}
|
2022-01-21 08:04:04 +00:00
|
|
|
db.Provider.AddVerificationRequest(models.VerificationRequest{
|
2021-07-18 07:26:17 +00:00
|
|
|
Token: token,
|
2021-12-23 05:01:52 +00:00
|
|
|
Identifier: params.Identifier,
|
2021-07-18 07:26:17 +00:00
|
|
|
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() {
|
2022-01-17 06:02:13 +00:00
|
|
|
email.SendVerificationMail(params.Email, token)
|
2021-07-18 07:26:17 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|