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"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-18 07:26:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ResendVerifyEmail(ctx context.Context, params model.ResendVerifyEmailInput) (*model.Response, error) {
|
|
|
|
var res *model.Response
|
|
|
|
params.Email = strings.ToLower(params.Email)
|
|
|
|
|
|
|
|
if !utils.IsValidEmail(params.Email) {
|
|
|
|
return res, fmt.Errorf("invalid email")
|
|
|
|
}
|
|
|
|
|
|
|
|
verificationRequest, err := db.Mgr.GetVerificationByEmail(params.Email)
|
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf(`verification request not found`)
|
|
|
|
}
|
|
|
|
|
2021-08-04 10:25:13 +00:00
|
|
|
token, err := utils.CreateVerificationToken(params.Email, verificationRequest.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
|
|
|
}
|
|
|
|
db.Mgr.AddVerification(db.VerificationRequest{
|
|
|
|
Token: token,
|
|
|
|
Identifier: verificationRequest.Identifier,
|
|
|
|
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.SendVerificationMail(params.Email, token)
|
|
|
|
}()
|
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `Verification email has been sent. Please check your inbox`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|