mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-09 20:12:10 +10:00
better server side signin redirects
this makes it so if a user requests a page (not API route) and isn't signed in, it automatically redirects them to the sign in page (doesn't show a flash of the error page)
This commit is contained in:
24
server/plugins/redirect.ts
Normal file
24
server/plugins/redirect.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { H3Error } from "h3";
|
||||
|
||||
export default defineNitroPlugin((nitro) => {
|
||||
nitro.hooks.hook("error", async (error, { event }) => {
|
||||
if (!event) return;
|
||||
|
||||
// Don't handle for API routes
|
||||
if (event.path.startsWith("/api")) return;
|
||||
|
||||
// Make sure it's a web error
|
||||
if (!(error instanceof H3Error)) return;
|
||||
|
||||
switch (error.statusCode) {
|
||||
case 401:
|
||||
case 403:
|
||||
const userId = await event.context.session.getUserId(event);
|
||||
if (userId) break;
|
||||
return sendRedirect(
|
||||
event,
|
||||
`/signin?redirect=${encodeURIComponent(event.path)}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user