Skip to content

Commit 533d39d

Browse files
author
Harshit Yadav
committed
feat: added middleware, routes, auth-routes
1 parent b39dff9 commit 533d39d

File tree

8 files changed

+166
-2
lines changed

8 files changed

+166
-2
lines changed

.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
99

1010
DATABASE_URL="postgresql://CodeMaster17:qc8Qn7kBDrGb@ep-falling-pine-75509530-pooler.us-east-2.aws.neon.tech/auth?sslmode=require&pgbouncer=true"
11-
DIRECT_URL="postgresql://CodeMaster17:[email protected]/auth?sslmode=require"
11+
DIRECT_URL="postgresql://CodeMaster17:[email protected]/auth?sslmode=require"
12+
13+
AUTH_SECRET = 'secret'

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,75 @@ export const getUserById = async (id: string) => {
134134
const exisitingUser = await getUserByEmail(email)
135135
136136
```
137+
137138
33. Now, for `login` we have to install nextauth v5
138-
`npm i next-auth@beta`
139+
`npm i next-auth@beta`
140+
34. Create `auth.ts` file in root directory for configuration
141+
35. Add following code to `auth.ts` file
142+
- paste the code from website
143+
36. Create `app/api/auth/[...nextauth].ts` file and paste the code from the wesbite
144+
145+
- remove the edge because prisma does not support it
146+
147+
37. Add `AUTH_SECRET` in `.env` file
148+
149+
- for the development mode we can use any string
150+
151+
38. Go to see logs `http://localhost:3000/api/auth/providers`
152+
153+
39. Create middleware in `middleware.ts` in root directory
154+
155+
- `middleware.ts` file is nextjs specific file
156+
- update matcher to ` matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],` from clerk
157+
158+
40. Create `auth.config.ts` file in root directory
159+
160+
- paste the code from website
161+
162+
41. Update `auth.ts` file
163+
164+
```
165+
// * Comnfiguration for authentication
166+
import NextAuth from "next-auth";
167+
import authConfig from "@/auth.config";
168+
import { db } from "./lib/database.connection";
169+
import { PrismaAdapter } from "@auth/prisma-adapter";
170+
export const {
171+
handlers: { GET, POST },
172+
auth,
173+
} = NextAuth({
174+
adapter: PrismaAdapter(db), // prisma adapter is supported on non edge
175+
session: { strategy: "jwt" },
176+
...authConfig,
177+
});
178+
179+
180+
```
181+
182+
42. Update `api/auth/[...nextauth].ts` file
183+
184+
```
185+
// * middleware works on the edge
186+
187+
import authConfig from "./auth.config";
188+
import NextAuth from "next-auth";
189+
190+
const { auth } = NextAuth(authConfig);
191+
192+
export default auth((req) => {
193+
// req.auth
194+
});
195+
196+
// Optionally, don't invoke Middleware on some paths
197+
export const config = {
198+
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
199+
};
200+
201+
```
202+
203+
43. Create `route.ts` file in root directory
204+
205+
- this file will contain all types of routes
206+
207+
44. Edit middleware.ts file
208+
// cuurently here : 2:32:14

app/(protected)/settings/page.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { auth } from '@/auth'
2+
import React from 'react'
3+
4+
const Settings = async () => {
5+
const user = await auth()
6+
return (
7+
<div>
8+
{JSON.stringify(user)}
9+
</div>
10+
)
11+
}
12+
13+
export default Settings

app/api/auth/[...nextauth]/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { GET, POST } from "@/auth";

auth.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// * This file is used to trigger the middleware on the edge
2+
// * That is why we use auth.ts and auth.config.ts as seprate files
3+
4+
5+
import GitHub from "next-auth/providers/github";
6+
7+
import type { NextAuthConfig } from "next-auth";
8+
9+
export default {
10+
providers: [GitHub],
11+
} satisfies NextAuthConfig;

auth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// * Comnfiguration for authentication
2+
import NextAuth from "next-auth";
3+
import authConfig from "@/auth.config";
4+
import { db } from "./lib/database.connection";
5+
import { PrismaAdapter } from "@auth/prisma-adapter";
6+
export const {
7+
handlers: { GET, POST },
8+
auth,
9+
} = NextAuth({
10+
adapter: PrismaAdapter(db), // prisma adapter is supported on non edge
11+
session: { strategy: "jwt" },
12+
...authConfig,
13+
});

middleware.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// * middleware works on the edge
2+
3+
import authConfig from "./auth.config";
4+
import NextAuth from "next-auth";
5+
import {
6+
publicRoutes,
7+
authRoutes,
8+
apiAuthPrefix,
9+
DEFAULT_LOGIN_REDIRECT,
10+
} from "./route";
11+
const { auth } = NextAuth(authConfig);
12+
13+
export default auth((req) => {
14+
// req.auth
15+
const { nextUrl } = req;
16+
const isLoggeedIn = !!req.auth;
17+
18+
const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
19+
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
20+
const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
21+
22+
if (isApiAuthRoute) {
23+
return null;
24+
}
25+
26+
// TODO ; start from here
27+
});
28+
29+
// Optionally, don't invoke Middleware on some paths
30+
export const config = {
31+
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
32+
};

route.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// * an array of roues that are public
2+
// * These roues do not require authentication
3+
// @ @type {string[]}
4+
export const publicRoutes = ["/"];
5+
6+
// * an array of roues that are used for authentication
7+
// * These routes will redirect logged in users to /settings
8+
// @ @type {string[]}
9+
export const authRoutes = ["/auth/login", "/auth/register"];
10+
11+
/**
12+
* The prefix for API authentication routes
13+
* Routes that start with this prefix are used for API authentication purposes
14+
* @type {string}
15+
*/
16+
export const apiAuthPrefix = "/api/auth";
17+
18+
/**
19+
* The default redirect path after logging in
20+
* @type {string}
21+
*/
22+
export const DEFAULT_LOGIN_REDIRECT = "/settings";

0 commit comments

Comments
 (0)