From 88eeb6443858d2f3e142b6fe362211490aac12b3 Mon Sep 17 00:00:00 2001 From: Xeronith Date: Thu, 29 Sep 2022 16:58:52 +0330 Subject: [PATCH] feat(app): :sparkles: signup verification --- greataped/app/models/types/auth.go | 5 +++++ greataped/app/routes/all.go | 1 + greataped/app/routes/signup.go | 35 ++++++++++++++++++++++++++++++ greataped/utility/random.go | 7 ++++++ 4 files changed, 48 insertions(+) diff --git a/greataped/app/models/types/auth.go b/greataped/app/models/types/auth.go index 1d4bc57..c847415 100644 --- a/greataped/app/models/types/auth.go +++ b/greataped/app/models/types/auth.go @@ -10,6 +10,11 @@ type SignupDTO struct { Username string `json:"username" validate:"username"` } +type VerificationDTO struct { + Email string `json:"email" validate:"required,email"` + Code string `json:"code"` +} + type ProfileDTO struct { DisplayName string `json:"display_name"` Bio string `json:"bio"` diff --git a/greataped/app/routes/all.go b/greataped/app/routes/all.go index 5231c0f..a9ecdb3 100644 --- a/greataped/app/routes/all.go +++ b/greataped/app/routes/all.go @@ -9,6 +9,7 @@ var All = []contracts.IRoute{ Upload, Profile, Signup, + Verify, Login, GetProfile, UpdateProfile, diff --git a/greataped/app/routes/signup.go b/greataped/app/routes/signup.go index 9dd23a8..8d72262 100644 --- a/greataped/app/routes/signup.go +++ b/greataped/app/routes/signup.go @@ -39,6 +39,41 @@ var Signup = route.New(HttpPost, "/api/v1/signup", func(x IContext) error { PublicKey: publicKey, } + code := utility.GenerateConfirmationCode() + x.Cache().Put(user.Email, &struct { + user *repos.User + code string + }{ + user: user, + code: code, + }) + + return x.Json(struct{ Code string }{ + Code: code, // TODO: Remove and send with email + }) +}) + +var Verify = route.New(HttpPost, "/api/v1/verify", func(x IContext) error { + body := new(types.VerificationDTO) + if err := x.ParseBodyAndValidate(body); err != nil { + return err + } + + item := x.Cache().Get(body.Email) + if item == nil { + return x.BadRequest("not found") + } + + registration := item.(*struct { + user *repos.User + code string + }) + + if registration.code != body.Code { + return x.Unauthorized("invalid code") + } + + user := registration.user if err := repos.CreateUser(user); err != nil { return x.Conflict(err) } diff --git a/greataped/utility/random.go b/greataped/utility/random.go index 6868059..18be119 100644 --- a/greataped/utility/random.go +++ b/greataped/utility/random.go @@ -1,6 +1,8 @@ package utility import ( + "fmt" + "math/rand" "sync" "time" ) @@ -27,3 +29,8 @@ func UniqueId() int64 { lastId = id return id } + +func GenerateConfirmationCode() string { + rand.Seed(time.Now().UnixNano()) + return fmt.Sprintf("%d", 100000+rand.Intn(899999)) +}