Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/auth/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/express
/next
/next-auth
3 changes: 0 additions & 3 deletions packages/auth/express/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions packages/auth/next/package.json

This file was deleted.

80 changes: 80 additions & 0 deletions packages/auth/src/next-auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ThirdwebAuthConfig } from "./types";
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
import { serialize } from "cookie";
import { NextApiRequest, NextApiResponse } from "next";
import { NextAuthOptions, Session } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export function ThirdwebAuth(cfg: ThirdwebAuthConfig) {
const sdk = ThirdwebSDK.fromPrivateKey(cfg.privateKey, "mainnet");

const ThirdwebProvider = (req: NextApiRequest, res: NextApiResponse) =>
CredentialsProvider({
name: "ThirdwebAuth",
credentials: {
payload: {
label: "Payload",
type: "text",
placeholder: "",
},
},
async authorize({ payload }: any) {
try {
const parsed = JSON.parse(payload);
const token = await sdk.auth.generateAuthToken(
"thirdweb.com",
parsed
);
const address = await sdk.auth.authenticate("thirdweb.com", token);

// Securely set httpOnly cookie on request to prevent XSS on frontend
// And set path to / to enable thirdweb_auth_token usage on all endpoints
res.setHeader(
"Set-Cookie",
serialize("thirdweb_auth_token", token, {
path: "/",
httpOnly: true,
secure: true,
sameSite: "strict",
})
);

return { address };
} catch (err) {
return null;
}
},
});

const authOptions = (req: NextApiRequest, res: NextApiResponse) =>
({
callbacks: {
async session({ session }) {
const token = req.cookies.thirdweb_auth_token || "";
try {
const address = await sdk.auth.authenticate("thirdweb.com", token);
session.user = { ...session.user, address } as Session["user"];
return session;
} catch {
return session;
}
},
},
events: {
signOut() {
res.setHeader(
"Set-Cookie",
serialize("thirdweb_auth_token", "", {
path: "/",
expires: new Date(Date.now() + 5 * 1000),
})
);
},
},
} as Omit<NextAuthOptions, "providers">);

return {
ThirdwebProvider,
authOptions,
};
}
4 changes: 4 additions & 0 deletions packages/auth/src/next-auth/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ThirdwebAuthConfig = {
privateKey: string;
domain: string;
};