Skip to content

Commit

Permalink
feat: add logout
Browse files Browse the repository at this point in the history
  • Loading branch information
nevivurn committed Jun 10, 2024
1 parent 320858e commit f90070b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
11 changes: 11 additions & 0 deletions controller/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/labstack/echo/v4"

"github.com/bacchus-snu/sgs/pkg/auth"
"github.com/bacchus-snu/sgs/view"
)

// middlewareAuth adds the user to the context if they are authenticated.
Expand Down Expand Up @@ -104,3 +105,13 @@ func handleAuthCallback(
return c.Redirect(http.StatusSeeOther, c.Echo().Reverse("auth"))
}
}

func handleAuthLogout() echo.HandlerFunc {
return func(c echo.Context) error {
sess, _ := session.Get("session", c)
delete(sess.Values, "user")
sess.Save(c.Request(), c.Response())

return c.Render(http.StatusOK, "", view.PageLogout())
}
}
2 changes: 1 addition & 1 deletion controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func AddRoutes(
middleware.Secure(),
middleware.Gzip(),

// TODO: session
session.Middleware(stor),
middlewareAuth(),
)
Expand All @@ -116,6 +115,7 @@ func AddRoutes(

e.GET("/auth", handleAuth(authSvc)).Name = "auth"
e.GET("/auth/callback", handleAuthCallback(authSvc))
e.GET("/auth/logout", handleAuthLogout())

requireAuth := middlewareAuthenticated()

Expand Down
10 changes: 10 additions & 0 deletions view/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ templ page(title string) {
<a class={ classButtonBase, "text-lg", "ml-auto", "hover:text-gray-500" } href="https://sgs-docs.snucse.org">
Docs
</a>
<a class={ classButtonBase, "text-lg", "ml-auto", "hover:text-gray-500" } href="/auth/logout">
Log out
</a>
<a class={ classButtonPrimary, "ml-4" } href="/request">
Workspace request
</a>
Expand All @@ -38,3 +41,10 @@ templ page(title string) {
</footer>
</html>
}

templ PageLogout() {
@page("Log out") {
<h1 class="mb-4 text-xl font-bold">Log out</h1>
<p>You have been logged out successfully.</p>
}
}
63 changes: 62 additions & 1 deletion view/view_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f90070b

Please sign in to comment.