gpt4 book ai didi

laravel - 如何修复 Laravel 8 Sanctum 和 VueJS 3 的 "401 Unauthorized"错误?

转载 作者:行者123 更新时间:2023-12-05 03:40:22 25 4
gpt4 key购买 nike

所以我在前端有这个 Laravel 8 项目和 VueJS 3,我在我的本地主机上运行它
我正在使用 Sanctum 进行身份验证。
登录和注册过程运行良好,但当我转到仪表板并尝试向我的数据库中添加内容时,出现了 401 Unauthorized 错误。

拉拉维尔:

编辑 用户 Controller

public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password,
];

if (Auth::attempt($credentials)) {
$success = true;
$user = User::where('email', $credentials['email'])->first();
$token = $user->createToken("authToken")->plainTextToken;
} else {
$success = false;
}

$response = [
'success' => $success,
'access_token' => $token,
];
return response()->json($response);
}

web.php

Route::get('/{any}', function () {
return view('welcome');
})->where('any', '.*');

api.php

// Auth
Route::post('login', [UserController::class, 'login']);
Route::post('register', [UserController::class, 'register']);
Route::post('logout', [UserController::class, 'logout'])->middleware('auth:sanctum');

// Niveau
Route::group(['prefix' => 'niveaux', 'middleware' => 'auth:sanctum'], function () {
Route::get('/', [NiveauScolaireController::class, 'index']);
Route::post('add', [NiveauScolaireController::class, 'add']);
Route::get('edit/{id}', [NiveauScolaireController::class, 'edit']);
Route::post('update/{id}', [NiveauScolaireController::class, 'update']);
Route::delete('delete/{id}', [NiveauScolaireController::class, 'delete']);
});

cors.php

'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,

Sanctuary .php

'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'SANCTUM_STATEFUL_DOMAINS',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
env('APP_URL') ? ',' . parse_url(env('APP_URL'), PHP_URL_HOST) : ''
))),

session.php

'domain' => env('SESSION_DOMAIN', null),

这就是我为 Laravel 端设置的内容。

VueJS

app.js

import { createApp } from "vue";
import App from "./App.vue";
import router from "./Router/index";
import axios from "axios";
const app = createApp(App);
app.config.globalProperties.$axios = axios;
app.use(router);
app.mount("#app");

bootsrap.js

window.axios = require("axios");
window.axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
windoow.axios.defaults.withCredentials = true;

登录组件

this.$axios.get("/sanctum/csrf-cookie").then((res) => {
this.$axios
.post("api/login", {
email: this.email,
password: this.password,
})
.then((res) => {
if (res.data.success) {
this.$router.push({ name: "Dashboard" });
} else {
console.log("Error: " + res.data.message);
}
})
.catch(function (error) {
console.log("error :>> ", error);
});
});

这里我对 sanctum/csrf-cookie 的请求进展顺利,我会收到 Cookie,登录工作正常,它从数据库获取用户,然后将我重定向到仪表板.

问题
现在在这里,当我尝试向 (api/niveaux/add) 发送请求时,请求已发送,但我收到 401 Unauthorized 错误和 {"message":"Unauthenticated." 响应。

仪表板组件

this.$axios
.post("api/niveaux/add", {
nom: this.niveau,
})
.then((res) => {
if (res.data.success) {
alert(res.data.message);
} else {
alert(res.data.message);
}
})
.catch(function (error) {
console.log("error :>> ", error);
});

最佳答案

在 laravel 中,您需要在每个具有 middleware('auth:sanctum') 的请求中向您发送 access_token。您可以在登录后获得的 cookie 中找到 access_token。(可能有不同的名称,例如 token)

axios.defaults.headers.common['Authorization'] = `Bearer ${access_token}` 

axios.defaults.headers.common['Authorization'] = `${access_token}` 

一定要做到这一点

关于laravel - 如何修复 Laravel 8 Sanctum 和 VueJS 3 的 "401 Unauthorized"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68141818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com