Laravel 使用 Vue 和 Passport
Laravel with Vue and Passport
我有这个项目,我用 laravel 安装了 passport 和 vue。我有我的身份验证端点,但现在我正在努力让这些端点与我的项目前端一起工作。
所以当我安装 Laravel 时,我使用命令 php artisan ui vue --auth 来生成我的 login/register 脚手架。
api 个端点在文件 api.php
中
这是我的代码的一部分,来自 AuthController.php:
public function login(ApiLoginRequest $request){
//debug
//return $request;
$credentials = $request->only(['email', 'password']);
$authAttempt = auth()->attempt($credentials);
if(!$authAttempt){
return response([
'error' => 'Access forbidden',
'message' => 'Please check your email and password'
], 403 );
}
//debug result needs to be true
// return response([
// 'debug response' => $authAttempt
// ]);
$http = new Client();
$response = $http->post( 'http://laravel-xyzhub.test/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $this->CLIENT_ID,
'client_secret' => $this->CLIENT_SECRET,
'username' => $request->email,
'password' => $request->password,
'scope' => ''
]
]);
return json_decode((string) $response->getBody(), true );
}
vue 模板代码:
<template>
<div class="mx-aut h-full flex justify-center items-center bg-gray-800">
<div class="w-96 bg-green-400 rounded-lg shadow-xl p-6" >
<div class="text-center text-white uppercase">
<h1 class="font-extrabold text-6xl">XYZ HUB</h1>
<h1 class="text-3xl pt-8">Welcome Back</h1>
<h2 class="pb-8 text-xs">Enter your credentials below</h2>
</div>
<!-- form -->
<form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">
<div class="relative">
<label for="email" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Email</label>
<div class="">
<input id="email" type="email" class="pt-8 w-full rounded p-3 text-green-700" name="email" v-model="credentials.email" autocomplete="email" autofocus placeholder="your@email.com">
</div>
</div>
<div class="relative pt-6">
<label for="password" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Password</label>
<div class="">
<input id="password" type="password" class="pt-8 w-full rounded p-3 text-green-700" name="password" v-model="credentials.password" placeholder="password">
</div>
</div>
<div class="pt-8">
<button type="submit" class="uppercase font-bold rounded w-full bg-white text-green-400 py-2 px-3 text-2xl" v-on:click="loginUser">Login</button>
</div>
<div class="flex justify-center pt-8 text-white uppercase font-semibold text-sm">
<a :href="recover"> Forget Password </a>
</div>
<div class="flex justify-center pt-2 text-white uppercase font-semibold text-sm">
<a :href="register"> Register </a>
</div>
</form>
</div>
</div>
<script>
export default {
data(){
return {
credentials:{
email: '',
password: ''
}
}
},
methods:{
postNow(event){
console.log("event" , {event , $form: this.$refs.LoginForm});
axios.post('/api/login' , this.credentials)
.then( response => {
console.log ('response' , response.data); router.push("/register");
})
.catch( error => {
console.log("error", error.response);
})
}
}
}
我知道我在模板中的代码不正确只是不确定我做错了什么...任何帮助将不胜感激。提前致谢
更新:
因此,如果我删除表单中的阻止并在响应通过后添加重定向,那么突然之间我的提交没有通过
<form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">
改为
<form class="pb-8" @submit="postNow" ref="LoginForm" method="post">
你注意到我的端点也突然不正确了
真的对所有这些感到困惑...
您注意到端点是错误的。
所以改为:
axios.post('/login' , this.credentials)
改为
axios.post('/api/login' , this.credentials)
错误可能是由于路径“/login”中缺少 csrf 令牌字段。
使用 api 路由,您不需要 csrf。
此外,更容易记录错误响应,例如...
.catch( error => {
console.log(error.response);
})
我有这个项目,我用 laravel 安装了 passport 和 vue。我有我的身份验证端点,但现在我正在努力让这些端点与我的项目前端一起工作。
所以当我安装 Laravel 时,我使用命令 php artisan ui vue --auth 来生成我的 login/register 脚手架。
api 个端点在文件 api.php
中这是我的代码的一部分,来自 AuthController.php:
public function login(ApiLoginRequest $request){
//debug
//return $request;
$credentials = $request->only(['email', 'password']);
$authAttempt = auth()->attempt($credentials);
if(!$authAttempt){
return response([
'error' => 'Access forbidden',
'message' => 'Please check your email and password'
], 403 );
}
//debug result needs to be true
// return response([
// 'debug response' => $authAttempt
// ]);
$http = new Client();
$response = $http->post( 'http://laravel-xyzhub.test/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $this->CLIENT_ID,
'client_secret' => $this->CLIENT_SECRET,
'username' => $request->email,
'password' => $request->password,
'scope' => ''
]
]);
return json_decode((string) $response->getBody(), true );
}
vue 模板代码:
<template>
<div class="mx-aut h-full flex justify-center items-center bg-gray-800">
<div class="w-96 bg-green-400 rounded-lg shadow-xl p-6" >
<div class="text-center text-white uppercase">
<h1 class="font-extrabold text-6xl">XYZ HUB</h1>
<h1 class="text-3xl pt-8">Welcome Back</h1>
<h2 class="pb-8 text-xs">Enter your credentials below</h2>
</div>
<!-- form -->
<form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">
<div class="relative">
<label for="email" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Email</label>
<div class="">
<input id="email" type="email" class="pt-8 w-full rounded p-3 text-green-700" name="email" v-model="credentials.email" autocomplete="email" autofocus placeholder="your@email.com">
</div>
</div>
<div class="relative pt-6">
<label for="password" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Password</label>
<div class="">
<input id="password" type="password" class="pt-8 w-full rounded p-3 text-green-700" name="password" v-model="credentials.password" placeholder="password">
</div>
</div>
<div class="pt-8">
<button type="submit" class="uppercase font-bold rounded w-full bg-white text-green-400 py-2 px-3 text-2xl" v-on:click="loginUser">Login</button>
</div>
<div class="flex justify-center pt-8 text-white uppercase font-semibold text-sm">
<a :href="recover"> Forget Password </a>
</div>
<div class="flex justify-center pt-2 text-white uppercase font-semibold text-sm">
<a :href="register"> Register </a>
</div>
</form>
</div>
</div>
<script>
export default {
data(){
return {
credentials:{
email: '',
password: ''
}
}
},
methods:{
postNow(event){
console.log("event" , {event , $form: this.$refs.LoginForm});
axios.post('/api/login' , this.credentials)
.then( response => {
console.log ('response' , response.data); router.push("/register");
})
.catch( error => {
console.log("error", error.response);
})
}
}
}
我知道我在模板中的代码不正确只是不确定我做错了什么...任何帮助将不胜感激。提前致谢
更新: 因此,如果我删除表单中的阻止并在响应通过后添加重定向,那么突然之间我的提交没有通过
<form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">
改为
<form class="pb-8" @submit="postNow" ref="LoginForm" method="post">
你注意到我的端点也突然不正确了
真的对所有这些感到困惑...
您注意到端点是错误的。
所以改为:
axios.post('/login' , this.credentials)
改为
axios.post('/api/login' , this.credentials)
错误可能是由于路径“/login”中缺少 csrf 令牌字段。
使用 api 路由,您不需要 csrf。
此外,更容易记录错误响应,例如...
.catch( error => {
console.log(error.response);
})