mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
Merge pull request #2885 from logto-io/gao-log-5125-core-library-factory-logto-config
refactor(core): migrate logto config library to factory mode
This commit is contained in:
commit
76bd2bc7d0
3 changed files with 31 additions and 24 deletions
|
@ -5,7 +5,7 @@ import type { PostgreSql } from '@withtyped/postgres';
|
|||
import type { QueryClient } from '@withtyped/server';
|
||||
import type { DatabasePool } from 'slonik';
|
||||
|
||||
import { getOidcConfigs } from '#src/libraries/logto-config.js';
|
||||
import { createLogtoConfigLibrary } from '#src/libraries/logto-config.js';
|
||||
import { appendPath } from '#src/utils/url.js';
|
||||
|
||||
import { checkAlterationState } from './check-alteration-state.js';
|
||||
|
@ -108,7 +108,8 @@ class EnvSet {
|
|||
this.#pool = pool;
|
||||
this.#queryClient = createQueryClient(this.databaseUrl, this.isTest);
|
||||
|
||||
const [, oidcConfigs] = await Promise.all([checkAlterationState(pool), getOidcConfigs(pool)]);
|
||||
const { getOidcConfigs } = createLogtoConfigLibrary(pool);
|
||||
const [, oidcConfigs] = await Promise.all([checkAlterationState(pool), getOidcConfigs()]);
|
||||
this.#oidc = await loadOidcValues(
|
||||
appendPath(this.values.endpoint, '/oidc').toString(),
|
||||
oidcConfigs
|
||||
|
|
|
@ -15,8 +15,10 @@ const { privateKey } = generateKeyPairSync('rsa', {
|
|||
},
|
||||
});
|
||||
|
||||
export const getOidcConfigs = async (): Promise<LogtoOidcConfigType> => ({
|
||||
const getOidcConfigs = async (): Promise<LogtoOidcConfigType> => ({
|
||||
[LogtoOidcConfigKey.PrivateKeys]: [privateKey],
|
||||
[LogtoOidcConfigKey.CookieKeys]: ['LOGTOSEKRIT1'],
|
||||
[LogtoOidcConfigKey.RefreshTokenReuseInterval]: 3,
|
||||
});
|
||||
|
||||
export const createLogtoConfigLibrary = () => ({ getOidcConfigs });
|
||||
|
|
|
@ -5,30 +5,34 @@ import chalk from 'chalk';
|
|||
import type { CommonQueryMethods } from 'slonik';
|
||||
import { z, ZodError } from 'zod';
|
||||
|
||||
export const getOidcConfigs = async (pool: CommonQueryMethods): Promise<LogtoOidcConfigType> => {
|
||||
try {
|
||||
const { rows } = await getRowsByKeys(pool, Object.values(LogtoOidcConfigKey));
|
||||
export const createLogtoConfigLibrary = (pool: CommonQueryMethods) => {
|
||||
const getOidcConfigs = async (): Promise<LogtoOidcConfigType> => {
|
||||
try {
|
||||
const { rows } = await getRowsByKeys(pool, Object.values(LogtoOidcConfigKey));
|
||||
|
||||
return z
|
||||
.object(logtoOidcConfigGuard)
|
||||
.parse(Object.fromEntries(rows.map(({ key, value }) => [key, value])));
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ZodError) {
|
||||
console.error(
|
||||
error.issues
|
||||
.map(({ message, path }) => `${message} at ${chalk.green(path.join('.'))}`)
|
||||
.join('\n')
|
||||
);
|
||||
} else {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
return z
|
||||
.object(logtoOidcConfigGuard)
|
||||
.parse(Object.fromEntries(rows.map(({ key, value }) => [key, value])));
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ZodError) {
|
||||
console.error(
|
||||
error.issues
|
||||
.map(({ message, path }) => `${message} at ${chalk.green(path.join('.'))}`)
|
||||
.join('\n')
|
||||
`\n${chalk.red('[error]')} Failed to get OIDC configs from your Logto database.` +
|
||||
' Did you forget to seed your database?\n\n' +
|
||||
` Use ${chalk.green('npm run cli db seed')} to seed your Logto database;\n` +
|
||||
` Or use ${chalk.green('npm run cli db seed oidc')} to seed OIDC configs alone.\n`
|
||||
);
|
||||
} else {
|
||||
console.error(error);
|
||||
throw new Error('Failed to get configs');
|
||||
}
|
||||
};
|
||||
|
||||
console.error(
|
||||
`\n${chalk.red('[error]')} Failed to get OIDC configs from your Logto database.` +
|
||||
' Did you forget to seed your database?\n\n' +
|
||||
` Use ${chalk.green('npm run cli db seed')} to seed your Logto database;\n` +
|
||||
` Or use ${chalk.green('npm run cli db seed oidc')} to seed OIDC configs alone.\n`
|
||||
);
|
||||
throw new Error('Failed to get configs');
|
||||
}
|
||||
return { getOidcConfigs };
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue