【发布时间】:2022-01-03 11:23:16
【问题描述】:
我刚刚安装了这个https://github.com/laravel/breeze-next 存储库,并且正如预期的那样,一切正常,如记录的那样。登录、注销和注册工作正常。
现在登录后,我想保护 API 路由,但是当我在登录后尝试访问受保护的路由时,它会抛出 401 错误。
这里是 Repo 中给出的 AXIOS 设置,
import Axios from 'axios'
const axios = Axios.create({
baseURL: process.env.NEXT_PUBLIC_BACKEND_URL,
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
withCredentials: true,
})
export default axios
我刚刚用 Axios 用 \api\test 添加了一个服务器调用
export async function getStaticProps() {
const response = await axios.get(`/api/test`);
return {
props: {
customers: response.data
},
}
}
这是 laravel 中的 API。
Route::middleware(['auth:sanctum'])->get('/test', function () {
return response()->json([
'val' => 1,
'msg' => 'successfully',
]);
});
如果我删除中间件,它工作正常。但是添加中间件auth sanctum时,总是显示401错误。
Server Error
Error: Request failed with status code 401
This error happened while generating the page. Any console logs will be displayed in the terminal window.
所以我可以在 SPA 中使用 sanctum 进行身份验证?
【问题讨论】: