Laravel - Creating API tokens to use internally for each user

℡╲_俬逩灬. 提交于 2019-12-31 07:12:10

问题


Every user of my application may use the API only within the application with Vue. The old api_token solution works for me, but it seems to be insecure since the api_token is the only thing that separates the user from the data.

I've read about Passport that uses OAuth2 methodology which is far more secure than a simple api_token.

Is there a way to use Passport to achieve this? Note that every time a user is created, I must create a API token to him.

We have no plans to open this API for external applications.


回答1:


you can create tokens for every user, have a look at below code.

// find specific user
$user = App\User::find(1);

// Creating a token without scopes...
$token = $user->createToken('Token Name')->accessToken;

// Creating a token with scopes...
$token = $user->createToken('My Token', ['place-orders'])->accessToken;

you can create token after user creation too, just add logic of token after a user has been created.



来源:https://stackoverflow.com/questions/44761398/laravel-creating-api-tokens-to-use-internally-for-each-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!