Skip to content

Commit

Permalink
move CoH check to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Markusplay committed Jan 31, 2025
1 parent 34ed1f7 commit e6794c0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { acceptCodeOfHonor } from '@/actions/profile.actions';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
Expand Down Expand Up @@ -52,9 +51,6 @@ export default function CodeOfHonorAlert() {
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<Link href="/">
<AlertDialogCancel>{t('button.go-to-home')}</AlertDialogCancel>
</Link>
<AlertDialogAction onClick={handleAcceptCodeOfHonor}>{t('button.agree')}</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
Expand Down
7 changes: 0 additions & 7 deletions src/app/[locale]/(private)/sub-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
import { cn } from '@/lib/utils';
import React from 'react';
import { getTranslations } from 'next-intl/server';
import { getUserDetails } from '@/actions/auth.actions';
import CodeOfHonorAlert from '@/components/code-of-honor-alert';

interface SubLayoutProps {
children: React.ReactNode;
Expand All @@ -21,11 +19,6 @@ interface SubLayoutProps {

export const SubLayout = async ({ children, breadcrumbs = [], pageTitle, className }: SubLayoutProps) => {
const t = await getTranslations('global.menu');
const user = await getUserDetails();

if (!user?.codeOfHonorSignDate && !!user?.studentProfile) {
return <CodeOfHonorAlert />;
}

return (
<section>
Expand Down
1 change: 0 additions & 1 deletion src/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
"add-new": "Add new",
"edit": "Edit",
"agree": "Agree",
"go-to-home": "Go to homepage",
"logout": "Logout"
},
"placeholder": {
Expand Down
1 change: 0 additions & 1 deletion src/messages/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@
"add-new": "Додати новий",
"edit": "Редагувати",
"agree": "Погоджуюсь",
"go-to-home": "На головну",
"logout": "Вийти"
},
"placeholder": {
Expand Down
25 changes: 24 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import JWT from 'jsonwebtoken';
import createMiddleware from 'next-intl/middleware';
import dayjs from 'dayjs';
import { trim } from 'radash';
import { getUserDetails } from '@/actions/auth.actions';

const intlMiddleware = createMiddleware(routing);

Expand All @@ -16,6 +17,8 @@ const composePathsRegExp = (paths: string[]) => RegExp(`^(/(${LOCALES.join('|')}

const rootRegExp = new RegExp('^\/?$', 'i');

const honorPageRegExp = composePathsRegExp(['/accept-honor']);

const authPathRegExp = composePathsRegExp(['/login', '/password-reset/success', '/password-reset']);

const publicPathRegExp = composePathsRegExp([
Expand All @@ -32,6 +35,7 @@ const publicPathRegExp = composePathsRegExp([
const isRoot = (request: NextRequest) => rootRegExp.test(request.nextUrl.pathname);
const isPublicPath = (request: NextRequest) => publicPathRegExp.test(request.nextUrl.pathname);
const isAuthPath = (request: NextRequest) => authPathRegExp.test(request.nextUrl.pathname);
const isAcceptHonorPath = (request: NextRequest) => honorPageRegExp.test(request.nextUrl.pathname);

const isAuthenticated = (request: NextRequest) => {
const cookie = request.cookies.get('token');
Expand Down Expand Up @@ -76,8 +80,21 @@ const authMiddleware = (request: NextRequest) => {
return intlMiddleware(request);
};

const honorMiddleware = async (request: NextRequest) => {
try {
const user = await getUserDetails();

if (!user?.codeOfHonorSignDate && !!user?.studentProfile) {
return redirectWithIntl(request, '/accept-honor');
}
} catch (error) {
return intlMiddleware(request);
}
return intlMiddleware(request);
};

export async function middleware(request: NextRequest) {
// If it's a root path — proces it with i18n middleware first
// If it's a root path — process it with i18n middleware first
if (isRoot(request)) {
return intlMiddleware(request);
}
Expand All @@ -90,5 +107,11 @@ export async function middleware(request: NextRequest) {
return intlMiddleware(request);
}

await honorMiddleware(request);

if (isAcceptHonorPath(request)) {
return redirectWithIntl(request, '/');
}

return (authMiddleware as any)(request);
}

0 comments on commit e6794c0

Please sign in to comment.