Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add check member middleware #217

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/client/app/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { User } from '@/app/lib/utils';
import { cookies } from 'next/headers';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export function middleware(request: NextRequest) {
const user = cookies().get('user')?.value;
if (!user) {
return NextResponse.redirect(new URL('/', request.url));
}

const userObj = JSON.parse(user) as User;
if (!userObj.roles.some((role: { id: number }) => role.id === 2)) {
// Member role
return NextResponse.redirect(new URL('/', request.url));
}

if (userObj.status !== 'approved') {
return NextResponse.redirect(new URL('/', request.url));
}

if (userObj.date_validity && new Date(userObj.date_validity) < new Date()) {
return NextResponse.redirect(new URL('/', request.url));
}

return NextResponse.next();
}

export const config = {
matcher: '/members/:path*',
};
Loading