0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -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:
simeng-li 2022-01-26 11:07:45 +08:00 committed by GitHub
parent 871789e763
commit 37f2bab9c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 0 deletions

View 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}
`);

View file

@ -9,6 +9,7 @@ import connectorRoutes from '@/routes/connector';
import resourceRoutes from '@/routes/resource';
import sessionRoutes from '@/routes/session';
import settingRoutes from '@/routes/setting';
import signInExperiencesRoutes from '@/routes/sign-in-experience';
import statusRoutes from '@/routes/status';
import swaggerRoutes from '@/routes/swagger';
import userRoutes from '@/routes/user';
@ -29,6 +30,7 @@ const createRouters = (provider: Provider) => {
settingRoutes(router);
connectorRoutes(router);
resourceRoutes(router);
signInExperiencesRoutes(router);
return [anonymousRouter, router];
};

View 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();
}
);
}