feat(app): signup verification

master
Xeronith 2022-09-29 16:58:52 +03:30
rodzic 63b297d93d
commit 88eeb64438
4 zmienionych plików z 48 dodań i 0 usunięć

Wyświetl plik

@ -10,6 +10,11 @@ type SignupDTO struct {
Username string `json:"username" validate:"username"` Username string `json:"username" validate:"username"`
} }
type VerificationDTO struct {
Email string `json:"email" validate:"required,email"`
Code string `json:"code"`
}
type ProfileDTO struct { type ProfileDTO struct {
DisplayName string `json:"display_name"` DisplayName string `json:"display_name"`
Bio string `json:"bio"` Bio string `json:"bio"`

Wyświetl plik

@ -9,6 +9,7 @@ var All = []contracts.IRoute{
Upload, Upload,
Profile, Profile,
Signup, Signup,
Verify,
Login, Login,
GetProfile, GetProfile,
UpdateProfile, UpdateProfile,

Wyświetl plik

@ -39,6 +39,41 @@ var Signup = route.New(HttpPost, "/api/v1/signup", func(x IContext) error {
PublicKey: publicKey, 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 { if err := repos.CreateUser(user); err != nil {
return x.Conflict(err) return x.Conflict(err)
} }

Wyświetl plik

@ -1,6 +1,8 @@
package utility package utility
import ( import (
"fmt"
"math/rand"
"sync" "sync"
"time" "time"
) )
@ -27,3 +29,8 @@ func UniqueId() int64 {
lastId = id lastId = id
return id return id
} }
func GenerateConfirmationCode() string {
rand.Seed(time.Now().UnixNano())
return fmt.Sprintf("%d", 100000+rand.Intn(899999))
}