Skip to content

Commit

Permalink
add email confirm redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 22, 2024
1 parent b791e8c commit 7dfefb2
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 25 deletions.
6 changes: 5 additions & 1 deletion docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ prostgles<DBGeneratedSchema>({
Defaults to "session_id"
- **getUser** <span style="color: red">required</span> <span style="color: green;">(sid: string | undefined, dbo: DBOFullyTyped&lt;S&gt;, db: DB, client: AuthClientRequest & LoginClientInfo) =&gt; Awaitable&lt;AuthResultOrError&lt;...&gt;&gt;</span>

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** <span style="color: grey">optional</span> <span style="color: green;">ExpressConfig</span>

Will setup auth routes
Expand Down
2 changes: 1 addition & 1 deletion lib/Auth/AuthTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export type EmailProvider =
confirmationCode: string;
clientInfo: LoginClientInfo;
req: ExpressReq;
}) => Awaitable<AuthFailure["code"] | AuthResponse.AuthSuccess>;
}) => Awaitable<AuthFailure["code"] | (AuthResponse.AuthSuccess & { redirect_to?: string })>;
};

export type AuthProviderUserData =
Expand Down
2 changes: 1 addition & 1 deletion lib/Auth/authProviders/setEmailProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
39 changes: 23 additions & 16 deletions lib/Auth/endpoints/setConfirmEmailRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
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<AuthRegistrationConfig<void>>["email"],
{ signupType: "withPassword" }
>,
app: e.Express
) => {
) {
const requestHandler = async (
req: Request,
res: Response<
| AuthResponse.PasswordRegisterSuccess
| AuthResponse.PasswordRegisterFailure
| AuthResponse.AuthSuccess
>
res: Response<AuthFailure | AuthResponse.AuthSuccess>
) => {
const { id } = req.params;
try {
if (!id || typeof id !== "string") {
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)
Expand All @@ -40,4 +47,4 @@ export const setConfirmEmailRequestHandler = (
};

app.get(AUTH_ROUTES_AND_PARAMS.confirmEmailExpressRoute, requestHandler);
};
}
5 changes: 3 additions & 2 deletions lib/Auth/endpoints/setMagicLinkRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getClientRequestIPsInfo,
HTTP_FAIL_CODES,
} from "../AuthHandler";
import { DBOFullyTyped } from "../../DBSchemaBuilder";

export function setMagicLinkRequestHandler(
this: AuthHandler,
Expand All @@ -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 })
);
Expand All @@ -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" });
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7dfefb2

Please sign in to comment.