-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.go
63 lines (49 loc) · 1.72 KB
/
signup.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package views
import (
"fmt"
"net/http"
"github.com/gorilla/schema"
"github.com/labstack/echo/v4"
"github.com/ystv/web-auth/templates"
"github.com/ystv/web-auth/user"
)
var decoder = schema.NewDecoder()
// UserSignup represents the HTML form
type UserSignup struct {
Firstname string `db:"first_name" schema:"firstname" validate:"required,gte=3"`
Lastname string `db:"last_name" schema:"lastname" validate:"required,gte=3"`
Email string `db:"email" schema:"email" validate:"required,email"`
Password string `db:"password" schema:"password" validate:"required,gte=8"`
ConfirmPassword string `schema:"confirmpassword" validate:"required,eqfield=Password,gte=8"`
}
// SignUpFunc will enable new users to sign up to our service
func (v *Views) SignUpFunc(c echo.Context) error {
switch c.Request().Method {
case http.MethodPost:
uSignup := UserSignup{}
err := decoder.Decode(&uSignup, c.Request().PostForm)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
fmt.Errorf("failed to get form values for signup: %w", err))
}
uSignup.Email += "@york.ac.uk"
err = v.validate.Struct(uSignup)
if err != nil {
return fmt.Errorf("failed to parse form: %w", err)
}
uNormal := user.User{
Email: uSignup.Email,
}
_, err = v.user.GetUser(c.Request().Context(), uNormal)
if err == nil {
return v.template.RenderTemplate(c.Response(), "Account already exists", templates.SignupTemplate,
templates.NoNavType)
}
return c.Redirect(http.StatusFound, "/")
case http.MethodGet:
return v.template.RenderTemplate(c.Response(), "", templates.SignupTemplate, templates.NoNavType)
}
return v.invalidMethodUsed(c)
}
//nolint:godox
// TODO: Implement signup holding page