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

Restrict pages for non logged in user #34

Open
janithRS opened this issue Jan 23, 2024 · 1 comment
Open

Restrict pages for non logged in user #34

janithRS opened this issue Jan 23, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@janithRS
Copy link
Contributor

Describe the bug
We can navigate to pages like the dashboard without logging in.
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:
Do not login
Navigate to /dashboard

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
Restrict users who are not logged in to pages except the home page.
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

@janithRS janithRS added the bug Something isn't working label Jan 23, 2024
@janithRS
Copy link
Contributor Author

Since we are using i18n router + next auth, it seems like it's tricky to restrict the user who is not logged in using traditional methods. So far I've tried this way but it doesnt work 100% correctly.

// function getLocale(request) {
//   let headers = { "accept-language": "en" };
//   let languages = new Negotiator({ headers }).languages();
//   return match(languages, locales, defaultLocale); // -> 'en'
// }

// export function middleware(request) {
//   // cancel if exception
//   const pathname = request.nextUrl.pathname;
//   const isException = [
//     "/img",
//     "/preview",
//     "/icons",
//     "/logo.svg",
//     "/api",
//     "/manifest.json",
//     "/sw.js",
//   ].some((allowedPath) => pathname.startsWith(`${allowedPath}`));
//   if (isException) return;

//   // Check if there is any supported locale in the pathname
//   const pathnameIsMissingLocale = locales.every(
//     (locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
//   );

//   // Redirect if there is no locale
//   if (pathnameIsMissingLocale) {
//     const locale = getLocale(request);
//     return NextResponse.redirect(
//       new URL(`/${locale}/${pathname}`, request.url)
//     );
//     u;
//   }

//   // check if auth is required
//   if (pathname.includes("/dashboard")) {
//     const locale = getLocale(request);

//     // check & handle if logged in
//     const nextAuthResponse = nextAuthMiddleware(request);
//     console.log("nextAuthResponse", nextAuthResponse);
//     if (nextAuthResponse) {
//       // nextAuthResponse.then((res) => {
//         // console.log("res", res);
//         // if (res.status === 307) {
//           // return NextResponse.redirect(
//           //   new URL(`/${locale}`, request.url)
//           // );
//         // }
//       // });
//       // console.log("nextAuthResponse", nextAuthResponse);
//         // return NextResponse.next();
        
//       return nextAuthResponse;
//     }
//   }

//   return NextResponse.next();
// }

Need to do some research on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant