简体   繁体   English

Laravel 5 通过外部 API 对用户进行身份验证

[英]Laravel 5 authenticate users through external API

I'd like to know is it possible to extend the built-in authentication to use an external API to authenticate a user?我想知道是否可以扩展内置身份验证以使用外部 API 对用户进行身份验证? I'm a Laravel newbie, so I'd appreciate your help.我是 Laravel 新手,非常感谢您的帮助。 I'm making a custom app in Laravel 5.2 for my client, but I don't a direct access to their database server and I can only call their API to get users' details.我正在为我的客户在 Laravel 5.2 中制作一个自定义应用程序,但我不能直接访问他们的数据库服务器,我只能调用他们的 API 来获取用户的详细信息。

Thanks.谢谢。

If I understood correctly you want to log users from APIs like facebook, twitter or github for example?如果我理解正确的话,你想从 facebook、twitter 或 github 等 API 记录用户? If that's so you need to use a laravel package named Socialite, here is the link to download and use it: https://github.com/laravel/socialite如果是这样,您需要使用名为 Socialite 的 laravel 包,这里是下载和使用它的链接: https ://github.com/laravel/socialite

run on your command this:在你的命令上运行这个:

composer require laravel/socialite

Next you need to tell laravel you want to use this package, so you need to add this in config/app.php:接下来你需要告诉 laravel 你想使用这个包,所以你需要在 config/app.php 中添加:

'providers' => [
// Other service providers...

Laravel\Socialite\SocialiteServiceProvider::class,
],

and this is the aliases:这是别名:

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

Basically, you'll need to create an app on the developers site, i'll take facebook for this example.You need to go to this site: https://developers.facebook.com/ , create an account and you'll get your app url and secret key.基本上,您需要在开发者网站上创建一个应用程序,我将以 Facebook 为例。您需要访问此网站: https ://developers.facebook.com/,创建一个帐户,然后您将获取您的应用程序网址和密钥。 You'll use it on your.env and config/services files.您将在 your.env 和 config/services 文件中使用它。

In your config/services file add this after stripe:在您的 config/services 文件中,在 stripe 之后添加:

'facebook' => [
    'client_id' => env('FACEBOOK_ID'),
    'client_secret' => env('FACEBOOK_SECRET'),
    'redirect' => env('FACEBOOK_URL'),
],

And in your.env file:在你的 .env 文件中:

FACEBOOK_ID=*your facebook id*
FACEBOOK_SECRET=*your facebook secret*
FACEBOOK_URL=http://yourwebsite.com/callback

Next you'll need a controller to handle the auth process, create something like SocialAuthController and put this in:接下来,您需要一个控制器来处理身份验证过程,创建类似 SocialAuthController 的东西并将其放入:

public function redirect()
{
    return Socialite::driver('facebook')->redirect();
}

public function callback() {
    $user = $this->findOrCreateFbUser(Socialite::driver('facebook')->user());


    session([
        'user' => $user
    ]);
    return redirect()->route('/');

}

public function logout() {

    session()->forget('user');

    return redirect()->route('home');
}

protected function findOrCreateFbUser($fbUser) {
    // the data you want to get from facebook
    $fbData = [
        'facebook_id'   => $fbUser->id,
        'avatar'        => $fbUser->avatar,
        'username'      => $fbUser->name,
        'email'         => $fbUser->email,
    ];

    $user = \App\User::where('facebook_id', $fbData['facebook_id'])->first();

    if(!$user) $user = \App\User::create($fbData);

    $user->update([
        'avatar' => $fbUser->avatar,
        'username' => $fbUser->name,
        'email' => $fbUser->email
    ]);


    return $user;
}

Of course you need to add a facebook_id field in your user database and model.当然你需要在你的用户数据库和模型中添加一个 facebook_id 字段。 In User.php:在 User.php 中:

protected $fillable = [
    'facebook_id',
    'username',
    'email',
    'avatar'

];

I know this solution isn't really dynamic as it is for only one api, i'm still pretty new at Laravel too and this is my first answer to a stackoverflowquestion, but this did the trick for me:) If I forgot something don't hesitate to tell me so i can update this answer..我知道这个解决方案并不是真正动态的,因为它只适用于一个 api,我对 Laravel 还是很陌生,这是我对 stackoverflow 问题的第一个回答,但这对我有用:) 如果我忘记了什么,不要犹豫告诉我,这样我就可以更新这个答案..

I also suggest you follow Jeffrey Way's tutorial on social auth on the Laracasts website, it's very instructive and clear, i could manage it thanks to him !我还建议您遵循 Jeffrey Way 在 Laracasts 网站上关于社交身份验证的教程,它非常有指导意义且清晰,多亏了他,我才能做到!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM