Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/curvy-bobcats-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/auth": patch
---

Add callbacks to express and next auth
9 changes: 7 additions & 2 deletions packages/auth/src/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ export function ThirdwebAuth(app: Express, cfg: ThirdwebAuthConfig) {
if (token) {
try {
const address = await sdk.auth.authenticate(domain, token);
user = { address };

if (ctx.callbacks?.user) {
user = await ctx.callbacks.user(address);
}

user = { ...user, address };
} catch {
// No-op
}
}

req.user = user;
req.user = user as ThirdwebAuthUser | null;
next();
});

Expand Down
5 changes: 5 additions & 0 deletions packages/auth/src/express/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ export default async function handler(
})
);

if (ctx.callbacks?.login) {
const address = sdk.auth.verify(domain, payload);
await ctx.callbacks.login(address);
}

return res.status(301).redirect(req.query.redirect as string);
}
9 changes: 9 additions & 0 deletions packages/auth/src/express/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ export type ThirdwebAuthConfig = {
privateKey: string;
domain: string;
authUrl?: string;
callbacks?: {
login?: (address: string) => (Promise<void> | void);
user?: (address: string) => (Promise<Omit<ThirdwebAuthUser, "address">> | Omit<ThirdwebAuthUser, "address">);
};
};

export type ThirdwebAuthContext = {
sdk: ThirdwebSDK;
domain: string;
callbacks?: {
login?: (address: string) => (Promise<void> | void);
user?: (address: string) => (Promise<Omit<ThirdwebAuthUser, "address">> | Omit<ThirdwebAuthUser, "address">);
};
};

export type ThirdwebAuthUser = {
address: string;
[key: string]: any;
};

export type RequestWithUser = Request & {
Expand Down
5 changes: 5 additions & 0 deletions packages/auth/src/next/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ export default async function handler(
})
);

if (ctx.callbacks?.login) {
const address = sdk.auth.verify(domain, payload);
await ctx.callbacks.login(address);
}

return res.status(301).redirect(req.query.redirect as string);
}
11 changes: 8 additions & 3 deletions packages/auth/src/next/routes/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ThirdwebAuthContext } from "../types";
import { ThirdwebAuthContext, ThirdwebAuthUser } from "../types";
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
Expand All @@ -19,11 +19,16 @@ export default async function handler(
if (token) {
try {
const address = await sdk.auth.authenticate(domain, token);
user = { address };

if (ctx.callbacks?.user) {
user = await ctx.callbacks.user(address);
}

user = { ...user, address };
} catch {
// No-op
}
}

return res.status(200).json(user);
return res.status(200).json(user as ThirdwebAuthUser | null);
}
9 changes: 9 additions & 0 deletions packages/auth/src/next/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ export type ThirdwebAuthRoute = "login" | "logout" | "user";
export type ThirdwebAuthConfig = {
privateKey: string;
domain: string;
callbacks?: {
login?: (address: string) => (Promise<void> | void);
user?: (address: string) => (Promise<Omit<ThirdwebAuthUser, "address">> | Omit<ThirdwebAuthUser, "address">);
};
};

export type ThirdwebAuthContext = {
sdk: ThirdwebSDK;
domain: string;
callbacks?: {
login?: (address: string) => (Promise<void> | void);
user?: (address: string) => (Promise<Omit<ThirdwebAuthUser, "address">> | Omit<ThirdwebAuthUser, "address">);
};
};

export type ThirdwebAuthUser = {
address: string;
[key: string]: any;
};