Files
sign/apps/remix/server/utils/get-loader-session.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-02-07 00:58:50 +11:00
import { getContext } from 'hono/context-storage';
2025-01-31 23:17:50 +11:00
import { redirect } from 'react-router';
2025-02-07 00:58:50 +11:00
import type { HonoEnv } from 'server';
2025-02-07 16:33:30 +11:00
import type { AppContext } from 'server/context';
2025-02-07 00:58:50 +11:00
import type { AppSession } from '@documenso/lib/client-only/providers/session';
2025-01-31 23:17:50 +11:00
2025-02-07 16:33:30 +11:00
/**
* Get the full context passed to the loader.
*
* @returns The full app context.
*/
export const getOptionalLoaderContext = (): AppContext => {
const { context } = getContext<HonoEnv>().var;
return context;
};
/**
* Returns the session extracted from the app context.
*
* @returns The session, or null if not authenticated.
*/
export const getOptionalLoaderSession = (): AppSession | null => {
const { context } = getContext<HonoEnv>().var;
return context.session;
};
2025-01-31 23:17:50 +11:00
/**
* Returns the session context or throws a redirect to signin if it is not present.
*/
2025-02-07 00:58:50 +11:00
export const getLoaderSession = (): AppSession => {
const session = getOptionalLoaderSession();
if (!session) {
2025-01-31 23:17:50 +11:00
throw redirect('/signin'); // Todo: Maybe add a redirect cookie to come back?
}
2025-02-07 00:58:50 +11:00
return session;
};
2025-01-31 23:17:50 +11:00
/**
* Returns the team session context or throws a redirect to signin if it is not present.
*/
2025-02-07 00:58:50 +11:00
export const getLoaderTeamSession = () => {
const session = getOptionalLoaderSession();
if (!session) {
2025-01-31 23:17:50 +11:00
throw redirect('/signin'); // Todo: Maybe add a redirect cookie to come back?
}
2025-02-07 00:58:50 +11:00
if (!session.currentTeam) {
2025-01-31 23:17:50 +11:00
throw new Response(null, { status: 404 }); // Todo: Test that 404 page shows up.
}
return {
2025-02-07 00:58:50 +11:00
...session,
currentTeam: session.currentTeam,
2025-01-31 23:17:50 +11:00
};
};