Skip to content

Commit d3431be

Browse files
committed
configure admin middleware
1 parent e782a2b commit d3431be

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
2-
import LoggedIn from './components/LoggedIn';
2+
import MainComponent from './components/Main';
33

44
export default function Home() {
5-
return <LoggedIn />;
5+
return <MainComponent />;
66
}

peerprep-fe/src/app/signin/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Input } from '@/components/ui/input';
66
import Link from 'next/link';
77
import { GithubIcon } from 'lucide-react';
88
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
9-
import { axiosUserClient } from '@/network/axiosClient';
9+
import { axiosAuthClient } from '@/network/axiosClient';
1010
import { login } from '@/lib/auth';
1111
import { useRouter } from 'next/navigation';
1212
import { useAuthStore } from '@/state/useAuthStore';
@@ -23,7 +23,7 @@ export default function LoginForm() {
2323
e.preventDefault();
2424
setError('');
2525

26-
const result = await axiosUserClient.post('/auth/login', {
26+
const result = await axiosAuthClient.post('/auth/login', {
2727
email: email,
2828
password: password,
2929
});

peerprep-fe/src/middleware.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
import { NextResponse } from 'next/server';
22
import type { NextRequest } from 'next/server';
3+
import axios from 'axios';
34

45
// This function can be marked `async` if using `await` inside
5-
export function middleware(request: NextRequest) {
6+
export async function middleware(request: NextRequest) {
67
const token = request.cookies.get('access-token');
78

89
// Check if token exists
9-
if (token && token.value) {
10-
return NextResponse.next();
10+
if (!token) {
11+
return NextResponse.redirect(new URL('/signin', request.url)); // Ensure redirection response is returned
1112
}
12-
return NextResponse.redirect(new URL('/signin', request.url));
13+
14+
// Check if accessing /admin route
15+
if (request.nextUrl.pathname.startsWith('/admin')) {
16+
try {
17+
const res = await axios.get(
18+
'http://localhost:3001/api/v1/auth/verify-token',
19+
{
20+
headers: {
21+
Authorization: `Bearer ${token.value}`,
22+
},
23+
},
24+
);
25+
console.log(res.status);
26+
27+
// Check if the response is valid
28+
if (res.status !== 200) {
29+
return NextResponse.redirect(new URL('/', request.url)); // Redirect in case of error
30+
}
31+
} catch (error) {
32+
console.error('Error verifying token:', error);
33+
return NextResponse.redirect(new URL('/', request.url)); // Redirect on error
34+
}
35+
}
36+
37+
// Continue to the next middleware or route handler
38+
return NextResponse.next();
1339
}
1440

1541
export const config = {

peerprep-fe/src/network/axiosClient.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const axiosQuestionClient = axios.create({
1010
},
1111
});
1212

13-
const axiosUserClient = axios.create({
13+
const axiosAuthClient = axios.create({
1414
baseURL:
1515
process.env.NEXT_PUBLIC_USER_SERVICE_URL || 'http://localhost:3001/api/v1',
1616
headers: {
@@ -30,6 +30,18 @@ axiosQuestionClient.interceptors.request.use(
3030
(error) => Promise.reject(error),
3131
);
3232

33+
axiosAuthClient.interceptors.request.use(
34+
(config) => {
35+
const token = getCookie('access-token');
36+
console.log('token', token);
37+
if (token) {
38+
config.headers['Authorization'] = `Bearer ${token}`;
39+
}
40+
return config;
41+
},
42+
(error) => Promise.reject(error),
43+
);
44+
3345
// // TODO: Add response interceptors as needed
3446
// axiosClient.interceptors.response.use(
3547
// (response) => response,
@@ -42,4 +54,4 @@ axiosQuestionClient.interceptors.request.use(
4254
// },
4355
// );
4456

45-
export { axiosQuestionClient, axiosUserClient };
57+
export { axiosQuestionClient, axiosAuthClient };

0 commit comments

Comments
 (0)