ch11.6
This commit is contained in:
@@ -219,7 +219,7 @@ func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.serverError(w, err)
|
app.serverError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.sessionManager.Remove(r.Context(), "authenticatedUserId")
|
app.sessionManager.Remove(r.Context(), "authenticatedUserID")
|
||||||
app.sessionManager.Put(r.Context(), "flash", "You've been logged out succefully!")
|
app.sessionManager.Put(r.Context(), "flash", "You've been logged out succefully!")
|
||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
|
|||||||
@@ -40,3 +40,7 @@ func (app *application) decodePostForm(r *http.Request, snippetForm any) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) isAuthenticated(r *http.Request) bool {
|
||||||
|
return app.sessionManager.Exists(r.Context(), "authenticatedUserID")
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,3 +38,14 @@ func (app *application) recoverPanic(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *application) requireAuthentication(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !app.isAuthenticated(r) {
|
||||||
|
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Cache-Control", "no-store")
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,14 +21,17 @@ func (app *application) routes() http.Handler {
|
|||||||
|
|
||||||
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
||||||
router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView))
|
router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView))
|
||||||
router.Handler(http.MethodGet, "/snippet/create", dynamic.ThenFunc(app.snippetCreate))
|
|
||||||
router.Handler(http.MethodPost, "/snippet/create", dynamic.ThenFunc(app.snippetCreatePost))
|
|
||||||
|
|
||||||
router.Handler(http.MethodGet, "/user/signup", dynamic.ThenFunc(app.userSignup))
|
router.Handler(http.MethodGet, "/user/signup", dynamic.ThenFunc(app.userSignup))
|
||||||
router.Handler(http.MethodPost, "/user/signup", dynamic.ThenFunc(app.userSignupPost))
|
router.Handler(http.MethodPost, "/user/signup", dynamic.ThenFunc(app.userSignupPost))
|
||||||
router.Handler(http.MethodGet, "/user/login", dynamic.ThenFunc(app.userLogin))
|
router.Handler(http.MethodGet, "/user/login", dynamic.ThenFunc(app.userLogin))
|
||||||
router.Handler(http.MethodPost, "/user/login", dynamic.ThenFunc(app.userLoginPost))
|
router.Handler(http.MethodPost, "/user/login", dynamic.ThenFunc(app.userLoginPost))
|
||||||
router.Handler(http.MethodPost, "/user/logout", dynamic.ThenFunc(app.userLogoutPost))
|
|
||||||
|
protected := dynamic.Append(app.requireAuthentication)
|
||||||
|
|
||||||
|
router.Handler(http.MethodGet, "/snippet/create", protected.ThenFunc(app.snippetCreate))
|
||||||
|
router.Handler(http.MethodPost, "/snippet/create", protected.ThenFunc(app.snippetCreatePost))
|
||||||
|
router.Handler(http.MethodPost, "/user/logout", protected.ThenFunc(app.userLogoutPost))
|
||||||
|
|
||||||
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
|
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
|
||||||
return standard.Then(router)
|
return standard.Then(router)
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type templateData struct {
|
type templateData struct {
|
||||||
CurrentYear int
|
CurrentYear int
|
||||||
Snippet *models.Snippet
|
Snippet *models.Snippet
|
||||||
Snippets []*models.Snippet
|
Snippets []*models.Snippet
|
||||||
Form any
|
Form any
|
||||||
Flash string
|
Flash string
|
||||||
|
IsAuthenticated bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func humanDate(t time.Time) string {
|
func humanDate(t time.Time) string {
|
||||||
@@ -27,8 +28,9 @@ var functions = template.FuncMap{
|
|||||||
|
|
||||||
func (app *application) newTemplateData(r *http.Request) *templateData {
|
func (app *application) newTemplateData(r *http.Request) *templateData {
|
||||||
return &templateData{
|
return &templateData{
|
||||||
CurrentYear: time.Now().Year(),
|
CurrentYear: time.Now().Year(),
|
||||||
Flash: app.sessionManager.PopString(r.Context(), "flash"),
|
Flash: app.sessionManager.PopString(r.Context(), "flash"),
|
||||||
|
IsAuthenticated: app.isAuthenticated(r),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,19 @@
|
|||||||
<nav>
|
<nav>
|
||||||
<div>
|
<div>
|
||||||
<a href='/'>Home</a>
|
<a href='/'>Home</a>
|
||||||
<a href='/snippet/create'>Create snippet</a>
|
{{if .IsAuthenticated}}
|
||||||
|
<a href='/snippet/create'>Create snippet</a>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<a href='/user/signup'>Signup</a>
|
{{if .IsAuthenticated}}
|
||||||
<a href='/user/login'>Login</a>
|
<form action='/user/logout' method='POST'>
|
||||||
<form action='/user/logout' method='POST'>
|
<button>Logout</button>
|
||||||
<button>Logout</button>
|
</form>
|
||||||
</form>
|
{{else}}
|
||||||
|
<a href='/user/signup'>Signup</a>
|
||||||
|
<a href='/user/login'>Login</a>
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
{{end}}
|
{{end}}
|
||||||
Reference in New Issue
Block a user