Use middleware for user session

This commit is contained in:
2024-08-28 22:54:58 -04:00
parent 2bb770cd7f
commit e32aa9ca6c
3 changed files with 59 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
userctx "git.kealoha.me/lks/lenslocked/context"
"git.kealoha.me/lks/lenslocked/models"
"git.kealoha.me/lks/lenslocked/templates"
"git.kealoha.me/lks/lenslocked/views"
@@ -89,7 +90,8 @@ func (u Users) PostSignin(w http.ResponseWriter, r *http.Request) {
}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/user", http.StatusFound)
fmt.Fprintf(w, "Current user: %s\n", user.Email)
//http.Redirect(w, r, "/user", http.StatusFound)
}
func (u Users) GetSignout(w http.ResponseWriter, r *http.Request) {
@@ -113,15 +115,8 @@ func (u Users) GetSignout(w http.ResponseWriter, r *http.Request) {
}
func (u Users) CurrentUser(w http.ResponseWriter, r *http.Request) {
seshCookie, err := r.Cookie("session")
if err != nil {
fmt.Println(err)
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
user, err := u.SessionService.User(seshCookie.Value)
if err != nil {
fmt.Println(err)
user := userctx.User(r.Context())
if user == nil {
http.Redirect(w, r, "/signin", http.StatusFound)
return
}