forked from ungerik/go-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
132 lines (124 loc) · 3.86 KB
/
views.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package user
import (
"errors"
"github.com/ungerik/go-start/view"
)
// The confirmation code will be passed in the GET parameter "code"
func EmailConfirmationView(profileURL view.URL) view.View {
return view.DynamicView(
func(ctx *view.Context) (view.View, error) {
confirmationCode, ok := ctx.Request.Params["code"]
if !ok {
return view.DIV("error", view.HTML("Invalid email confirmation code!")), nil
}
doc, email, confirmed, err := ConfirmEmail(confirmationCode)
if !confirmed {
return view.DIV("error", view.HTML("Invalid email confirmation code!")), err
}
Login(ctx.Session, doc)
return view.Views{
view.DIV("success", view.Printf("Email address %s confirmed!", email)),
&view.If{
Condition: profileURL != nil,
Content: view.P(
view.HTML("Continue to your "),
view.A(profileURL, "profile..."),
),
},
}, nil
},
)
}
func NewLoginForm(buttonText, class, errorMessageClass, successMessageClass string, redirectURL view.URL) view.View {
return view.DynamicView(
func(ctx *view.Context) (v view.View, err error) {
if from, ok := ctx.Request.Params["from"]; ok {
redirectURL = view.StringURL(from)
}
model := &LoginFormModel{}
if email, ok := ctx.Request.Params["email"]; ok {
model.Email.Set(email)
}
form := &view.Form{
Class: class,
ErrorMessageClass: errorMessageClass,
SuccessMessageClass: successMessageClass,
SuccessMessage: "Login successful",
SubmitButtonText: buttonText,
FormID: "gostart_user_login",
GetModel: view.FormModel(model),
Redirect: redirectURL,
OnSubmit: func(form *view.Form, formModel interface{}, ctx *view.Context) (string, view.URL, error) {
m := formModel.(*LoginFormModel)
ok, err := LoginEmailPassword(ctx.Session, m.Email.Get(), m.Password.Get())
if err != nil {
if view.Config.Debug.Mode {
return "", nil, err
} else {
return "", nil, errors.New("An internal error ocoured")
}
}
if !ok {
return "", nil, errors.New("Wrong email and password combination")
}
return "", nil, nil
},
}
return form, nil
},
)
}
// If redirect is nil, the redirect will go to "/"
func LogoutView(redirect view.URL) view.View {
return view.RenderView(
func(ctx *view.Context) (err error) {
Logout(ctx.Session)
if redirect != nil {
return view.Redirect(redirect.URL(ctx))
}
return view.Redirect("/")
},
)
}
// confirmationPage must have the confirmation code as first URL parameter
func NewSignupForm(buttonText, class, errorMessageClass, successMessageClass string, confirmationURL, redirectURL view.URL) *view.Form {
return &view.Form{
Class: class,
ErrorMessageClass: errorMessageClass,
SuccessMessageClass: successMessageClass,
SuccessMessage: Config.ConfirmationMessage.Sent,
SubmitButtonText: buttonText,
FormID: "gostart_user_signup",
GetModel: func(form *view.Form, ctx *view.Context) (interface{}, error) {
return &EmailPasswordFormModel{}, nil
},
Redirect: redirectURL,
OnSubmit: func(form *view.Form, formModel interface{}, ctx *view.Context) (string, view.URL, error) {
m := formModel.(*EmailPasswordFormModel)
email := m.Email.Get()
password := m.Password1.Get()
var user *User
doc, found, err := FindByEmail(email)
if err != nil {
return "", nil, err
}
if found {
user = From(doc)
if user.EmailPasswordConfirmed() {
return "", nil, errors.New("A user with that email and a password already exists")
}
user.Password.SetHashed(password)
} else {
user, _, err = New(email, password)
if err != nil {
return "", nil, err
}
}
err = <-user.Email[0].SendConfirmationEmail(ctx, confirmationURL)
if err != nil {
return "", nil, err
}
return "", nil, user.Save()
},
}
}