-
Notifications
You must be signed in to change notification settings - Fork 0
/
route-code.go
46 lines (39 loc) · 958 Bytes
/
route-code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"net/http"
"github.com/ivanrave/goreth"
)
var (
errTokenAlreadyExists = apiError{
Code: "TokenAlreadyExists",
Description: "Need to wait while current token will be expired",
}
)
func routeCode(w http.ResponseWriter, r *http.Request) error {
// r.ParseForm() // parse arguments
lgn := r.FormValue("lgn")
// generate a verification code
vcode := generateToken(5)
// validate on db level: login + email or phone number regexp
// save it in Redis
err := goreth.SetLoginAndVcode(lgn, vcode, 90)
if err != nil {
switch (err){
// client error
case goreth.ErrLgnExists:
return errTokenAlreadyExists
case goreth.ErrLgnInvalid:
return apiError{
Code: "BadRequest",
Description: goreth.ErrLgnInvalid.Error(),
}
// server error
default:
return err
}
}
http.Redirect(w, r, "./login.html?vcode=" + vcode, 302)
//json.NewEncoder(w).Encode(tkn)
//fmt.Fprintf(w, tkn)
return nil
}