@@ -162,8 +162,9 @@ func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) {
162
162
}
163
163
164
164
type userLoginForm struct {
165
- Email string `form:"email"`
166
- Password string `form:"password"`
165
+ Email string `form:"email"`
166
+ Password string `form:"password"`
167
+
167
168
validator.Validator `form:"-"`
168
169
}
169
170
@@ -217,6 +218,12 @@ func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) {
217
218
218
219
app .sessionManager .Put (r .Context (), "authenticatedUserID" , id )
219
220
221
+ path := app .sessionManager .PopString (r .Context (), "redirectPathAfterLogin" )
222
+ if path != "" {
223
+ http .Redirect (w , r , path , http .StatusSeeOther )
224
+ return
225
+ }
226
+
220
227
http .Redirect (w , r , "/snippet/create" , http .StatusSeeOther )
221
228
}
222
229
@@ -233,3 +240,90 @@ func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
233
240
234
241
http .Redirect (w , r , "/" , http .StatusSeeOther )
235
242
}
243
+
244
+ func (app * application ) about (w http.ResponseWriter , r * http.Request ) {
245
+ data := app .newTemplateData (r )
246
+
247
+ app .render (w , http .StatusOK , "about.html" , data )
248
+ }
249
+
250
+ func (app * application ) accountView (w http.ResponseWriter , r * http.Request ) {
251
+ userID := app .sessionManager .GetInt (r .Context (), "authenticatedUserID" )
252
+
253
+ user , err := app .users .Get (userID )
254
+ if err != nil {
255
+ if errors .Is (err , models .ErrNoRecord ) {
256
+ http .Redirect (w , r , "/user/login" , http .StatusSeeOther )
257
+ } else {
258
+ app .serverError (w , err )
259
+ }
260
+
261
+ return
262
+ }
263
+
264
+ data := app .newTemplateData (r )
265
+ data .User = user
266
+
267
+ app .render (w , http .StatusOK , "account.html" , data )
268
+ }
269
+
270
+ type accountPasswordUpdateForm struct {
271
+ CurrentPassword string `form:"currentPassword"`
272
+ NewPassword string `form:"newPassword"`
273
+ NewPasswordConfirmation string `form:"newPasswordConfirmation"`
274
+
275
+ validator.Validator `form:"-"`
276
+ }
277
+
278
+ func (app * application ) accountPasswordUpdate (w http.ResponseWriter , r * http.Request ) {
279
+ data := app .newTemplateData (r )
280
+ data .Form = accountPasswordUpdateForm {}
281
+
282
+ app .render (w , http .StatusOK , "password.html" , data )
283
+ }
284
+
285
+ func (app * application ) accountPasswordUpdatePost (w http.ResponseWriter , r * http.Request ) {
286
+ var form accountPasswordUpdateForm
287
+
288
+ err := app .decodePostForm (r , & form )
289
+ if err != nil {
290
+ app .clientError (w , http .StatusBadRequest )
291
+ return
292
+ }
293
+
294
+ form .CheckField (validator .NotBlank (form .CurrentPassword ), "currentPassword" , "This field cannot be blank" )
295
+ form .CheckField (validator .NotBlank (form .NewPassword ), "newPassword" , "This field cannot be blank" )
296
+ form .CheckField (validator .MinChars (form .NewPassword , 8 ), "newPassword" , "This field must be at least 8 characters long" )
297
+ form .CheckField (validator .NotBlank (form .NewPasswordConfirmation ), "newPasswordConfirmation" , "This field cannot be blank" )
298
+ form .CheckField (form .NewPassword == form .NewPasswordConfirmation , "newPasswordConfirmation" , "Passwords do not match" )
299
+
300
+ if ! form .Valid () {
301
+ data := app .newTemplateData (r )
302
+ data .Form = form
303
+
304
+ app .render (w , http .StatusUnprocessableEntity , "password.html" , data )
305
+ return
306
+ }
307
+
308
+ userID := app .sessionManager .GetInt (r .Context (), "authenticatedUserID" )
309
+
310
+ err = app .users .PasswordUpdate (userID , form .CurrentPassword , form .NewPassword )
311
+ if err != nil {
312
+ if errors .Is (err , models .ErrInvalidCredentials ) {
313
+ form .AddFieldError ("currentPassword" , "Current password is incorrect" )
314
+
315
+ data := app .newTemplateData (r )
316
+ data .Form = form
317
+
318
+ app .render (w , http .StatusUnprocessableEntity , "password.html" , data )
319
+ } else if err != nil {
320
+ app .serverError (w , err )
321
+ }
322
+
323
+ return
324
+ }
325
+
326
+ app .sessionManager .Put (r .Context (), "flash" , "Your password has been updated successfully!" )
327
+
328
+ http .Redirect (w , r , "/account/view" , http .StatusSeeOther )
329
+ }
0 commit comments