kopia lustrzana https://github.com/reiver/greatape
feat(app): ✨ signup verification
rodzic
63b297d93d
commit
88eeb64438
|
@ -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"`
|
||||||
|
|
|
@ -9,6 +9,7 @@ var All = []contracts.IRoute{
|
||||||
Upload,
|
Upload,
|
||||||
Profile,
|
Profile,
|
||||||
Signup,
|
Signup,
|
||||||
|
Verify,
|
||||||
Login,
|
Login,
|
||||||
GetProfile,
|
GetProfile,
|
||||||
UpdateProfile,
|
UpdateProfile,
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue