Skip to content

Commit

Permalink
Move next auth into own package (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnmclean authored Aug 28, 2024
1 parent bdb491a commit fb4e529
Show file tree
Hide file tree
Showing 15 changed files with 521 additions and 84 deletions.
2 changes: 2 additions & 0 deletions apps/sovoli.com/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const nextConfig = {
transpilePackages: [
"@sovoli/ui",
"@sovoli/api",
"@sovoli/auth",
"@sovoli/db",
"nativewind",
"react-native-svg",
"react-native-css-interop",
Expand Down
3 changes: 1 addition & 2 deletions apps/sovoli.com/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"with-env": "dotenv -e ../../.env --"
},
"dependencies": {
"@auth/drizzle-adapter": "1.4.2",
"@gluestack/ui-next-adapter": "2.1.12",
"@sovoli/api": "workspace:*",
"@sovoli/auth": "workspace:*",
"@sovoli/db": "workspace:*",
"@sovoli/ui": "workspace:*",
"@t3-oss/env-nextjs": "0.10.1",
Expand All @@ -37,7 +37,6 @@
"@vercel/speed-insights": "1.0.12",
"nativewind": "4.0.36",
"next": "14.2.4",
"next-auth": "5.0.0-beta.20",
"raf": "3.4.1",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/sovoli.com/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { handlers } from "~/auth"; // Referring to the auth.ts we just created
import { handlers } from "@sovoli/auth";

export const { GET, POST } = handlers;
3 changes: 1 addition & 2 deletions apps/sovoli.com/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { auth, signIn } from "@sovoli/auth";
import { HomeScreen } from "@sovoli/ui/screens/home";

import { auth, signIn } from "~/auth";

export default function Home() {
return (
<div className="container mx-auto">
Expand Down
63 changes: 0 additions & 63 deletions apps/sovoli.com/src/auth.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/sovoli.com/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { auth as middleware } from "~/auth";
export { auth as middleware } from "@sovoli/auth";
7 changes: 7 additions & 0 deletions packages/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `@sovoli/services`

A collection of external services that we use in our app.

## Services

- [Google Books API](https://developers.google.com/books/docs/overview)
10 changes: 10 additions & 0 deletions packages/auth/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import baseConfig, { restrictEnvAccess } from "@sovoli/eslint-config/base";

/** @type {import('typescript-eslint').Config} */
export default [
{
ignores: ["dist/**"],
},
...baseConfig,
//...restrictEnvAccess,
];
39 changes: 39 additions & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@sovoli/auth",
"version": "0.1.0",
"private": true,
"type": "module",
"exports": {
".": {
"react-server": "./src/index.rsc.ts",
"default": "./src/index.ts"
}
},
"license": "MIT",
"scripts": {
"clean": "git clean -xdf .cache .turbo dist node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@sovoli/db": "workspace:*",
"@auth/core": "0.34.2",
"@auth/drizzle-adapter": "1.4.2",
"@t3-oss/env-nextjs": "0.11.0",
"next": "14.2.5",
"next-auth": "5.0.0-beta.20",
"react": "18.2.0",
"react-dom": "18.2.0",
"zod": "catalog:"
},
"devDependencies": {
"@sovoli/eslint-config": "workspace:*",
"@sovoli/prettier-config": "workspace:*",
"@sovoli/typescript-config": "workspace:*",
"eslint": "catalog:",
"prettier": "catalog:",
"typescript": "catalog:"
},
"prettier": "@sovoli/prettier-config"
}
62 changes: 62 additions & 0 deletions packages/auth/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { DefaultSession, NextAuthConfig, Session as NextAuthSession } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db } from "@sovoli/db";
// import {
// accounts,
// sessions,
// users,
// verificationTokens,
// } from "@sovoli/db/schema";
import Resend from "next-auth/providers/resend";


const adapter = DrizzleAdapter(db);

export const authConfig: NextAuthConfig = {
adapter,
providers: [
Resend({
from: "[email protected]",
}),
],
callbacks: {
session({ session, user }) {
// `session.user.address` is now a valid property, and will be type-checked
// in places like `useSession().data.user` or `auth().user`
return {
...session,
user: {
...session.user,
username: user.username,
},
};
},
},
}

export const validateToken = async (
token: string,
): Promise<NextAuthSession | null> => {
const sessionToken = token.slice("Bearer ".length);
const session = await adapter.getSessionAndUser?.(sessionToken);
return session
? {
user: {
...session.user,
},
expires: session.session.expires.toISOString(),
}
: null;
};


declare module "next-auth" {
interface User {
username: string;
}
interface Session {
user: {
username: string;
} & DefaultSession["user"];
}
}
20 changes: 20 additions & 0 deletions packages/auth/src/index.rsc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { cache } from "react";
import NextAuth from "next-auth";

import { authConfig } from "./config";

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

const { handlers, 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 { handlers, auth, signIn, signOut };

export {
validateToken,
} from "./config";
13 changes: 13 additions & 0 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import NextAuth from "next-auth";

import { authConfig } from "./config";

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

const { handlers, auth, signIn, signOut } = NextAuth(authConfig);

export { handlers, auth, signIn, signOut };

export {
validateToken,
} from "./config";
5 changes: 5 additions & 0 deletions packages/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@sovoli/typescript-config/base.json",
"include": ["src"],
"exclude": ["node_modules"]
}
3 changes: 1 addition & 2 deletions packages/services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"clean": "git clean -xdf node_modules dist .turbo",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
"with-env": "dotenv -e ../../.env --"
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {},
"devDependencies": {
Expand Down
Loading

0 comments on commit fb4e529

Please sign in to comment.