Extending the default PrismaAdapter you get
I want to extend the default prisma adapter to be able to award a badge to the first 100 users to sign up for my site. Unfortunately, the following code actually breaks nextjs entirely and becomes a zombie process. Not quite sure what I'm doing wrong here:
Then later on
const extendedAdapter: typeof PrismaAdapter = (db) => {
const adapter = PrismaAdapter(db);
assert(adapter.createUser !== undefined);
return {
...adapter,
createUser: async (user) => {
const created = await adapter.createUser!(user);
const userCount = await db.user.count();
console.log('BADGE', db.userBadge);
if (userCount <= 100) {
await db.userBadge.create({
data: {
award: Award.FIRST_100_USER,
userId: created.id,
}
});
}
if (userCount <= 1000 && userCount > 100) {
await db.userBadge.create({
data: {
award: Award.FIRST_1000_USER,
userId: created.id,
}
});
}
return created;
}
}
}const extendedAdapter: typeof PrismaAdapter = (db) => {
const adapter = PrismaAdapter(db);
assert(adapter.createUser !== undefined);
return {
...adapter,
createUser: async (user) => {
const created = await adapter.createUser!(user);
const userCount = await db.user.count();
console.log('BADGE', db.userBadge);
if (userCount <= 100) {
await db.userBadge.create({
data: {
award: Award.FIRST_100_USER,
userId: created.id,
}
});
}
if (userCount <= 1000 && userCount > 100) {
await db.userBadge.create({
data: {
award: Award.FIRST_1000_USER,
userId: created.id,
}
});
}
return created;
}
}
}Then later on
`
export const authOptions: NextAuthOptions = {
{...}
adapter: extendedAdapter(db),
{...}
}
`
export const authOptions: NextAuthOptions = {
{...}
adapter: extendedAdapter(db),
{...}
}
Solution
Was able to make it work like this:
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { Award, type PrismaClient } from "@prisma/client"
import { type AdapterUser } from "next-auth/adapters"
const CustomAdapter = (client: PrismaClient) => {
const adapter = PrismaAdapter(client);
return {
...adapter,
async createUser(user: Omit<AdapterUser, 'id'>) {
const created = await client.user.create({
data: user,
})
const count = await client.user.count();
if (count <= 100) {
await client.userBadge.create({
data: {
award: Award.FIRST_100_USER,
userId: created.id,
}
});
}
if (count <= 1000 && count > 100) {
await client.userBadge.create({
data: {
award: Award.FIRST_1000_USER,
userId: created.id,
}
});
}
return created as AdapterUser;
},
}
}
export default CustomAdapter;import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { Award, type PrismaClient } from "@prisma/client"
import { type AdapterUser } from "next-auth/adapters"
const CustomAdapter = (client: PrismaClient) => {
const adapter = PrismaAdapter(client);
return {
...adapter,
async createUser(user: Omit<AdapterUser, 'id'>) {
const created = await client.user.create({
data: user,
})
const count = await client.user.count();
if (count <= 100) {
await client.userBadge.create({
data: {
award: Award.FIRST_100_USER,
userId: created.id,
}
});
}
if (count <= 1000 && count > 100) {
await client.userBadge.create({
data: {
award: Award.FIRST_1000_USER,
userId: created.id,
}
});
}
return created as AdapterUser;
},
}
}
export default CustomAdapter;