mirror of
https://github.com/documenso/documenso.git
synced 2025-11-19 19:21:39 +10:00
fix: wip
This commit is contained in:
@ -1,17 +1,43 @@
|
||||
import type { Context } from 'hono';
|
||||
import { getSignedCookie, setSignedCookie } from 'hono/cookie';
|
||||
|
||||
import { authDebugger } from '../utils/debugger';
|
||||
import { appLog } from '@documenso/lib/utils/debugger';
|
||||
import { env } from '@documenso/lib/utils/env';
|
||||
|
||||
export const sessionCookieName = 'sessionId';
|
||||
|
||||
const getAuthSecret = () => {
|
||||
const authSecret = env('NEXTAUTH_SECRET');
|
||||
|
||||
if (!authSecret) {
|
||||
throw new Error('NEXTAUTH_SECRET is not set');
|
||||
}
|
||||
|
||||
return authSecret;
|
||||
};
|
||||
|
||||
export const extractSessionCookieFromHeaders = (headers: Headers): string | null => {
|
||||
const cookieHeader = headers.get('cookie') || '';
|
||||
const cookiePairs = cookieHeader.split(';');
|
||||
const sessionCookie = cookiePairs.find((pair) => pair.trim().startsWith(sessionCookieName));
|
||||
|
||||
if (!sessionCookie) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sessionCookie.split('=')[1].trim();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the session cookie attached to the request headers.
|
||||
*
|
||||
* @param c - The Hono context.
|
||||
* @returns The session ID or null if no session cookie is found.
|
||||
*/
|
||||
export const getSessionCookie = async (c: Context) => {
|
||||
const sessionId = await getSignedCookie(c, 'secret', 'sessionId'); // Todo: Use secret
|
||||
export const getSessionCookie = async (c: Context): Promise<string | null> => {
|
||||
const sessionId = await getSignedCookie(c, getAuthSecret(), sessionCookieName);
|
||||
|
||||
return sessionId;
|
||||
return sessionId || null;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -21,13 +47,13 @@ export const getSessionCookie = async (c: Context) => {
|
||||
* @param sessionToken - The session token to set.
|
||||
*/
|
||||
export const setSessionCookie = async (c: Context, sessionToken: string) => {
|
||||
await setSignedCookie(c, 'sessionId', sessionToken, 'secret', {
|
||||
await setSignedCookie(c, sessionCookieName, sessionToken, getAuthSecret(), {
|
||||
path: '/',
|
||||
// sameSite: '', // whats the default? we need to change this for embed right?
|
||||
// secure: true,
|
||||
domain: 'localhost', // todo
|
||||
}).catch((err) => {
|
||||
authDebugger(`Error setting signed cookie: ${err}`);
|
||||
appLog('SetSessionCookie', `Error setting signed cookie: ${err}`);
|
||||
|
||||
throw err;
|
||||
});
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
import { env } from '@documenso/lib/utils/env';
|
||||
|
||||
// Todo: Delete
|
||||
export const authDebugger = (message: string) => {
|
||||
if (env('NODE_ENV') === 'development') {
|
||||
// console.log(`[DEBUG]: ${message}`);
|
||||
}
|
||||
};
|
||||
@ -6,14 +6,11 @@ import { AuthenticationErrorCode } from '../errors/error-codes';
|
||||
import type { SessionValidationResult } from '../session/session';
|
||||
import { validateSessionToken } from '../session/session';
|
||||
import { getSessionCookie } from '../session/session-cookies';
|
||||
import { authDebugger } from './debugger';
|
||||
|
||||
export const getSession = async (c: Context | Request): Promise<SessionValidationResult> => {
|
||||
// Todo: Make better
|
||||
const sessionId = await getSessionCookie(mapRequestToContextForCookie(c));
|
||||
|
||||
authDebugger(`Session ID: ${sessionId}`);
|
||||
|
||||
if (!sessionId) {
|
||||
return {
|
||||
isAuthenticated: false,
|
||||
|
||||
Reference in New Issue
Block a user