QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.2k stars 54 forks source link

Feedback for “Middleware configuration” #284

Closed MisterCommit closed 8 months ago

MisterCommit commented 8 months ago

I am already usingng middleware for auth. is there any how can i add return I18nMiddleware(request); before ruten.

import type { NextRequest } from 'next/server';
import { userRole } from '@enums/user-role';
import { cookiesKeys } from '@enums/cookies-keys';

// eslint-disable-next-line consistent-return
export function middleware(request: NextRequest) {
  const path = request.nextUrl.pathname;
  const isPrivatePath = path.startsWith('/brand') || path.startsWith('/influencer');

  // get data from request cookies base
  const token = request.cookies.get(cookiesKeys.token)?.value || null;
  const role = request.cookies.get(cookiesKeys.role)?.value || null;

  //  check if user hit base url "/ like https://app.influence.network/"

  //  if token or user role is null and path is base url then redirect to signin.
  if (path === '/' && (!token || !role)) {
    return NextResponse.redirect(new URL('/signin', request.nextUrl));
  }

  //  if token and user role in cookies it will redirect to user dashboard according to user type.
  if (path === '/' && token && role === userRole.Brand) {
    return NextResponse.redirect(new URL('brand/dashboard', request.nextUrl));
  }

  if (path === '/' && token && role === userRole.Influencer) {
    return NextResponse.redirect(new URL('influencer/dashboard', request.nextUrl));
  }

  // redirect user to signin if token or role is not in cookie
  if (isPrivatePath && (!token || !role)) {
    return NextResponse.redirect(new URL('/signin', request.nextUrl));
  }

  // redirect user to dashboard if token is present in cookie and user try to access some public path like signin or signup
  if (!isPrivatePath && token && role) {
    if (role === userRole.Brand) {
      return NextResponse.redirect(new URL('brand/dashboard', request.nextUrl));
    }
    if (role === userRole.Influencer) {
      return NextResponse.redirect(new URL('influencer/dashboard', request.nextUrl));
    }
  }
}

// See "Matching Paths" below to learn more
export const config = {
  matcher: ['/', '/signin', '/signup', '/brand/:path*', '/influencer/:path*'],
};
QuiiBz commented 8 months ago

I can't find the part with the auth middleware in the code you attached. See https://github.com/QuiiBz/next-international/issues/187 if you want to chain multiple middlewares together.

MisterCommit commented 8 months ago

@QuiiBz I follow a straightforward approach if token and role are there then the user is good to go else redirect to /signin in the main middleware function. now if I want to add internationalisation middleware from your package how can I do this.

QuiiBz commented 8 months ago

See https://github.com/QuiiBz/next-international/issues/187#issuecomment-1729745552 for an example of how to chain multiple middleware - closing this issue as it's not directly related to next-international.