Dinocan
TTCTheo's Typesafe Cult
•Created by Steve Melons on 11/30/2023 in #questions
Extend next-auth session with drizzle adapter
I found the solution for my project, I hope it will be a solution for you too.
import { env } from "@/env";
import { db } from "@/server/db";
import { accounts, sessions, sqliteTable, users, verificationTokens } from "@/server/db/schema";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { type SQLiteTableFn } from "drizzle-orm/sqlite-core";
import {
getServerSession,
type DefaultSession,
type NextAuthOptions,
} from "next-auth";
import type { Adapter } from 'next-auth/adapters';
import DiscordProvider from "next-auth/providers/discord";
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
role: "BANNED" | "USER" | "MODERATOR" | "ADMIN";
points: number;
} & DefaultSession["user"];
}
interface User {
role: "BANNED" | "USER" | "MODERATOR" | "ADMIN";
points: number;
}
}
type TableFnParams = Parameters<SQLiteTableFn>;
function dumbAdapter (
name: TableFnParams[0],
columns: TableFnParams[1],
extraConfig: TableFnParams[2],
) {
switch (name) {
case "user":
return users;
case "account":
return accounts;
case "session":
return sessions;
case "verification_token":
return verificationTokens;
default:
return sqliteTable(name, columns, extraConfig);
}
};
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
role: user.role,
points: user.points,
},
}),
},
adapter: DrizzleAdapter(db, dumbAdapter as SQLiteTableFn<undefined>) as Adapter,
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
};
export const getServerAuthSession = () => getServerSession(authOptions);
import { env } from "@/env";
import { db } from "@/server/db";
import { accounts, sessions, sqliteTable, users, verificationTokens } from "@/server/db/schema";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { type SQLiteTableFn } from "drizzle-orm/sqlite-core";
import {
getServerSession,
type DefaultSession,
type NextAuthOptions,
} from "next-auth";
import type { Adapter } from 'next-auth/adapters';
import DiscordProvider from "next-auth/providers/discord";
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
role: "BANNED" | "USER" | "MODERATOR" | "ADMIN";
points: number;
} & DefaultSession["user"];
}
interface User {
role: "BANNED" | "USER" | "MODERATOR" | "ADMIN";
points: number;
}
}
type TableFnParams = Parameters<SQLiteTableFn>;
function dumbAdapter (
name: TableFnParams[0],
columns: TableFnParams[1],
extraConfig: TableFnParams[2],
) {
switch (name) {
case "user":
return users;
case "account":
return accounts;
case "session":
return sessions;
case "verification_token":
return verificationTokens;
default:
return sqliteTable(name, columns, extraConfig);
}
};
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
role: user.role,
points: user.points,
},
}),
},
adapter: DrizzleAdapter(db, dumbAdapter as SQLiteTableFn<undefined>) as Adapter,
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
};
export const getServerAuthSession = () => getServerSession(authOptions);
75 replies