简体   繁体   English

Auth0 端点“api/auth/me”在 Next.js 应用程序中返回 404 错误

[英]Auth0 Endpoint "api/auth/me" returns a 404 Error in Next.js App

I have gone through the following tutorial to get my Next.js App integrated with Auth0.我已经完成了以下教程,让我的 Next.js 应用程序与 Auth0 集成。 I am able to log in and log out just fine but when trying to display user information on the page after login, the user object is unable to be returned .我可以正常登录和注销,但是在登录后尝试在页面上显示用户信息时,无法返回user object I have ensured that there is nothing wrong with the Profile.js page that is rendering the user object or the env.local file with my app's secret keys.我已确保使用我的应用程序的密钥呈现user object 或 env.local 文件的 Profile.js 页面没有任何问题。

After further inspection I noticed that I get an error in the browser console that reads: Failed to Load Resource... 404 Not Found: http://localhost:3000/api/auth/me .进一步检查后,我注意到我在浏览器控制台中收到一条错误消息: Failed to Load Resource... 404 Not Found: http://localhost:3000/api/auth/me

This error gives me a gut feeling that there is a discrepancy in the mapping between my next.js app and Auth0 since I have modified the basepath in next.config.js:这个错误让我有一种直觉,我的 next.js 应用程序和 Auth0 之间的映射存在差异,因为我修改了 next.config.js 中的基本路径:

module.exports = {
  basePath: '/my_path',

  webpack: (config) => {
    return config
  },

  env: {
  },

  publicRuntimeConfig: {
    BACKEND_API_URL: process.env.BACKEND_API_URL,
    CONSENT_COOKIE_NAME: 'ConsentCookie'
  },
}

Is there a way to add my basepath into the endpoint that the user object is being returned from?有没有办法将我的基本路径添加到user object 从中返回的端点? The end result would look something like: https://localhost:3000/my_path/api/auth/me最终结果类似于: https://localhost:3000/my_path/api/auth/me

I am not 100% certain that this will fix my issue with getting the user object returned properly, so I am open to any other suggestions and willing to add more context surrounding specific files in my app.我不是 100% 确定这会解决我的问题,让user object 正确返回,所以我愿意接受任何其他建议,并愿意在我的应用程序中添加更多关于特定文件的上下文。


Edit: After bringing this issue up on the Auth0 forums ( link ), I was pointed towards this link , which is another example Next.js Auth0 sample app, except they have written their frontend with TypeScript (which I am not familiar with).编辑:在 Auth0 论坛( 链接)上提出这个问题后,我被指向了这个链接,这是另一个例子 Next.js Auth0 示例应用程序,除了他们用 TypeScript(我不熟悉)编写了他们的前端。 They are manipulating the UserContext object and resetting the ProfileURL, which is what I am after;他们正在操纵UserContext object 并重置 ProfileURL,这就是我所追求的; so what would be the JavaScript equivalent to this?那么 JavaScript 相当于什么?

The same repsonse to the Auth0 forum post I mentioned also included another link to an example function that creates a custom URL for the login.我提到的对 Auth0 论坛帖子的相同回复还包括另一个指向示例 function 的链接,该示例为登录创建自定义 URL。 This is very close to what I am after since again, I am trying to create a custom auth URL to retrieve the User object and get rid of the 404... /api/auth/me not found error.这与我再次追求的非常接近,我正在尝试创建自定义身份验证 URL 以检索User object 并消除404... /api/auth/me not found错误。 Due to my inexperience with JS, my attempts at trying to create a similar function to the example stated previously have failed, so what would this look like?由于我对 JS 缺乏经验,我试图创建一个类似于前面所述示例的 function 的尝试失败了,那么这会是什么样子呢?

I am feeling intense bittersweet emotions after finding an insultingly simple solution to this issue.在找到解决此问题的极其简单的解决方案后,我感到苦乐参半。

Found in the readme.md of the NextJS-Auth0 repository... This small snippet of code fixed all of my issues after hours of searching for a solution -NextJS -Auth0 存储库的 readme.md 中找到...经过数小时搜索解决方案后,这段小代码修复了我所有的问题 -

// _app.js
function App({ Component, pageProps }) {
  return (
    <UserProvider loginUrl="/foo/api/auth/login" profileUrl="/foo/api/auth/me">
      <Component {...pageProps} />
    </UserProvider>
  );
}

Now to get back to wiping the tears off my desk..现在回去擦掉我桌子上的眼泪..

I have been having this issue too.我也一直有这个问题。 What was happening for my Next app deployed on Vercel is that all the api/auth/* routes were not working in production but everything worked locally.我在 Vercel 上部署的 Next 应用程序发生的情况是,所有api/auth/*路由都无法在生产环境中运行,但一切都在本地运行。

I'm using the Auth0 Universal Login Experience我正在使用 Auth0 通用登录体验

// package.json
...
"dependencies": {
    "@auth0/nextjs-auth0": "^1.9.2",
}
...

All I had before was the function我之前只有 function

// api/auth/[...auth0].ts
import { handleAuth } from "@auth0/nextjs-auth0";

export default handleAuth();

So what I did is create all the paths I'd need in my application in their respective files.所以我所做的是在我的应用程序各自的文件中创建我需要的所有路径。 I think Next.js was not creating the dynamic files at [...auth0].ts我认为 Next.js 没有在[...auth0].ts创建动态文件

// api/auth/callback.ts
import { handleCallback } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

const callbackHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    await handleCallback(req, res);
  } catch (error) {
    res.status(error.status || 400).end(error.message);
  }
};

export default callbackHandler;

// api/auth/login.ts
import { handleLogin } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

const loginHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    await handleLogin(req, res, {
      authorizationParams: {
        screen_hint: "login",
      },
    });
  } catch (error) {
    res.status(error.status || 400).end(error.message);
  }
};

export default loginHandler;

// api/auth/logout.ts
import { handleLogout } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

const logoutHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    await handleLogout(req, res);
  } catch (error) {
    res.status(error.status || 400).end(error.message);
  }
};

export default logoutHandler;

// api/auth/me.ts
// not api/auth/profile.ts

import { handleProfile } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

const profileHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    await handleProfile(req, res);
  } catch (error) {
    res.status(error.status || 400).end(error.message);
  }
};

export default profileHandler;

// api/auth/signup.ts

import { handleLogin } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

const signupHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  try {
    await handleLogin(req, res, {
      authorizationParams: {
        screen_hint: "signup",
      },
    });
  } catch (error) {
    res.status(error.status || 400).end(error.message);
  }
};

export default signupHandler;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM