-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Private Paths #273
Comments
Hey @alexllao, Good question – I think it just boils down to how you orchestrate the redirects within This is the default configuration with public paths: handleValidToken: async ({token, decodedToken, customToken}, headers) => {
// Authenticated user should not be able to access /login, /register and /reset-password routes
if (PUBLIC_PATHS.includes(request.nextUrl.pathname)) {
return redirectToHome(request);
}
return NextResponse.next({
request: {
headers
}
});
},
handleInvalidToken: async (_reason) => {
return redirectToLogin(request, {
path: '/login',
publicPaths: PUBLIC_PATHS
});
},
handleError: async (error) => {
console.error('Unhandled authentication error', {error});
return redirectToLogin(request, {
path: '/login',
publicPaths: PUBLIC_PATHS
});
} It works as follows:
We need to reverse this logic to support private paths, and make it so that:
First, we would need to write custom redirect function, instead of using built in const PRIVATE_PATHS = [/^\/dashboard/];
export function redirectToLoginIfPrivate(
request: NextRequest,
) {
if (PRIVATE_PATHS.some(regexp => regexp.test(request.nextUrl.pathname))) {
const url = request.nextUrl.clone();
url.pathname = '/login';
url.search = `redirect=${request.nextUrl.pathname}${url.search}`;
return NextResponse.redirect(url);
}
return NextResponse.next();
} Then, we could use the function in the new configuration: handleValidToken: async ({token, decodedToken, customToken}, headers) => {
return NextResponse.next({
request: {
headers
}
});
},
handleInvalidToken: async (_reason) => {
return redirectToLoginIfPrivate(request);
},
handleError: async (error) => {
return redirectToLoginIfPrivate(request);
} It would be much simpler than initial configuration :-) |
Thnks! |
No worries, I am glad you brought up the issue. I will provide update to |
Thnks!!!! |
I'm starting with what we talked about yesterday to do something basic mixed with intl. I haven't included the intlMiddleware yet, but I always see MISSING_CREDENTIALS in logs. ` import { clientConfig, serverConfig } from "./config/auth-config"; const PUBLIC_PATHS = [ // const PROTECTED_PATHS = ["/(es|ca)/dashboard"]; const intlMiddleware = createMiddleware(routing); export async function middleware(request: NextRequest) {
} export const config = { |
It works! `import type { NextRequest } from "next/server"; import { clientConfig, serverConfig } from "./config/auth-config"; const PUBLIC_PATHS_REGEX = process.env.PUBLIC_PATHS const PRIVATE_PATHS_REGEX = process.env.PRIVATE_PATHS const isPublicPath = (pathname: string) => const isPrivatePath = (pathname: string) => const intlMiddleware = createMiddleware(routing); const redirectToLogin = (request: NextRequest) => {
}; export async function middleware(request: NextRequest) {
} export const config = { |
If missing credentials happens in |
Is there any way to do it the other way around? What happens if most URLs are public and only those starting with /dashboard are private? Is there any way to make it easier? I guess I should modify the authMiddleware and adapt it to my taste and needs.
The text was updated successfully, but these errors were encountered: