diff --git a/.npmignore b/.npmignore index 117d9d4b..86f118d4 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,5 @@ tests documenation -docs \ No newline at end of file +docs +examples +.vscode \ No newline at end of file diff --git a/lib/Auth/AuthHandler.ts b/lib/Auth/AuthHandler.ts index e6922d9a..d6a87f09 100644 --- a/lib/Auth/AuthHandler.ts +++ b/lib/Auth/AuthHandler.ts @@ -18,6 +18,7 @@ import { ExpressRes, LoginClientInfo, LoginParams, + LoginResponse, } from "./AuthTypes"; import { getSafeReturnURL } from "./getSafeReturnURL"; import { setupAuthRoutes } from "./setupAuthRoutes"; @@ -247,7 +248,7 @@ export class AuthHandler { }); }; - loginThrottled = async (params: LoginParams, client: LoginClientInfo): Promise => { + loginThrottled = async (params: LoginParams, client: LoginClientInfo): Promise => { if (!this.opts?.login) throw "Auth login config missing"; const { responseThrottle = 500 } = this.opts; @@ -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)) @@ -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", diff --git a/lib/Auth/AuthTypes.ts b/lib/Auth/AuthTypes.ts index 9aec9c62..ff4c66da 100644 --- a/lib/Auth/AuthTypes.ts +++ b/lib/Auth/AuthTypes.ts @@ -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"; @@ -92,7 +99,7 @@ type EmailWithoutTo = Omit; type EmailProvider = | { signupType: "withMagicLink"; - onRegistered: (data: { username: string }) => void | Promise; + onRegistered: (data: { username: string }) => Awaitable; emailMagicLink: { onSend: (data: { email: string; @@ -108,7 +115,7 @@ type EmailProvider = onRegistered: ( data: { username: string; password: string }, clientInfo: LoginClientInfo - ) => void | Promise; + ) => Awaitable; /** * Defaults to 8 */ @@ -280,7 +287,7 @@ export type Auth = { dbo: DBOFullyTyped, db: DB, client: LoginClientInfo - ) => Awaitable | BasicSession; + ) => Awaitable; logout?: (sid: string | undefined, dbo: DBOFullyTyped, db: DB) => Awaitable; /** @@ -291,6 +298,8 @@ export type Auth = { }; }; +export type LoginResponse = BasicSession | Exclude; + export type LoginParams = | { type: "username"; username: string; password: string; [key: string]: any } | ({ type: "provider" } & AuthProviderUserData); diff --git a/lib/Auth/setEmailProvider.ts b/lib/Auth/setEmailProvider.ts index 68b1aca9..01d6eccc 100644 --- a/lib/Auth/setEmailProvider.ts +++ b/lib/Auth/setEmailProvider.ts @@ -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" }); @@ -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, diff --git a/package-lock.json b/package-lock.json index 7d32598e..2c1f7a97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.185", + "version": "4.2.186", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.185", + "version": "4.2.186", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0", @@ -28,7 +28,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", @@ -3643,9 +3643,9 @@ } }, "node_modules/prostgles-types": { - "version": "4.0.122", - "resolved": "https://registry.npmjs.org/prostgles-types/-/prostgles-types-4.0.122.tgz", - "integrity": "sha512-m9F+ws74oN0RWjnf98x0KLXaOkgTO26/mtIcVdh98oj8AiDdOsXK8X1/t6vpY0UWzBT5fbrqDqx+4pZd2iwwyw==", + "version": "4.0.123", + "resolved": "https://registry.npmjs.org/prostgles-types/-/prostgles-types-4.0.123.tgz", + "integrity": "sha512-FZoUtL02meHVT9V6A7bJ0c9RjjaRIqJlfpNpnFXCZ+IDoQZ3fyE7VWgoTFzqCJ4TQKnXkCt32Whp6feYEL4TIw==", "license": "MIT" }, "node_modules/punycode": { @@ -6863,9 +6863,9 @@ "dev": true }, "prostgles-types": { - "version": "4.0.122", - "resolved": "https://registry.npmjs.org/prostgles-types/-/prostgles-types-4.0.122.tgz", - "integrity": "sha512-m9F+ws74oN0RWjnf98x0KLXaOkgTO26/mtIcVdh98oj8AiDdOsXK8X1/t6vpY0UWzBT5fbrqDqx+4pZd2iwwyw==" + "version": "4.0.123", + "resolved": "https://registry.npmjs.org/prostgles-types/-/prostgles-types-4.0.123.tgz", + "integrity": "sha512-FZoUtL02meHVT9V6A7bJ0c9RjjaRIqJlfpNpnFXCZ+IDoQZ3fyE7VWgoTFzqCJ4TQKnXkCt32Whp6feYEL4TIw==" }, "punycode": { "version": "2.3.1", diff --git a/package.json b/package.json index 927c4212..99a51940 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/tests/client/package-lock.json b/tests/client/package-lock.json index 0bd8028d..34648b63 100644 --- a/tests/client/package-lock.json +++ b/tests/client/package-lock.json @@ -10,7 +10,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" }, @@ -327,12 +327,12 @@ } }, "node_modules/prostgles-client": { - "version": "4.0.194", - "resolved": "https://registry.npmjs.org/prostgles-client/-/prostgles-client-4.0.194.tgz", - "integrity": "sha512-DNTIuy+yjPNx9ySlxpAwfUrZnBDnz5H4gKcxdut0of/3de8/THEzIUInn+yMaB1d6rmHXlPikp3YWHLPQGCFpg==", + "version": "4.0.195", + "resolved": "https://registry.npmjs.org/prostgles-client/-/prostgles-client-4.0.195.tgz", + "integrity": "sha512-ievqSkwJeKNpKETfCoN0gSuHHUZegiSjCGpuvrHoynuICe9T4GWKnJUSg6ZlYcS+DPNCX0Owdk5qw84uTMD//A==", "license": "MIT", "dependencies": { - "prostgles-types": "^4.0.122" + "prostgles-types": "^4.0.123" }, "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0", @@ -348,9 +348,9 @@ } }, "node_modules/prostgles-types": { - "version": "4.0.122", - "resolved": "https://registry.npmjs.org/prostgles-types/-/prostgles-types-4.0.122.tgz", - "integrity": "sha512-m9F+ws74oN0RWjnf98x0KLXaOkgTO26/mtIcVdh98oj8AiDdOsXK8X1/t6vpY0UWzBT5fbrqDqx+4pZd2iwwyw==", + "version": "4.0.123", + "resolved": "https://registry.npmjs.org/prostgles-types/-/prostgles-types-4.0.123.tgz", + "integrity": "sha512-FZoUtL02meHVT9V6A7bJ0c9RjjaRIqJlfpNpnFXCZ+IDoQZ3fyE7VWgoTFzqCJ4TQKnXkCt32Whp6feYEL4TIw==", "license": "MIT" }, "node_modules/psl": { diff --git a/tests/client/package.json b/tests/client/package.json index fe3e5345..128095a5 100644 --- a/tests/client/package.json +++ b/tests/client/package.json @@ -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" }, diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index 7ba6731a..d46a2665 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.185", + "version": "4.2.186", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0", @@ -43,7 +43,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", @@ -1828,7 +1828,7 @@ "pg-cursor": "^2.11.0", "pg-promise": "^11.9.1", "prettier": "^3.4.2", - "prostgles-types": "^4.0.122", + "prostgles-types": "^4.0.123", "socket.io": "^4.8.1", "typescript": "^5.3.3" }