diff options
| author | Alexander Kavon <hawk@alexkavon.com> | 2023-11-30 20:10:41 -0500 |
|---|---|---|
| committer | Alexander Kavon <hawk@alexkavon.com> | 2023-11-30 20:10:50 -0500 |
| commit | 17af6e1a5017285b680a7d0a1dace1e1a1612ab9 (patch) | |
| tree | b33696d408ca10c5b0edd99cad399cc1dd093f52 /src/user/routes.go | |
| parent | b3ea95f3d494c10d3f37de7842982138b4ee19a5 (diff) | |
fixed login/create templates to point to correct endpoints, updated func names to be more idiomatic, reference Session objects to save memory, logout/session.Destroy method
Diffstat (limited to 'src/user/routes.go')
| -rw-r--r-- | src/user/routes.go | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/src/user/routes.go b/src/user/routes.go index d3db728..3bcab06 100644 --- a/src/user/routes.go +++ b/src/user/routes.go @@ -20,28 +20,37 @@ var Routes = server.Routes{ server.Route{ Name: "Store", Method: "POST", - Path: "/user", + Path: "/u", HandlerFunc: Store, + Middlewares: server.NewMiddlewares(sessions.GuestSession), }, server.Route{ Name: "LoginForm", Method: "GET", - Path: "/u/login", + Path: "/u/auth", HandlerFunc: LoginForm, + Middlewares: server.NewMiddlewares(sessions.GuestSession), }, server.Route{ Name: "Authenticate", Method: "POST", Path: "/u/auth", - HandlerFunc: Authenticate, + HandlerFunc: Login, + Middlewares: server.NewMiddlewares(sessions.GuestSession), + }, + server.Route{ + Name: "Logout", + Method: "GET", + Path: "/u/logout", + HandlerFunc: Logout, + Middlewares: server.NewMiddlewares(sessions.AuthSession), }, server.Route{ - Name: "Me", - Method: "GET", - Path: "/u/me", - AuthRequired: true, - HandlerFunc: Show, - Middlewares: server.NewMiddlewares(sessions.AuthSession), + Name: "Me", + Method: "GET", + Path: "/u/me", + HandlerFunc: Show, + Middlewares: server.NewMiddlewares(sessions.AuthSession), }, } @@ -81,7 +90,7 @@ func Store(s *server.Server) http.HandlerFunc { } // Send email validation // Create cookie session - sessions.NewSession(w, map[string]interface{}{"uid": user.Id, "username": user.Username}) + sessions.NewSession(w, sessions.SessionValues{"uid": user.Id, "username": user.Username}) // Redirect to user profile http.Redirect(w, r, "/u/me", http.StatusSeeOther) } @@ -93,13 +102,26 @@ func LoginForm(s *server.Server) http.HandlerFunc { } } -func Authenticate(s *server.Server) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) {} +func Login(s *server.Server) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // look up the user from the db + // hash the form secret + // compare form hash to db hash + // login or dont + } +} + +func Logout(s *server.Server) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + session := r.Context().Value("session").(*sessions.Session) + session.Destroy(w) + http.Redirect(w, r, "/u/auth", http.StatusSeeOther) + } } func Show(s *server.Server) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session := r.Context().Value(sessions.SessionCtxKey("session")).(sessions.Session) + session := r.Context().Value(sessions.SessionCtxKey("session")).(*sessions.Session) username := session.Get("username").(string) s.Ui.Render(w, "user/me", &struct{ Message, Username string }{"Congrats on getting this far!", username}) } |
