Skip to content

Commit

Permalink
rework auth for rsc cache
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Mar 30, 2024
1 parent 880bb33 commit 794bee8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
1 change: 0 additions & 1 deletion apps/nextjs/src/app/blog/PostGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ async function getData() {
const PostGrid = async () => {
const { articles } = await getData();

console.log(articles);
const postPreviews = articles.map((post, index) => (
<PostPreview key={index} {...post} />
));
Expand Down
5 changes: 4 additions & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts",
".": {
"react-server": "./src/index.rsc.ts",
"default": "./src/index.ts"
},
"./env": "./env.ts"
},
"license": "MIT",
Expand Down
31 changes: 31 additions & 0 deletions packages/auth/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { DefaultSession, NextAuthConfig } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import Discord from "next-auth/providers/discord";

import { db, tableCreator } from "@realms-world/db";

declare module "next-auth" {
interface Session {
user: {
id: string;
} & DefaultSession["user"];
}
}

export const authConfig = {
adapter: DrizzleAdapter(db, tableCreator),
providers: [Discord],
callbacks: {
session: (opts) => {
if (!("user" in opts)) throw "unreachable with session strategy";

return {
...opts.session,
user: {
...opts.session.user,
id: opts.user.id,
},
};
},
},
} satisfies NextAuthConfig;
21 changes: 21 additions & 0 deletions packages/auth/src/index.rsc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { cache } from "react";
import NextAuth from "next-auth";

import { authConfig } from "./config";

export type { Session } from "next-auth";

const {
handlers: { GET, POST },
auth: defaultAuth,
signIn,
signOut,
} = NextAuth(authConfig);

/**
* This is the main way to get session data for your RSCs.
* This will de-duplicate all calls to next-auth's default `auth()` function and only call it once per request
*/
const auth = cache(defaultAuth);

export { GET, POST, auth, signIn, signOut };
35 changes: 4 additions & 31 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
import type { DefaultSession } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import NextAuth from "next-auth";
import Discord from "next-auth/providers/discord";

import { db, tableCreator } from "@realms-world/db";
import { authConfig } from "./config";

export type { Session } from "next-auth";

declare module "next-auth" {
interface Session {
user: {
id: string;
} & DefaultSession["user"];
}
}

export const {
const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
adapter: DrizzleAdapter(db, tableCreator),
providers: [Discord],
callbacks: {
session: (opts) => {
if (!("user" in opts)) throw "unreachable with session strategy";

return {
...opts.session,
user: {
...opts.session.user,
id: opts.user.id,
},
};
},
},
} = NextAuth(authConfig);

});
export { GET, POST, auth, signIn, signOut };

0 comments on commit 794bee8

Please sign in to comment.