From 7dfefb29baa51d100bfe748bb901d005ae2c6a8a Mon Sep 17 00:00:00 2001 From: prostgles Date: Sun, 22 Dec 2024 12:43:41 +0200 Subject: [PATCH] add email confirm redirect --- docs/server.md | 6 ++- lib/Auth/AuthTypes.ts | 2 +- lib/Auth/authProviders/setEmailProvider.ts | 2 +- .../setConfirmEmailRequestHandler.ts | 39 +++++++++++-------- .../endpoints/setMagicLinkRequestHandler.ts | 5 ++- package-lock.json | 4 +- package.json | 2 +- tests/server/package-lock.json | 2 +- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/docs/server.md b/docs/server.md index 6164b21e..8e56bb32 100644 --- a/docs/server.md +++ b/docs/server.md @@ -196,7 +196,11 @@ prostgles({ Defaults to "session_id" - **getUser** required (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB, client: AuthClientRequest & LoginClientInfo) => Awaitable<AuthResultOrError<...>> - undefined sid is allowed to enable public users + Used in: + - WS AUTHGUARD - allows connected SPA client to check if on protected route and needs to reload to ne redirected to login + - PublishParams - userData and/or sid (in testing) are passed to the publish function + - auth.expressConfig.use - express middleware to get user data and + undefined sid is allowed to enable public users - **expressConfig** optional ExpressConfig Will setup auth routes diff --git a/lib/Auth/AuthTypes.ts b/lib/Auth/AuthTypes.ts index e39e4bce..92b2acd4 100644 --- a/lib/Auth/AuthTypes.ts +++ b/lib/Auth/AuthTypes.ts @@ -146,7 +146,7 @@ export type EmailProvider = confirmationCode: string; clientInfo: LoginClientInfo; req: ExpressReq; - }) => Awaitable; + }) => Awaitable; }; export type AuthProviderUserData = diff --git a/lib/Auth/authProviders/setEmailProvider.ts b/lib/Auth/authProviders/setEmailProvider.ts index 02bc7f90..ffcb37f1 100644 --- a/lib/Auth/authProviders/setEmailProvider.ts +++ b/lib/Auth/authProviders/setEmailProvider.ts @@ -21,6 +21,6 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) { setRegisterRequestHandler({ email, websiteUrl }, app); if (email.signupType === "withPassword") { - setConfirmEmailRequestHandler(email, app); + setConfirmEmailRequestHandler.bind(this)(email, app); } } diff --git a/lib/Auth/endpoints/setConfirmEmailRequestHandler.ts b/lib/Auth/endpoints/setConfirmEmailRequestHandler.ts index bcd0e3dd..4c1204bd 100644 --- a/lib/Auth/endpoints/setConfirmEmailRequestHandler.ts +++ b/lib/Auth/endpoints/setConfirmEmailRequestHandler.ts @@ -1,24 +1,21 @@ import type { Request, Response } from "express"; -import { AuthResponse } from "prostgles-types"; -import { AUTH_ROUTES_AND_PARAMS, HTTP_FAIL_CODES } from "../AuthHandler"; +import { AuthFailure, AuthResponse } from "prostgles-types"; +import { AUTH_ROUTES_AND_PARAMS, AuthHandler, HTTP_FAIL_CODES } from "../AuthHandler"; import { AuthRegistrationConfig } from "../AuthTypes"; import { getClientRequestIPsInfo } from "../utils/getClientRequestIPsInfo"; import e from "express"; -export const setConfirmEmailRequestHandler = ( +export function setConfirmEmailRequestHandler( + this: AuthHandler, emailAuthConfig: Extract< Required>["email"], { signupType: "withPassword" } >, app: e.Express -) => { +) { const requestHandler = async ( req: Request, - res: Response< - | AuthResponse.PasswordRegisterSuccess - | AuthResponse.PasswordRegisterFailure - | AuthResponse.AuthSuccess - > + res: Response ) => { const { id } = req.params; try { @@ -26,12 +23,22 @@ export const setConfirmEmailRequestHandler = ( return res.send({ success: false, code: "something-went-wrong", message: "Invalid code" }); } const { httpReq, ...clientInfo } = getClientRequestIPsInfo({ httpReq: req, res }); - await emailAuthConfig.onEmailConfirmation({ - confirmationCode: id, - clientInfo, - req: httpReq, - }); - res.json({ success: true, message: "Email confirmed" }); + const response = await this.throttledFunc(async () => + emailAuthConfig.onEmailConfirmation({ + confirmationCode: id, + clientInfo, + req: httpReq, + }) + ); + if (typeof response === "string") { + return res + .status(HTTP_FAIL_CODES.BAD_REQUEST) + .json({ success: false, code: "something-went-wrong" }); + } + if (response.redirect_to) { + return res.redirect(response.redirect_to); + } + res.json(response); } catch (_e) { res .status(HTTP_FAIL_CODES.BAD_REQUEST) @@ -40,4 +47,4 @@ export const setConfirmEmailRequestHandler = ( }; app.get(AUTH_ROUTES_AND_PARAMS.confirmEmailExpressRoute, requestHandler); -}; +} diff --git a/lib/Auth/endpoints/setMagicLinkRequestHandler.ts b/lib/Auth/endpoints/setMagicLinkRequestHandler.ts index b5a51c84..87286a48 100644 --- a/lib/Auth/endpoints/setMagicLinkRequestHandler.ts +++ b/lib/Auth/endpoints/setMagicLinkRequestHandler.ts @@ -7,6 +7,7 @@ import { getClientRequestIPsInfo, HTTP_FAIL_CODES, } from "../AuthHandler"; +import { DBOFullyTyped } from "../../DBSchemaBuilder"; export function setMagicLinkRequestHandler( this: AuthHandler, @@ -28,7 +29,7 @@ export function setMagicLinkRequestHandler( const response = await this.throttledFunc(async () => { return onMagicLink( id, - this.dbo as any, + this.dbo as DBOFullyTyped, this.db, getClientRequestIPsInfo({ httpReq: req, res }) ); @@ -38,7 +39,7 @@ export function setMagicLinkRequestHandler( } else { this.setCookieAndGoToReturnURLIFSet(response.session, { req, res }); } - } catch (e) { + } catch (_e) { res .status(HTTP_FAIL_CODES.UNAUTHORIZED) .json({ success: false, code: "something-went-wrong" }); diff --git a/package-lock.json b/package-lock.json index 882930b3..08acfb89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.196", + "version": "4.2.197", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.196", + "version": "4.2.197", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0", diff --git a/package.json b/package.json index 60eb63de..e3dc692a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.2.196", + "version": "4.2.197", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index 4096e362..07026cec 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.196", + "version": "4.2.197", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0",