diff --git a/auth/auth.go b/auth/auth.go index 83a6feee..7548ecca 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -125,6 +125,12 @@ func SigninUser(r *http.Request, username string, password string) (*models.User return user, nil } +func SignoutUser(r *http.Request, user *models.User) { + sessionManager.Put(r.Context(), "user_id", "") + sessionManager.Put(r.Context(), "username", "") + log.Info().Str("username", user.Username).Int32("user_id", user.ID).Msg("Ended user session") +} + func SignupUser(ctx context.Context, username string, name string, password string) (*models.User, error) { passwordHash, err := hashPassword(password) if err != nil { diff --git a/sync/routes.go b/sync/routes.go index 8c57a09b..89cf6444 100644 --- a/sync/routes.go +++ b/sync/routes.go @@ -52,6 +52,7 @@ func Router() chi.Router { r.Get("/qr-code/report/{code}", getQRCodeReport) r.Get("/signin", getSignin) r.Post("/signin", postSignin) + r.Method("GET", "/signout", auth.NewEnsureAuth(getSignout)) r.Get("/signup", getSignup) r.Post("/signup", postSignup) r.Get("/sms", getSMS) diff --git a/sync/signin.go b/sync/signin.go index 180b119c..b1f9b63f 100644 --- a/sync/signin.go +++ b/sync/signin.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/Gleipnir-Technology/nidus-sync/auth" + "github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/htmlpage" "github.com/rs/zerolog/log" ) @@ -20,6 +21,11 @@ func getSignin(w http.ResponseWriter, r *http.Request) { signin(w, errorCode) } +func getSignout(w http.ResponseWriter, r *http.Request, user *models.User) { + auth.SignoutUser(r, user) + http.Redirect(w, r, "/signin", http.StatusFound) +} + func getSignup(w http.ResponseWriter, r *http.Request) { signup(w, r.URL.Path) } diff --git a/sync/template/components/header.html b/sync/template/components/header.html index 93e806df..00f43dad 100644 --- a/sync/template/components/header.html +++ b/sync/template/components/header.html @@ -70,7 +70,7 @@ diff --git a/sync/template/signup.html b/sync/template/signup.html index a44e93da..57d2b072 100644 --- a/sync/template/signup.html +++ b/sync/template/signup.html @@ -65,7 +65,7 @@
-

Already have an account? Sign in

+

Already have an account? Sign in