mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
feat(core): add sign-in experience query and api (#197)
* feat(core): add sign-in experience query and api add sign-in experience query and api * fix(core): cr update of sign-in-experience apis - remove unused queries - replace hardcoded 'default' id filtering, use pool.one method instead
This commit is contained in:
parent
871789e763
commit
37f2bab9c0
3 changed files with 71 additions and 0 deletions
25
packages/core/src/queries/sign-in-experience.ts
Normal file
25
packages/core/src/queries/sign-in-experience.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import { SignInExperience, CreateSignInExperience, SignInExperiences } from '@logto/schemas';
|
||||||
|
import { sql } from 'slonik';
|
||||||
|
|
||||||
|
import pool from '@/database/pool';
|
||||||
|
import { buildUpdateWhere } from '@/database/update-where';
|
||||||
|
import { convertToIdentifiers } from '@/database/utils';
|
||||||
|
|
||||||
|
const { table, fields } = convertToIdentifiers(SignInExperiences);
|
||||||
|
|
||||||
|
const updateSignInExperience = buildUpdateWhere<CreateSignInExperience, SignInExperience>(
|
||||||
|
pool,
|
||||||
|
SignInExperiences,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
export const updateSignInExperienceById = async (
|
||||||
|
id: string,
|
||||||
|
set: Partial<CreateSignInExperience>
|
||||||
|
) => updateSignInExperience({ set, where: { id } });
|
||||||
|
|
||||||
|
export const findDefaultSignInExperience = async () =>
|
||||||
|
pool.one<SignInExperience>(sql`
|
||||||
|
select ${sql.join(Object.values(fields), sql`, `)}
|
||||||
|
from ${table}
|
||||||
|
`);
|
|
@ -9,6 +9,7 @@ import connectorRoutes from '@/routes/connector';
|
||||||
import resourceRoutes from '@/routes/resource';
|
import resourceRoutes from '@/routes/resource';
|
||||||
import sessionRoutes from '@/routes/session';
|
import sessionRoutes from '@/routes/session';
|
||||||
import settingRoutes from '@/routes/setting';
|
import settingRoutes from '@/routes/setting';
|
||||||
|
import signInExperiencesRoutes from '@/routes/sign-in-experience';
|
||||||
import statusRoutes from '@/routes/status';
|
import statusRoutes from '@/routes/status';
|
||||||
import swaggerRoutes from '@/routes/swagger';
|
import swaggerRoutes from '@/routes/swagger';
|
||||||
import userRoutes from '@/routes/user';
|
import userRoutes from '@/routes/user';
|
||||||
|
@ -29,6 +30,7 @@ const createRouters = (provider: Provider) => {
|
||||||
settingRoutes(router);
|
settingRoutes(router);
|
||||||
connectorRoutes(router);
|
connectorRoutes(router);
|
||||||
resourceRoutes(router);
|
resourceRoutes(router);
|
||||||
|
signInExperiencesRoutes(router);
|
||||||
|
|
||||||
return [anonymousRouter, router];
|
return [anonymousRouter, router];
|
||||||
};
|
};
|
||||||
|
|
44
packages/core/src/routes/sign-in-experience.ts
Normal file
44
packages/core/src/routes/sign-in-experience.ts
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import { SignInExperiences } from '@logto/schemas';
|
||||||
|
import { object, string } from 'zod';
|
||||||
|
|
||||||
|
import koaGuard from '@/middleware/koa-guard';
|
||||||
|
import {
|
||||||
|
findDefaultSignInExperience,
|
||||||
|
updateSignInExperienceById,
|
||||||
|
} from '@/queries/sign-in-experience';
|
||||||
|
|
||||||
|
import { AuthedRouter } from './types';
|
||||||
|
|
||||||
|
export default function signInExperiencesRoutes<T extends AuthedRouter>(router: T) {
|
||||||
|
/**
|
||||||
|
* As we only support single signInExperience settings for V1
|
||||||
|
* always return the default settings in DB for the /sign-in-ex get method
|
||||||
|
*/
|
||||||
|
router.get('/sign-in-ex', async (ctx, next) => {
|
||||||
|
const signInExperience = await findDefaultSignInExperience();
|
||||||
|
ctx.body = signInExperience;
|
||||||
|
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: LOG-1403 need to find a way to validate SignInMethod input
|
||||||
|
router.patch(
|
||||||
|
'/sign-in-ex/:id',
|
||||||
|
koaGuard({
|
||||||
|
params: object({ id: string().min(1) }),
|
||||||
|
body: SignInExperiences.createGuard.omit({ id: true }).partial(),
|
||||||
|
}),
|
||||||
|
async (ctx, next) => {
|
||||||
|
const {
|
||||||
|
params: { id },
|
||||||
|
body,
|
||||||
|
} = ctx.guard;
|
||||||
|
|
||||||
|
ctx.body = await updateSignInExperienceById(id, {
|
||||||
|
...body,
|
||||||
|
});
|
||||||
|
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue