From 37f2bab9c068b84b74e92f9814945a5432b4f0d4 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Wed, 26 Jan 2022 11:07:45 +0800 Subject: [PATCH] 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 --- .../core/src/queries/sign-in-experience.ts | 25 +++++++++++ packages/core/src/routes/init.ts | 2 + .../core/src/routes/sign-in-experience.ts | 44 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 packages/core/src/queries/sign-in-experience.ts create mode 100644 packages/core/src/routes/sign-in-experience.ts diff --git a/packages/core/src/queries/sign-in-experience.ts b/packages/core/src/queries/sign-in-experience.ts new file mode 100644 index 000000000..362812670 --- /dev/null +++ b/packages/core/src/queries/sign-in-experience.ts @@ -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( + pool, + SignInExperiences, + true +); + +export const updateSignInExperienceById = async ( + id: string, + set: Partial +) => updateSignInExperience({ set, where: { id } }); + +export const findDefaultSignInExperience = async () => + pool.one(sql` + select ${sql.join(Object.values(fields), sql`, `)} + from ${table} + `); diff --git a/packages/core/src/routes/init.ts b/packages/core/src/routes/init.ts index f3b22cc5a..56bf38e1d 100644 --- a/packages/core/src/routes/init.ts +++ b/packages/core/src/routes/init.ts @@ -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]; }; diff --git a/packages/core/src/routes/sign-in-experience.ts b/packages/core/src/routes/sign-in-experience.ts new file mode 100644 index 000000000..90468ba1b --- /dev/null +++ b/packages/core/src/routes/sign-in-experience.ts @@ -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(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(); + } + ); +}