logout and auth url

This commit is contained in:
Corban-Lee Jones 2025-01-29 18:20:28 +00:00
parent 3bbfed025d
commit d276281ce9
5 changed files with 14 additions and 6 deletions

View File

@ -41,7 +41,7 @@ app.use(flash());
app.use(flashMiddleware);
// register routers & middleware
app.use("/login", authRouter);
app.use("/auth", authRouter);
app.use("/guild", ensureAuthenticated, attachUser, attachGuilds, guildRouter);
app.use("/", ensureAuthenticated, attachUser, attachGuilds, homeRouter);

View File

@ -17,7 +17,7 @@
</div>
<div class="mt-5">
<a href="/login/api" class="w-full py-3 px-4 inline-flex justify-center items-center gap-x-2 text-sm font-medium rounded-lg border border-gray-200 bg-white text-gray-800 shadow-sm hover:bg-gray-50 focus:outline-none focus:bg-gray-50 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-white dark:hover:bg-neutral-800 dark:focus:bg-neutral-800">
<a href="/auth/login/api" class="w-full py-3 px-4 inline-flex justify-center items-center gap-x-2 text-sm font-medium rounded-lg border border-gray-200 bg-white text-gray-800 shadow-sm hover:bg-gray-50 focus:outline-none focus:bg-gray-50 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-white dark:hover:bg-neutral-800 dark:focus:bg-neutral-800">
<svg class="w-4 h-auto" xmlns="http://www.w3.org/2000/svg" width="46" height="46" fill="currentColor" class="bi bi-discord" viewBox="0 0 16 16">
<path d="M13.545 2.907a13.2 13.2 0 0 0-3.257-1.011.05.05 0 0 0-.052.025c-.141.25-.297.577-.406.833a12.2 12.2 0 0 0-3.658 0 8 8 0 0 0-.412-.833.05.05 0 0 0-.052-.025c-1.125.194-2.22.534-3.257 1.011a.04.04 0 0 0-.021.018C.356 6.024-.213 9.047.066 12.032q.003.022.021.037a13.3 13.3 0 0 0 3.995 2.02.05.05 0 0 0 .056-.019q.463-.63.818-1.329a.05.05 0 0 0-.01-.059l-.018-.011a9 9 0 0 1-1.248-.595.05.05 0 0 1-.02-.066l.015-.019q.127-.095.248-.195a.05.05 0 0 1 .051-.007c2.619 1.196 5.454 1.196 8.041 0a.05.05 0 0 1 .053.007q.121.1.248.195a.05.05 0 0 1-.004.085 8 8 0 0 1-1.249.594.05.05 0 0 0-.03.03.05.05 0 0 0 .003.041c.24.465.515.909.817 1.329a.05.05 0 0 0 .056.019 13.2 13.2 0 0 0 4.001-2.02.05.05 0 0 0 .021-.037c.334-3.451-.559-6.449-2.366-9.106a.03.03 0 0 0-.02-.019m-8.198 7.307c-.789 0-1.438-.724-1.438-1.612s.637-1.613 1.438-1.613c.807 0 1.45.73 1.438 1.613 0 .888-.637 1.612-1.438 1.612m5.316 0c-.788 0-1.438-.724-1.438-1.612s.637-1.613 1.438-1.613c.807 0 1.451.73 1.438 1.613 0 .888-.631 1.612-1.438 1.612"/>
</svg>

View File

@ -11,11 +11,18 @@ export const get = async (_request: Request, response: Response) => {
export const authenticate = async (request: Request, response: Response, next: NextFunction) => {
passport.authenticate("discord", {
successRedirect: "/",
failureRedirect: "/login",
failureRedirect: "/auth/login",
failureFlash: true
})(request, response, next);
};
export const logout = async (request: Request, response: Response, next: NextFunction) => {
request.logout(error => {
if (error) { return next(); }
response.redirect("/");
});
};
export const setupPassport = (passport: PassportStatic) => {
const scopes: Array<string> = ["identify"];
const authorisedUserIds: Array<string> = process.env.DISCORD_USER_IDS?.split("/") || [];

View File

@ -5,7 +5,7 @@ export const ensureAuthenticated = (request: Request, response: Response, next:
return next();
}
response.redirect("/login");
response.redirect("/auth/login");
};
export const forwardAuthenticated = (request: Request, response: Response, next: NextFunction) => {

View File

@ -1,8 +1,9 @@
import { Router } from "express";
import { forwardAuthenticated } from "@server/middleware/authenticated";
import { ensureAuthenticated, forwardAuthenticated } from "@server/middleware/authenticated";
import * as controller from "@server/controllers/auth";
export const router = Router();
router.get("/", forwardAuthenticated, controller.get);
router.get("/login", forwardAuthenticated, controller.get);
router.get("/logout", ensureAuthenticated, controller.logout);
router.get("/api", forwardAuthenticated, controller.authenticate);