Laravel Passport - 创建用户时自动创建令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47459797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Passport - Automatically create token when a user is created
提问by fightstarr20
I have been looking into using Passport to allow api access to my Laravel App. I have successfully implemented it using the standard tutorial from the Laravel docs.
我一直在研究使用 Passport 来允许 api 访问我的 Laravel 应用程序。我已经使用 Laravel 文档中的标准教程成功实现了它。
I don't want an oauth management screen as described in the tutorial, I just want to be able to register a user using the App and have it automatically create the tokens it needs for that user.
我不想要教程中描述的 oauth 管理屏幕,我只想能够使用该应用程序注册用户并让它自动为该用户创建所需的令牌。
Is this possible and does anybody have an example they can point me at?
这可能吗,有没有人可以举个例子?
回答by Webinion
$tokenStr
will give you the new generated token for that specific user.
$tokenStr
将为您提供该特定用户的新生成的令牌。
$newUser = User::create();
$userObj = User::find($newUser->id);
$tokenStr = $userObj->createToken('Token Name')->accessToken;
// or try this.
$newUser = User::create();
$tokenStr = $newUser->createToken('Token Name')->accessToken;