Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: include user.id on session #180

Merged
merged 14 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/installers/next-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export const nextAuthInstaller: Installer = async ({
"src/pages/api/restricted.ts",
);

const nextAuthDefinitionSrc = path.join(nextAuthAssetDir, "next-auth.d.ts");
const nextAuthDefinitionDest = path.join(projectDir, "next-auth.d.ts");

await Promise.all([
fs.copy(apiHandlerSrc, apiHandlerDest),
fs.copy(restrictedApiSrc, restrictedApiDest),
fs.copy(nextAuthDefinitionSrc, nextAuthDefinitionDest),
]);
};
9 changes: 9 additions & 0 deletions template/addons/next-auth/api-handler-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import { prisma } from "../../../server/db/client";
import { env } from "../../../server/env";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
},
},
// Configure one or more authentication providers
adapter: PrismaAdapter(prisma),
providers: [
Expand Down
9 changes: 9 additions & 0 deletions template/addons/next-auth/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import CredentialsProvider from "next-auth/providers/credentials";
import { env } from "../../../server/env";

export const authOptions: NextAuthOptions = {
// Include user.id on session
bhatvikrant marked this conversation as resolved.
Show resolved Hide resolved
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
},
},
// Configure one or more authentication providers
providers: [
DiscordProvider({
Expand Down
12 changes: 12 additions & 0 deletions template/addons/next-auth/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DefaultSession } from "next-auth";
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id?: string;
} & DefaultSession["user"];
}
}
8 changes: 7 additions & 1 deletion template/base/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"incremental": true,
"noUncheckedIndexedAccess": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
"include": [
"next-env.d.ts",
"next-auth.d.ts",
bhatvikrant marked this conversation as resolved.
Show resolved Hide resolved
"**/*.ts",
"**/*.tsx",
"**/*.js"
],
"exclude": ["node_modules"]
}