Skip to content

Commit

Permalink
add psw to email auth requests
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 19, 2024
1 parent 9a3cb58 commit c5079a6
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tests
documenation
docs
docs
examples
.vscode
10 changes: 8 additions & 2 deletions lib/Auth/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ExpressRes,
LoginClientInfo,
LoginParams,
LoginResponse,
} from "./AuthTypes";
import { getSafeReturnURL } from "./getSafeReturnURL";
import { setupAuthRoutes } from "./setupAuthRoutes";
Expand Down Expand Up @@ -247,7 +248,7 @@ export class AuthHandler {
});
};

loginThrottled = async (params: LoginParams, client: LoginClientInfo): Promise<BasicSession> => {
loginThrottled = async (params: LoginParams, client: LoginClientInfo): Promise<LoginResponse> => {
if (!this.opts?.login) throw "Auth login config missing";
const { responseThrottle = 500 } = this.opts;

Expand All @@ -260,6 +261,7 @@ export class AuthHandler {
};

if (!result) throw err;
if ("success" in result) throw result;
if (
(result && (typeof result.sid !== "string" || typeof result.expires !== "number")) ||
(!result && ![undefined, null].includes(result))
Expand All @@ -282,8 +284,12 @@ export class AuthHandler {
loginParams: LoginParams
) => {
const start = Date.now();
const { sid, expires } =
const loginResponse =
(await this.loginThrottled(loginParams, getLoginClientInfo({ httpReq: req }))) || {};
if ("success" in loginResponse) {
return res.status(HTTPCODES.AUTH_ERROR).json(loginResponse);
}
const { sid, expires } = loginResponse;
await this.prostgles.opts.onLog?.({
type: "auth",
command: "login",
Expand Down
17 changes: 13 additions & 4 deletions lib/Auth/AuthTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Express, NextFunction, Request, Response } from "express";
import { AnyObject, FieldFilter, IdentityProvider, UserLike } from "prostgles-types";
import {
AnyObject,
EmailLoginResponse,
EmailRegisterResponse,
FieldFilter,
IdentityProvider,
UserLike,
} from "prostgles-types";
import { DB } from "../Prostgles";
import { DBOFullyTyped } from "../DBSchemaBuilder";
import { PRGLIOSocket } from "../DboBuilder/DboBuilderTypes";
Expand Down Expand Up @@ -92,7 +99,7 @@ type EmailWithoutTo = Omit<Email, "to">;
type EmailProvider =
| {
signupType: "withMagicLink";
onRegistered: (data: { username: string }) => void | Promise<void>;
onRegistered: (data: { username: string }) => Awaitable<EmailRegisterResponse>;
emailMagicLink: {
onSend: (data: {
email: string;
Expand All @@ -108,7 +115,7 @@ type EmailProvider =
onRegistered: (
data: { username: string; password: string },
clientInfo: LoginClientInfo
) => void | Promise<void>;
) => Awaitable<EmailRegisterResponse>;
/**
* Defaults to 8
*/
Expand Down Expand Up @@ -280,7 +287,7 @@ export type Auth<S = void, SUser extends SessionUser = SessionUser> = {
dbo: DBOFullyTyped<S>,
db: DB,
client: LoginClientInfo
) => Awaitable<BasicSession> | BasicSession;
) => Awaitable<LoginResponse>;
logout?: (sid: string | undefined, dbo: DBOFullyTyped<S>, db: DB) => Awaitable<any>;

/**
Expand All @@ -291,6 +298,8 @@ export type Auth<S = void, SUser extends SessionUser = SessionUser> = {
};
};

export type LoginResponse = BasicSession | Exclude<EmailLoginResponse, { success: true }>;

export type LoginParams =
| { type: "username"; username: string; password: string; [key: string]: any }
| ({ type: "provider" } & AuthProviderUserData);
Expand Down
11 changes: 10 additions & 1 deletion lib/Auth/setEmailProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {

if (emailMessage) {
await sendEmail(emailMessage.smtp, emailMessage.message);
res.json({ success: true, message: "Email sent" });
res.json({
success: true,
message:
email.signupType === "withPassword" ?
`We've sent a confirmation email to ${emailMessage.message.to}. Please check your inbox (and your spam folder) for a message from us.`
: "Email sent",
});
}
} catch {
res.status(HTTPCODES.AUTH_ERROR).json({ success: false, error: "Failed to send email" });
Expand All @@ -82,6 +88,9 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {
app.get(AUTH_ROUTES_AND_PARAMS.confirmEmailExpressRoute, async (req, res) => {
const { id } = req.params ?? {};
try {
if (!id || typeof id !== "string") {
throw new Error("Invalid confirmation code");
}
const { httpReq, ...clientInfo } = getLoginClientInfo({ httpReq: req });
await email.emailConfirmation?.onConfirmed({
confirmationCode: id,
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.185",
"version": "4.2.186",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -55,7 +55,7 @@
"pg": "^8.11.5",
"pg-cursor": "^2.11.0",
"pg-promise": "^11.9.1",
"prostgles-types": "^4.0.122"
"prostgles-types": "^4.0.123"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand Down
16 changes: 8 additions & 8 deletions tests/client/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 tests/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "ISC",
"dependencies": {
"@types/node": "^20.9.2",
"prostgles-client": "^4.0.194",
"prostgles-client": "^4.0.195",
"prostgles-types": "^4.0.51",
"socket.io-client": "^4.8.1"
},
Expand Down
6 changes: 3 additions & 3 deletions 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 c5079a6

Please sign in to comment.