Skip to content

Commit e0eb179

Browse files
committed
FEAT: added next-auth authentication
1 parent 92f0bff commit e0eb179

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { handlers } from "@/auth"; // Referring to the auth.ts we just created
2+
export const { GET, POST } = handlers;

apps/web/auth.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { NextAuthConfig } from "next-auth";
2+
import google from "next-auth/providers/google";
3+
4+
export default {
5+
providers: [google],
6+
trustHost: true,
7+
callbacks: {
8+
// this function runs on each and every request according to the matcher in middleware file
9+
authorized: async ({ auth, request: { nextUrl } }) => {
10+
/*
11+
TODO: Uncomment This and add the code to protect routes in the similar way as in the example below
12+
*/
13+
// const isLoggedIn = !!auth?.user;
14+
// const isOnUser = nextUrl.pathname.startsWith("/user");
15+
// if (isOnUser) {
16+
// if (isLoggedIn) {
17+
// return true;
18+
// }
19+
// return false;
20+
// }
21+
return true;
22+
},
23+
},
24+
// theme: {
25+
// logo: "/logo.svg",
26+
// },
27+
} satisfies NextAuthConfig;

apps/web/auth.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PrismaAdapter } from "@auth/prisma-adapter";
2+
import NextAuth from "next-auth";
3+
import authConfig from "./auth.config";
4+
import { PG_PRISMA_CLIENT } from "@repo/database";
5+
export const { handlers, signIn, signOut, auth } = NextAuth({
6+
adapter: PrismaAdapter(PG_PRISMA_CLIENT),
7+
session: { strategy: "jwt", maxAge: 7 * 24 * 60 * 60 },
8+
...authConfig,
9+
// callbacks: {
10+
// // this function determines if the user is allowed to signing or not, it runs before the user is created in the database
11+
// async signIn({ user, account, profile, email, credentials }) {
12+
// return true;
13+
// },
14+
// },
15+
});

apps/web/middleware.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import NextAuth from "next-auth";
2+
import authConfig from "./auth.config";
3+
export const { auth: middleware } = NextAuth(authConfig);
4+
export const config = {
5+
/*
6+
* Match all request paths except for the ones starting with:
7+
* - api (API routes)
8+
* - _next/static (static files)
9+
* - _next/image (image optimization files)
10+
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
11+
*/
12+
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
13+
};

apps/web/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"@auth/prisma-adapter": "^2.7.0",
1213
"@radix-ui/react-slot": "^1.1.0",
1314
"@repo/database": "workspace:*",
1415
"@repo/ui": "workspace:*",
1516
"class-variance-authority": "^0.7.0",
1617
"clsx": "^2.1.1",
1718
"lucide-react": "^0.452.0",
1819
"next": "14.2.6",
20+
"next-auth": "5.0.0-beta.22",
1921
"react": "18.3.1",
2022
"react-dom": "18.3.1",
2123
"tailwind-merge": "^2.5.3",

pnpm-lock.yaml

Lines changed: 133 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)