From 17af6e1a5017285b680a7d0a1dace1e1a1612ab9 Mon Sep 17 00:00:00 2001 From: Alexander Kavon Date: Thu, 30 Nov 2023 20:10:41 -0500 Subject: 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 --- src/user/routes.go | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'src/user/routes.go') 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}) } -- cgit v1.2.3