feat: add authorization
This commit is contained in:
@@ -6,11 +6,13 @@ describe('validateEnv', () => {
|
||||
validateEnv({
|
||||
DATABASE_URL: 'file:./data.db',
|
||||
FRONTEND_ORIGIN: 'http://localhost:5173/',
|
||||
JWT_SECRET: 'a-secret-that-is-at-least-thirty-two-characters',
|
||||
}),
|
||||
).toEqual({
|
||||
DATABASE_URL: 'file:./data.db',
|
||||
FRONTEND_ORIGIN: 'http://localhost:5173',
|
||||
PORT: 3000,
|
||||
JWT_SECRET: 'a-secret-that-is-at-least-thirty-two-characters',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -32,6 +34,7 @@ describe('validateEnv', () => {
|
||||
validateEnv({
|
||||
DATABASE_URL: 'file:./data.db',
|
||||
FRONTEND_ORIGIN: origin,
|
||||
JWT_SECRET: 'a-secret-that-is-at-least-thirty-two-characters',
|
||||
}),
|
||||
).toThrow('FRONTEND_ORIGIN must be a valid HTTP origin');
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export interface Environment {
|
||||
DATABASE_URL: string;
|
||||
FRONTEND_ORIGIN: string;
|
||||
JWT_SECRET: string;
|
||||
PORT: number;
|
||||
}
|
||||
|
||||
@@ -8,6 +9,7 @@ export function validateEnv(config: Record<string, unknown>): Environment {
|
||||
const databaseUrl = config['DATABASE_URL'];
|
||||
const frontendOrigin = config['FRONTEND_ORIGIN'];
|
||||
const port = config['PORT'] ?? 3000;
|
||||
const jwtSecret = config['JWT_SECRET'];
|
||||
|
||||
if (typeof databaseUrl !== 'string' || !databaseUrl.startsWith('file:')) {
|
||||
throw new Error('DATABASE_URL must be a SQLite file URL');
|
||||
@@ -38,9 +40,14 @@ export function validateEnv(config: Record<string, unknown>): Environment {
|
||||
throw new Error('PORT must be a valid port number');
|
||||
}
|
||||
|
||||
if (typeof jwtSecret !== 'string' || jwtSecret.length < 32) {
|
||||
throw new Error('JWT_SECRET must be at least 32 characters long');
|
||||
}
|
||||
|
||||
return {
|
||||
DATABASE_URL: databaseUrl,
|
||||
FRONTEND_ORIGIN: parsedOrigin.origin,
|
||||
JWT_SECRET: jwtSecret,
|
||||
PORT: parsedPort,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user