如何使用API​​调用在Laravel上对Auth :: check()用户进行身份验证 [英] How to Auth::check() user on Laravel with API calls

查看:83
本文介绍了如何使用API​​调用在Laravel上对Auth :: check()用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在前端使用VueJS,因此我在前端代码中的某个地方调用了此API:

I'm using VueJS in my front-end, so I have this API call somewhere in the front-end code:

let products = axios.get('api/products');


然后在 routes/api.php 中:-

Routes::get('products', 'ProductsController@index');


ProductsController 的索引方法中:

public function index () {

    if ( Auth::check() ) {
        $user = Auth::user();
        return $user->products;
    }
    // return 'You don't have any products right now!';
}

索引方法将始终返回null,即使用户已登录!
那么,在这种情况下,如何在使用API​​调用时对用户进行身份验证?

The index method will always return null even if the user is logged-in!
So how to authenticate the users in this case while using the API calls?

推荐答案

您是否要发送auth令牌,因为在编写API时,我们需要处理auth令牌而不是Auth :: check().您需要在api调用时发送带有用户ID的身份验证令牌,并确认详细信息可以继续.

Are you sending auth token because when we write the API we need to deal with auth token and not with Auth::check(). You need to send auth token with user id at the time of api call and verify that details to proceed.

这篇关于如何使用API​​调用在Laravel上对Auth :: check()用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆