Use middleware for user session
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user