@@ -17,20 +17,15 @@ import (
17
17
"github.com/mikestefanello/pagoda/pkg/redirect"
18
18
"github.com/mikestefanello/pagoda/pkg/routenames"
19
19
"github.com/mikestefanello/pagoda/pkg/services"
20
+ "github.com/mikestefanello/pagoda/pkg/ui"
20
21
"github.com/mikestefanello/pagoda/templates"
21
22
)
22
23
23
24
const (
24
- routeNameForgotPassword = "forgot_password"
25
- routeNameForgotPasswordSubmit = "forgot_password.submit"
26
- routeNameLogin = "login"
27
- routeNameLoginSubmit = "login.submit"
28
- routeNameLogout = "logout"
29
- routeNameRegister = "register"
30
- routeNameRegisterSubmit = "register.submit"
31
- routeNameResetPassword = "reset_password"
32
- routeNameResetPasswordSubmit = "reset_password.submit"
33
- routeNameVerifyEmail = "verify_email"
25
+ routeNameLogout = "logout"
26
+ routeNameResetPassword = "reset_password"
27
+ routeNameResetPasswordSubmit = "reset_password.submit"
28
+ routeNameVerifyEmail = "verify_email"
34
29
)
35
30
36
31
type (
@@ -41,25 +36,6 @@ type (
41
36
* services.TemplateRenderer
42
37
}
43
38
44
- forgotPasswordForm struct {
45
- Email string `form:"email" validate:"required,email"`
46
- form.Submission
47
- }
48
-
49
- loginForm struct {
50
- Email string `form:"email" validate:"required,email"`
51
- Password string `form:"password" validate:"required"`
52
- form.Submission
53
- }
54
-
55
- registerForm struct {
56
- Name string `form:"name" validate:"required"`
57
- Email string `form:"email" validate:"required,email"`
58
- Password string `form:"password" validate:"required"`
59
- ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
60
- form.Submission
61
- }
62
-
63
39
resetPasswordForm struct {
64
40
Password string `form:"password" validate:"required"`
65
41
ConfirmPassword string `form:"password-confirm" validate:"required,eqfield=Password"`
@@ -84,12 +60,12 @@ func (h *Auth) Routes(g *echo.Group) {
84
60
g .GET ("/email/verify/:token" , h .VerifyEmail ).Name = routeNameVerifyEmail
85
61
86
62
noAuth := g .Group ("/user" , middleware .RequireNoAuthentication ())
87
- noAuth .GET ("/login" , h .LoginPage ).Name = routeNameLogin
88
- noAuth .POST ("/login" , h .LoginSubmit ).Name = routeNameLoginSubmit
89
- noAuth .GET ("/register" , h .RegisterPage ).Name = routeNameRegister
90
- noAuth .POST ("/register" , h .RegisterSubmit ).Name = routeNameRegisterSubmit
91
- noAuth .GET ("/password" , h .ForgotPasswordPage ).Name = routeNameForgotPassword
92
- noAuth .POST ("/password" , h .ForgotPasswordSubmit ).Name = routeNameForgotPasswordSubmit
63
+ noAuth .GET ("/login" , h .LoginPage ).Name = routenames . Login
64
+ noAuth .POST ("/login" , h .LoginSubmit ).Name = routenames . LoginSubmit
65
+ noAuth .GET ("/register" , h .RegisterPage ).Name = routenames . Register
66
+ noAuth .POST ("/register" , h .RegisterSubmit ).Name = routenames . RegisterSubmit
67
+ noAuth .GET ("/password" , h .ForgotPasswordPage ).Name = routenames . ForgotPassword
68
+ noAuth .POST ("/password" , h .ForgotPasswordSubmit ).Name = routenames . ForgotPasswordSubmit
93
69
94
70
resetGroup := noAuth .Group ("/password/reset" ,
95
71
middleware .LoadUser (h .orm ),
@@ -100,17 +76,11 @@ func (h *Auth) Routes(g *echo.Group) {
100
76
}
101
77
102
78
func (h * Auth ) ForgotPasswordPage (ctx echo.Context ) error {
103
- p := page .New (ctx )
104
- p .Layout = templates .LayoutAuth
105
- p .Name = templates .PageForgotPassword
106
- p .Title = "Forgot password"
107
- p .Form = form.Get [forgotPasswordForm ](ctx )
108
-
109
- return h .RenderPage (ctx , p )
79
+ return ui .ForgotPassword (ctx , form.Get [ui.ForgotPasswordForm ](ctx ))
110
80
}
111
81
112
82
func (h * Auth ) ForgotPasswordSubmit (ctx echo.Context ) error {
113
- var input forgotPasswordForm
83
+ var input ui. ForgotPasswordForm
114
84
115
85
succeed := func () error {
116
86
form .Clear (ctx )
@@ -169,17 +139,11 @@ func (h *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
169
139
}
170
140
171
141
func (h * Auth ) LoginPage (ctx echo.Context ) error {
172
- p := page .New (ctx )
173
- p .Layout = templates .LayoutAuth
174
- p .Name = templates .PageLogin
175
- p .Title = "Log in"
176
- p .Form = form.Get [loginForm ](ctx )
177
-
178
- return h .RenderPage (ctx , p )
142
+ return ui .Login (ctx , form.Get [ui.LoginForm ](ctx ))
179
143
}
180
144
181
145
func (h * Auth ) LoginSubmit (ctx echo.Context ) error {
182
- var input loginForm
146
+ var input ui. LoginForm
183
147
184
148
authFailed := func () error {
185
149
input .SetFieldError ("Email" , "" )
@@ -224,7 +188,7 @@ func (h *Auth) LoginSubmit(ctx echo.Context) error {
224
188
return fail (err , "unable to log in user" )
225
189
}
226
190
227
- msg .Success (ctx , fmt .Sprintf ("Welcome back, <strong>%s</strong> . You are now logged in." , u .Name ))
191
+ msg .Success (ctx , fmt .Sprintf ("Welcome back, %s . You are now logged in." , u .Name ))
228
192
229
193
return redirect .New (ctx ).
230
194
Route (routenames .Home ).
@@ -243,17 +207,11 @@ func (h *Auth) Logout(ctx echo.Context) error {
243
207
}
244
208
245
209
func (h * Auth ) RegisterPage (ctx echo.Context ) error {
246
- p := page .New (ctx )
247
- p .Layout = templates .LayoutAuth
248
- p .Name = templates .PageRegister
249
- p .Title = "Register"
250
- p .Form = form.Get [registerForm ](ctx )
251
-
252
- return h .RenderPage (ctx , p )
210
+ return ui .Register (ctx , form.Get [ui.RegisterForm ](ctx ))
253
211
}
254
212
255
213
func (h * Auth ) RegisterSubmit (ctx echo.Context ) error {
256
- var input registerForm
214
+ var input ui. RegisterForm
257
215
258
216
err := form .Submit (ctx , & input )
259
217
@@ -288,7 +246,7 @@ func (h *Auth) RegisterSubmit(ctx echo.Context) error {
288
246
case * ent.ConstraintError :
289
247
msg .Warning (ctx , "A user with this email address already exists. Please log in." )
290
248
return redirect .New (ctx ).
291
- Route (routeNameLogin ).
249
+ Route (routenames . Login ).
292
250
Go ()
293
251
default :
294
252
return fail (err , "unable to create user" )
@@ -303,7 +261,7 @@ func (h *Auth) RegisterSubmit(ctx echo.Context) error {
303
261
)
304
262
msg .Info (ctx , "Your account has been created." )
305
263
return redirect .New (ctx ).
306
- Route (routeNameLogin ).
264
+ Route (routenames . Login ).
307
265
Go ()
308
266
}
309
267
@@ -398,7 +356,7 @@ func (h *Auth) ResetPasswordSubmit(ctx echo.Context) error {
398
356
399
357
msg .Success (ctx , "Your password has been updated." )
400
358
return redirect .New (ctx ).
401
- Route (routeNameLogin ).
359
+ Route (routenames . Login ).
402
360
Go ()
403
361
}
404
362
0 commit comments