re-implemented JWT auth
This commit is contained in:
parent
c190d368de
commit
b47b0f3752
18 changed files with 390 additions and 174 deletions
74
app/server/config.ts
Normal file
74
app/server/config.ts
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import cors from 'cors';
|
||||
import 'dotenv/config';
|
||||
|
||||
interface AppConfig {
|
||||
readonly isProd: boolean;
|
||||
readonly host: string;
|
||||
readonly port: number;
|
||||
readonly limit: { readonly limit: string };
|
||||
readonly origin: string[];
|
||||
readonly corsOptions: cors.CorsOptions;
|
||||
readonly greeting: string;
|
||||
readonly accessToken: TokenConfig;
|
||||
readonly refreshToken: RefreshTokenConfig;
|
||||
}
|
||||
|
||||
interface TokenConfig {
|
||||
readonly secret: string;
|
||||
readonly expiresIn: number;
|
||||
}
|
||||
|
||||
interface RefreshTokenConfig extends TokenConfig {
|
||||
readonly maxAge: number;
|
||||
}
|
||||
|
||||
export function toAppConfig(): AppConfig {
|
||||
const MODE = process.env.VITE_MODE || 'prod';
|
||||
const isProd = MODE === 'prod';
|
||||
|
||||
const host = process.env.VITE_HOST || 'localhost';
|
||||
const port = parseInt(process.env.VITE_PORT || '3000');
|
||||
|
||||
const urlRemote = `https://${host}:${port}`;
|
||||
const urlLocal = host !== 'localhost' ? `https://localhost:${port}` : undefined;
|
||||
|
||||
const limit = { limit: '50mb' };
|
||||
|
||||
const origin = urlLocal != null ? [urlLocal, urlRemote] : [urlRemote];
|
||||
const corsOptions: cors.CorsOptions = {
|
||||
origin,
|
||||
credentials: true
|
||||
};
|
||||
|
||||
const accessTokenExpiresIn = 24 * 60 * 60;
|
||||
const refreshTokenExpiresIn = 15 * 24 * 60 * 60;
|
||||
|
||||
const accessTokenSecret = process.env.ACCESS_TOKEN_SECRET;
|
||||
const refreshTokenSecret = process.env.REFRESH_TOKEN_SECRET;
|
||||
if (accessTokenSecret == null || refreshTokenSecret == null) throw new Error('No secrets found');
|
||||
|
||||
const accessToken: TokenConfig = {
|
||||
secret: accessTokenSecret,
|
||||
expiresIn: accessTokenExpiresIn
|
||||
};
|
||||
|
||||
const refreshToken: RefreshTokenConfig = {
|
||||
secret: refreshTokenSecret,
|
||||
expiresIn: refreshTokenExpiresIn,
|
||||
maxAge: refreshTokenExpiresIn * 1000
|
||||
};
|
||||
|
||||
const greeting = `\n\n\n\x1b[35m-server is listening at...-\x1b[0m\n\n${urlRemote}\n${urlLocal}\n\n\n`;
|
||||
|
||||
return {
|
||||
isProd,
|
||||
host,
|
||||
port,
|
||||
limit,
|
||||
origin,
|
||||
corsOptions,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
greeting
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue