在REST API中使用护照进行注册/登录流程

时间:2020-01-04 07:38:53

标签: laravel laravel-passport

我制作了@vue/cli/axios 4.0.5应用,并使用Laravel 6 Backend REST API和护照认证驱动程序读取了数据。 我想澄清注册/登录流程,因为除此之外,我还需要使用护照客户端令牌进行oauth2连接。

在注册方法中,我创建新用户,例如:

DB::beginTransaction();

$requestData= $request->all();
$newUser = new User();
$newUser->name= $requestData['name'];
$newUser->password= Hash::make($requestData['password']);
$newUser->status= 'A';
$newUser->email= $requestData['email'];
$newUser->save();

我是否还要使用在上面的行中创建的user_id创建oauth_clients行?喜欢:

const data = {
    name: 'Client Name',
    ‘user_id’ : $newUser.id, // CONNECT USER USER CREATED ABOVE
    redirect: 'http://example.com/callback'
};

axios.post('/oauth/clients', data)
    .then(response => {
        console.log(response.data);
    })
    .catch (response => {
        // List errors on response...
    });

由于我没有在命令行中创建新客户端

要登录,我必须使用此文档https://laravel.com/docs/6.x/passport#requesting-password-grant-tokens

请求令牌创建密码授予客户端后,您可以 可以通过向POST发出POST请求来请求访问令牌 / oauth / token路由以及用户的电子邮件地址和密码。 请记住,此路线已由Passport :: routes注册 方法,因此无需手动定义。如果请求是 成功后,您将在窗口中收到一个access_token和refresh_token 来自服务器的JSON响应:

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'taylor@laravel.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);

使用用户名和密码参数很明显,但是client_id和client_secret参数是什么? 跑步前有我

$response = $http->post('http://your-app.com/oauth/token'

要通过user_id获取oauth_clients(我需要为此表创建一个模型)行?

"laravel/framework": "^6.2",
"laravel/passport": "^8.1",

谢谢!

0 个答案:

没有答案