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
redirectToSignIn not respecting publishableKey passed to authMiddleware #1442
Comments
Hello @dcyoung |
Hello @dcyoung // helper.ts - edited copy of packages/nextjs/src/server/redirect.ts
import { constants, redirect } from '@clerk/backend';
import { NextResponse } from 'next/server';
export const setHeader = (res: Response, name: string, val: string) => {
res.headers.set(name, val);
return res;
};
const redirectAdapter = (url: string) => {
const res = NextResponse.redirect(url);
return setHeader(res, constants.Headers.ClerkRedirectTo, 'true');
};
export const { redirectToSignIn, redirectToSignUp } = redirect({
redirectAdapter,
signInUrl: '<your value if needed>',
signUpUrl: '<your value if needed>',
publishableKey: '<your value if needed>',
}); // middleware.ts
import { customRedirectToSignIn } from './helper.ts'; // file above
authMiddleware({
afterAuth: (auth: AuthObject, req) => {
// ....
return customRedirectToSignIn({ returnBackUrl: req.experimental_clerkUrl.href });
// ...
}
}); I will ping you back when a fix or workaround is available. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
Hello 👋 We currently close issues after 40 days of inactivity. It's been 30 days since the last update here. If we missed this issue, please reply here. Otherwise, we'll close this issue in 10 days. As a friendly reminder: The best way to see an issue fixed is to open a pull request. If you're not sure how to do that, please check out our contributing guide. Thanks for being a part of the Clerk community! 🙏 |
Bump |
Could be related to #3001 |
Hello @kevinmitch14, |
Hello @dcyoung , import { clerkMiddleware } from '@clerk/nextjs/server';
export default clerkMiddleware((auth, request, next)=>{
if (!auth.userId) {
return auth().redirectToSignIn();
}
}, {
publishableKey: 'env-or-whatever'
});
export const config = {
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
}; |
Hey @dimkl what I've found is that as soon as you pass a custom You get the "Error: Missing publishableKey" error. It happens no matter what if you do not use the env variable name I think that the environment variables are not read correctly when they are passed in any way other than the default env variable method used in the docs.
Furthermore, if you pass random values directly into the middleware and also have the ENV vars defined, the ENV vars take precedence over the passed values. // This will still work if you have NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY defined in env vars.
authMiddleware({
secretKey: "randomvalue",
publishableKey: "randomvalue",
}) |
This still does not work as expected, the same issue persists. If I pass a |
@kevinmitch14 You are correct, there is an issue with the publishableKey and secretKey parameters not being respected. We are looking into this and we will fix the issue with the parameters having priority over the env and avoid raising error when the env are not set for the internal code of the middleware and not for the |
I've been trying to pass
but it always appears as empty string when I debug the source code from Clerk at javascript/packages/sdk-node/src/clerkExpressWithAuth.ts Lines 5 to 6 in 83ec173
Despite it reading like it would take it as argument. I don't know what is going on, but I keep getting the following error because of that:
|
I think this has been fixed, #3001 |
@nbkhope This issue related to the |
@nbkhope I've found a way to make it work. With environment vars
Without environment vars
Simpler version with env vars
|
@cenobitedk hey thanks for the message. Instead of trying to pass the values with the constructor functions, I ended up letting it automatically detect it as Clerk usually presents in the examples. For that to work, inject my configuration manager values into the required variables into my process.env.CLERK_PUBLISHABLE_KEY = getValueFromSomewhereElseNotInEnvFile();
// ... (repeat for any other necessary variable) |
I'm using a custom env var naming scheme in order to provide env vars at deploy time.
I am able to supply the publishableKey as a parameter to the middleware,
but when I call
redirectToSignIn
within afterAuth, I am getting an error:The text was updated successfully, but these errors were encountered: