我有几个守卫的会话登录系统。我经常使用Vue,为了正确地获取和发布数据,我需要在Vue中进行身份验证。问题是如何让经过身份验证的会话用户使用API。因此,在api.php中,我想使用一个控制器,它的中间件是经过身份验证的用户。我不想使用Passport,因为我只在网页上登录,而没有API。
发布于 2018-06-12 17:25:11
vue支持组件还有一个叫做props的东西,它们是你可以传递给你的vue组件的数据,我通常要做的就是把经过身份验证的用户id传递给我的vue组件,然后当我从vue组件发出一个请求时,我会把当前经过身份验证的用户传递给后端,在那里我会检查通过请求接收到的id是否与当前经过身份验证的用户相同。
检查下面的示例,我将使用常规的守卫
正在从刀片加载vue组件
//loading vue test-component and pass the authenticated user
<test-component :authuser="{{Auth::(user)->id}}"></test-component>vue组件
<script>
export default {
props : ['authuser'], //should be the same name as you passed it
data(){
return {
}
},
created(){
axios.post('/api/test' , {
'authuser' : this.authuser
})
.then(res => {
console.log(res);
})
.catch(err => {
});
}
}Api路由
use Auth;
Route::post('api/test' , function($request){
if(Auth::user()->id == $request->authuser)
return 'you are authenticated';
else
return 'you are not authenticated';
});希望这篇文章对你有帮助,祝你好运。
https://stackoverflow.com/questions/50811766
复制相似问题