Add an /api/authcheck and use it to toggle logout/login link on top header.

This commit is contained in:
Zed A. Shaw 2025-09-17 11:33:22 -04:00
parent c2123ff741
commit fcb71add03
3 changed files with 15 additions and 2 deletions

View file

@ -15,6 +15,12 @@ import (
. "MY/webapp/common"
)
func GetApiAuthCheck(c *fiber.Ctx) error {
_, err := CheckAuthed(c, false)
// auth failure or not authed is determined by err, with nil meaning YES AUTHED
return c.JSON(fiber.Map{"is_authed": err == nil})
}
func GetApiLogout(c *fiber.Ctx) error {
err := LogoutUser(c)
if err != nil { return IfErrNil(err, c) }

View file

@ -97,6 +97,7 @@ func Setup(app *fiber.App) {
app.Post("/api/link", PostApiLink)
// api/auth.go
app.Get("/api/authcheck", GetApiAuthCheck)
app.Get("/api/logout", GetApiLogout)
app.Post("/api/register", PostApiRegister)
app.Post("/api/login", PostApiLogin)

View file

@ -16,7 +16,8 @@
<title>ZedShaw.games</title>
</head>
<body id="top" data-testid="{{.PageId}}">
<header>
<header x-data="{auth: false}"
x-init="auth = await GetJson('/api/authcheck')">
<nav>
<a id="home" href="/">
<svg xmlns="http://www.w3.org/2000/svg"
@ -30,7 +31,12 @@
<a id="stream" href="/stream/">Streams</a>
<a id="game" href="/game/">Games</a>
<a id="register" href="/register/">Register</a>
<template x-if="auth.is_authed">
<a id="logout" href="/api/logout">Logout</a>
</template>
<template x-if="!auth.is_authed">
<a id="login" href="/login/">Login</a>
</template>
</nav>
</header>