Skip to content

Commit

Permalink
feat: added middleware for the manage page
Browse files Browse the repository at this point in the history
  • Loading branch information
Paribesh01 committed Oct 20, 2024
1 parent a5680bb commit c79d197
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/actions/company.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { CompanySchemaType } from '@/lib/validators/companies.validator';

export const getAllCompanies = async () => {
const auth = await getServerSession(authOptions);
if (!auth || !auth?.user?.id)
throw new ErrorHandler('Not Authorized', 'UNAUTHORIZED');
const companies = await prisma.company.findMany({
where: {
userId: auth?.user.id,
Expand Down
8 changes: 7 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextRequest } from 'next/server';
export async function middleware(req: NextRequest) {
const token = await getToken({ req, secret: process.env.SECRET });
const { pathname } = new URL(req.url);
if (!token && pathname === '/create') {
if (!token && (pathname === '/create' || pathname.startsWith('/manage'))) {
return NextResponse.redirect(new URL('/signin', req.url));
}
if (
Expand All @@ -22,6 +22,12 @@ export async function middleware(req: NextRequest) {
) {
return NextResponse.redirect(new URL('/create-profile', req.url));
}

// Check if user is trying to access any manage route without a token
if (pathname.startsWith('/manage') && !token) {
return NextResponse.redirect(new URL('/signin', req.url));
}

return NextResponse.next();
}

Expand Down

0 comments on commit c79d197

Please sign in to comment.