Implement verify email using server url

- fix confirm password typo
This commit is contained in:
Lakhan Samani 2021-07-21 03:34:03 +05:30
parent ae669eaf86
commit 885aaab6ee
8 changed files with 105 additions and 17 deletions

View File

@ -533,7 +533,7 @@ input SignUpInput {
lastName: String lastName: String
email: String! email: String!
password: String! password: String!
cofirmPassword: String! confirmPassword: String!
image: String image: String
} }
@ -3311,11 +3311,11 @@ func (ec *executionContext) unmarshalInputSignUpInput(ctx context.Context, obj i
if err != nil { if err != nil {
return it, err return it, err
} }
case "cofirmPassword": case "confirmPassword":
var err error var err error
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("cofirmPassword")) ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("confirmPassword"))
it.CofirmPassword, err = ec.unmarshalNString2string(ctx, v) it.ConfirmPassword, err = ec.unmarshalNString2string(ctx, v)
if err != nil { if err != nil {
return it, err return it, err
} }

View File

@ -38,12 +38,12 @@ type Response struct {
} }
type SignUpInput struct { type SignUpInput struct {
FirstName *string `json:"firstName"` FirstName *string `json:"firstName"`
LastName *string `json:"lastName"` LastName *string `json:"lastName"`
Email string `json:"email"` Email string `json:"email"`
Password string `json:"password"` Password string `json:"password"`
CofirmPassword string `json:"cofirmPassword"` ConfirmPassword string `json:"confirmPassword"`
Image *string `json:"image"` Image *string `json:"image"`
} }
type UpdateProfileInput struct { type UpdateProfileInput struct {

View File

@ -47,7 +47,7 @@ input SignUpInput {
lastName: String lastName: String
email: String! email: String!
password: String! password: String!
cofirmPassword: String! confirmPassword: String!
image: String image: String
} }

View File

@ -65,7 +65,5 @@ func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResol
// Query returns generated.QueryResolver implementation. // Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} } func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
type ( type mutationResolver struct{ *Resolver }
mutationResolver struct{ *Resolver } type queryResolver struct{ *Resolver }
queryResolver struct{ *Resolver }
)

View File

@ -0,0 +1,69 @@
package handlers
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/yauthdev/yauth/server/constants"
"github.com/yauthdev/yauth/server/db"
"github.com/yauthdev/yauth/server/enum"
"github.com/yauthdev/yauth/server/session"
"github.com/yauthdev/yauth/server/utils"
)
// Defining the Graphql handler
func VerifyEmail() gin.HandlerFunc {
return func(c *gin.Context) {
errorRes := gin.H{
"message": "invalid token",
}
token := c.Query("token")
if token == "" {
c.JSON(400, errorRes)
return
}
_, err := db.Mgr.GetVerificationByToken(token)
if err != nil {
c.JSON(400, errorRes)
return
}
// verify if token exists in db
claim, err := utils.VerifyVerificationToken(token)
if err != nil {
c.JSON(400, errorRes)
return
}
user, err := db.Mgr.GetUserByEmail(claim.Email)
if err != nil {
c.JSON(400, gin.H{
"message": err.Error(),
})
return
}
// update email_verified_at in users table
db.Mgr.UpdateVerificationTime(time.Now().Unix(), user.ID)
// delete from verification table
db.Mgr.DeleteToken(claim.Email)
userIdStr := fmt.Sprintf("%d", user.ID)
refreshToken, _, _ := utils.CreateAuthToken(utils.UserAuthInfo{
ID: userIdStr,
Email: user.Email,
}, enum.RefreshToken)
accessToken, _, _ := utils.CreateAuthToken(utils.UserAuthInfo{
ID: userIdStr,
Email: user.Email,
}, enum.AccessToken)
session.SetToken(userIdStr, refreshToken)
utils.SetCookie(c, accessToken)
c.Redirect(http.StatusTemporaryRedirect, constants.FRONTEND_URL)
}
}

View File

@ -15,7 +15,7 @@ import (
func Signup(ctx context.Context, params model.SignUpInput) (*model.Response, error) { func Signup(ctx context.Context, params model.SignUpInput) (*model.Response, error) {
var res *model.Response var res *model.Response
if params.CofirmPassword != params.Password { if params.ConfirmPassword != params.Password {
return res, fmt.Errorf(`passowrd and confirm password does not match`) return res, fmt.Errorf(`passowrd and confirm password does not match`)
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/yauthdev/yauth/server/enum" "github.com/yauthdev/yauth/server/enum"
@ -17,11 +18,31 @@ func GinContextToContextMiddleware() gin.HandlerFunc {
} }
} }
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
log.Println("-> origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func main() { func main() {
r := gin.Default() r := gin.Default()
r.Use(GinContextToContextMiddleware()) r.Use(GinContextToContextMiddleware())
r.Use(CORSMiddleware())
r.GET("/", handlers.PlaygroundHandler()) r.GET("/", handlers.PlaygroundHandler())
r.POST("/graphql", handlers.GraphqlHandler()) r.POST("/graphql", handlers.GraphqlHandler())
r.GET("/verify_email", handlers.VerifyEmail())
if oauth.OAuthProvider.GoogleConfig != nil { if oauth.OAuthProvider.GoogleConfig != nil {
r.GET("/login/google", handlers.HandleOAuthLogin(enum.GoogleProvider)) r.GET("/login/google", handlers.HandleOAuthLogin(enum.GoogleProvider))
r.GET("/callback/google", handlers.HandleOAuthCallback(enum.GoogleProvider)) r.GET("/callback/google", handlers.HandleOAuthCallback(enum.GoogleProvider))

View File

@ -26,7 +26,7 @@ func SendVerificationMail(toEmail, token string) error {
<a href="%s">Click here to verify</a> <a href="%s">Click here to verify</a>
</body> </body>
</html> </html>
`, constants.FRONTEND_URL+"/"+constants.VERIFY_EMAIL_URI+"?token="+token) `, constants.SERVER_URL+"/verify_email"+"?token="+token)
bodyMessage := sender.WriteHTMLEmail(Receiver, Subject, message) bodyMessage := sender.WriteHTMLEmail(Receiver, Subject, message)
return sender.SendMail(Receiver, Subject, bodyMessage) return sender.SendMail(Receiver, Subject, bodyMessage)