mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
feat: migrate nextjs to rr7
This commit is contained in:
30
packages/auth/server/lib/utils/authorizer.ts
Normal file
30
packages/auth/server/lib/utils/authorizer.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import type { Context } from 'hono';
|
||||
|
||||
import type { HonoAuthContext } from '../../types/context';
|
||||
import { createSession, generateSessionToken } from '../session/session';
|
||||
import { setSessionCookie } from '../session/session-cookies';
|
||||
|
||||
type AuthorizeUser = {
|
||||
userId: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles creating a session.
|
||||
*/
|
||||
export const onAuthorize = async (user: AuthorizeUser, c: Context<HonoAuthContext>) => {
|
||||
const metadata = c.get('requestMetadata');
|
||||
|
||||
const sessionToken = generateSessionToken();
|
||||
|
||||
await createSession(sessionToken, user.userId, metadata);
|
||||
|
||||
await setSessionCookie(c, sessionToken);
|
||||
|
||||
// Todo.
|
||||
// Create the Stripe customer and attach it to the user if it doesn't exist.
|
||||
// if (user.customerId === null && IS_BILLING_ENABLED()) {
|
||||
// await getStripeCustomerByUser(user).catch((err) => {
|
||||
// console.error(err);
|
||||
// });
|
||||
// }
|
||||
};
|
||||
54
packages/auth/server/lib/utils/get-session.ts
Normal file
54
packages/auth/server/lib/utils/get-session.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import type { Context } from 'hono';
|
||||
|
||||
import { AppError } from '@documenso/lib/errors/app-error';
|
||||
|
||||
import { AuthenticationErrorCode } from '../errors/error-codes';
|
||||
import type { SessionValidationResult } from '../session/session';
|
||||
import { validateSessionToken } from '../session/session';
|
||||
import { getSessionCookie } from '../session/session-cookies';
|
||||
|
||||
export const getSession = async (c: Context | Request): Promise<SessionValidationResult> => {
|
||||
// Todo: Make better
|
||||
const sessionId = await getSessionCookie(mapRequestToContextForCookie(c));
|
||||
|
||||
if (!sessionId) {
|
||||
return {
|
||||
isAuthenticated: false,
|
||||
session: null,
|
||||
user: null,
|
||||
};
|
||||
}
|
||||
|
||||
return await validateSessionToken(sessionId);
|
||||
};
|
||||
|
||||
export const getRequiredSession = async (c: Context | Request) => {
|
||||
const { session, user } = await getSession(mapRequestToContextForCookie(c));
|
||||
|
||||
if (session && user) {
|
||||
return { session, user };
|
||||
}
|
||||
|
||||
// Todo: Test if throwing errors work
|
||||
if (c instanceof Request) {
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
throw new AppError(AuthenticationErrorCode.Unauthorized);
|
||||
};
|
||||
|
||||
const mapRequestToContextForCookie = (c: Context | Request) => {
|
||||
if (c instanceof Request) {
|
||||
// c.req.raw.headers.
|
||||
const partialContext = {
|
||||
req: {
|
||||
raw: c,
|
||||
},
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
return partialContext as unknown as Context;
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
28
packages/auth/server/lib/utils/redirect.ts
Normal file
28
packages/auth/server/lib/utils/redirect.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
|
||||
|
||||
/**
|
||||
* Handle an optional redirect path.
|
||||
*/
|
||||
export const handleRequestRedirect = (redirectUrl?: string) => {
|
||||
if (!redirectUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = new URL(redirectUrl, NEXT_PUBLIC_WEBAPP_URL());
|
||||
|
||||
if (url.origin !== NEXT_PUBLIC_WEBAPP_URL()) {
|
||||
window.location.href = '/documents';
|
||||
} else {
|
||||
window.location.href = redirectUrl;
|
||||
}
|
||||
};
|
||||
|
||||
export const handleSignInRedirect = (redirectUrl: string = '/documents') => {
|
||||
const url = new URL(redirectUrl, NEXT_PUBLIC_WEBAPP_URL());
|
||||
|
||||
if (url.origin !== NEXT_PUBLIC_WEBAPP_URL()) {
|
||||
window.location.href = '/documents';
|
||||
} else {
|
||||
window.location.href = redirectUrl;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user