Skip to content

Commit

Permalink
improve email types
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Nov 27, 2024
1 parent 5151032 commit da27569
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
tests
13 changes: 4 additions & 9 deletions lib/Auth/AuthTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ type EmailProvider =
/**
* Defaults to 8
*/
minPasswordLength: number;
minPasswordLength?: number;
/**
* If provided, the user will be required to confirm their email address
*/
emailConfirmation?: {
onSend: (data: { email: string; confirmationUrlPath: string; }) => EmailWithoutTo | Promise<EmailWithoutTo>;
smtp: SMTPConfig;
onConfirmed: (data: { confirmationUrlPath: string; }) => void | Promise<void>;
onConfirmed: (data: { confirmationCode: string; }) => void | Promise<void>;
};
};

Expand Down Expand Up @@ -152,20 +152,15 @@ export type AuthRegistrationConfig<S> = {
*/
websiteUrl: string;

/**
* Do something with the registered user
*/
onRegister: (data: RegistrationData) => void | Promise<any>;

/**
* Used to stop abuse
*/
onProviderLoginStart: (data: { provider: IdentityProvider; dbo: DBOFullyTyped<S>, db: DB; req: ExpressReq, res: ExpressRes; clientInfo: LoginClientInfo }) => Promise<{ error: string; } | { ok: true; }>;
onProviderLoginStart?: (data: { provider: IdentityProvider; dbo: DBOFullyTyped<S>, db: DB; req: ExpressReq, res: ExpressRes; clientInfo: LoginClientInfo }) => Promise<{ error: string; } | { ok: true; }>;

/**
* Used to identify abuse
*/
onProviderLoginFail: (data: { provider: IdentityProvider; error: any; dbo: DBOFullyTyped<S>, db: DB; req: ExpressReq, res: ExpressRes; clientInfo: LoginClientInfo }) => void | Promise<void>;
onProviderLoginFail?: (data: { provider: IdentityProvider; error: any; dbo: DBOFullyTyped<S>, db: DB; req: ExpressReq, res: ExpressRes; clientInfo: LoginClientInfo }) => void | Promise<void>;
};

export type SessionUser<ServerUser extends UserLike = UserLike, ClientUser extends UserLike = UserLike> = {
Expand Down
9 changes: 4 additions & 5 deletions lib/Auth/setAuthProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const upsertNamedExpressMiddleware = (app: e.Express, handler: RequestHan

export async function setAuthProviders (this: AuthHandler, { registrations, app }: Required<Auth>["expressConfig"]) {
if(!registrations) return;
const { onRegister, onProviderLoginFail, onProviderLoginStart, websiteUrl, OAuthProviders } = registrations;
const { onProviderLoginFail, onProviderLoginStart, websiteUrl, OAuthProviders } = registrations;

await setEmailProvider.bind(this)(app);

Expand Down Expand Up @@ -56,7 +56,6 @@ export async function setAuthProviders (this: AuthHandler, { registrations, app
},
async (accessToken, refreshToken, profile, done) => {
// This callback is where you would normally store or retrieve user info from the database
await onRegister({ provider: providerName as "google", accessToken, refreshToken, profile });
return done(null, profile, { accessToken, refreshToken, profile });
}
)
Expand All @@ -74,8 +73,8 @@ export async function setAuthProviders (this: AuthHandler, { registrations, app
const db = this.db;
const dbo = this.dbo as any;
const args = { provider: providerName, req, res, clientInfo, db, dbo };
const startCheck = await onProviderLoginStart(args);
if("error" in startCheck){
const startCheck = await onProviderLoginStart?.(args);
if(startCheck && "error" in startCheck){
res.status(500).json({ error: startCheck.error });
return;
}
Expand All @@ -88,7 +87,7 @@ export async function setAuthProviders (this: AuthHandler, { registrations, app
},
async (error: any, _profile: any, authInfo: any) => {
if(error){
await onProviderLoginFail({ ...args, error });
await onProviderLoginFail?.({ ...args, error });
res.status(500).json({
error: "Failed to login with provider",
});
Expand Down
2 changes: 1 addition & 1 deletion lib/Auth/setEmailProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {
app.get(AUTH_ROUTES_AND_PARAMS.confirmEmailExpressRoute, async (req, res) => {
const { id } = req.params ?? {};
try {
await email.emailConfirmation?.onConfirmed({ confirmationUrlPath: id });
await email.emailConfirmation?.onConfirmed({ confirmationCode: id });
res.json({ msg: "Email confirmed" });
} catch (_e) {
res.status(500).json({ error: "Failed to confirm email" });
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.157",
"version": "4.2.158",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion tests/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ function dd(){
},
onProviderLoginStart: async () => ({ ok: true }),
onProviderLoginFail: console.error,
onRegister: console.log
},
}
},
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 da27569

Please sign in to comment.