如何使用Passport获取Laravel API中的当前用户? [英] How to get current user in Laravel API using Passport?

查看:356
本文介绍了如何使用Passport获取Laravel API中的当前用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用护照包进行Laravel API身份验证.发布数据时出现错误:

I am using passport package for Laravel API Authentication. When I post data I get error:

401未经授权",错误":{错误":未经身份验证."}.

401 Unauthorized","error":{"error":"Unauthenticated."}.

我使用Auth::user()->id;获取当前用户ID.

I use Auth::user()->id; to get current user id.

如何解决此错误?

推荐答案

此代码对我有帮助:

auth()->guard('api')->user()

这篇关于如何使用Passport获取Laravel API中的当前用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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