Skip to content
Merged
Next Next commit
Add callbacks to next and express auth
  • Loading branch information
adam-maj committed Aug 31, 2022
commit cfadada527fc26eb6c7a265d6c82d2e1688a41b6
7 changes: 6 additions & 1 deletion packages/auth/src/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ 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 = ctx.callbacks.user(address);
}

user = { ...user, address };
} catch {
// No-op
}
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);
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) => void;
user?: (address: string) => ThirdwebAuthUser;
};
};

export type ThirdwebAuthContext = {
sdk: ThirdwebSDK;
domain: string;
callbacks?: {
login?: (address: string) => void;
user?: (address: string) => ThirdwebAuthUser;
};
};

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);
ctx.callbacks.login(address);
}

return res.status(301).redirect(req.query.redirect as string);
}
7 changes: 6 additions & 1 deletion packages/auth/src/next/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default async function handler(
if (token) {
try {
const address = await sdk.auth.authenticate(domain, token);
user = { address };

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

user = { ...user, address };
} catch {
// No-op
}
Expand Down
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) => void;
user?: (address: string) => ThirdwebAuthUser;
};
};

export type ThirdwebAuthContext = {
sdk: ThirdwebSDK;
domain: string;
callbacks?: {
login?: (address: string) => void;
user?: (address: string) => ThirdwebAuthUser;
};
};

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