Laravel 5 通过外部 API 对用户进行身份验证 [英] Laravel 5 authenticate users through external API

查看:33
本文介绍了Laravel 5 通过外部 API 对用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以扩展内置身份验证以使用外部 API 来对用户进行身份验证?我是 Laravel 新手,所以我很感激你的帮助.我正在为我的客户在 Laravel 5.2 中制作一个自定义应用程序,但我不能直接访问他们的数据库服务器,我只能调用他们的 API 来获取用户的详细信息.

I'd like to know is it possible to extend the built-in authentication to use an external API to authenticate a user? I'm a Laravel newbie, so I'd appreciate your help. 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.

谢谢.

推荐答案

如果我理解正确,您想从 facebook、twitter 或 github 等 API 登录用户?如果是这样,您需要使用名为 Socialite 的 laravel 包,这里是下载和使用它的链接:https://github.com/laravel/socialite

If I understood correctly you want to log users from APIs like facebook, twitter or github for example ? 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

按照您的命令运行:

composer require laravel/socialite

接下来需要告诉laravel你要使用这个包,所以需要在config/app.php中添加:

Next you need to tell laravel you want to use this package, so you need to add this in config/app.php :

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

Laravel\Socialite\SocialiteServiceProvider::class,
],

这是别名:

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

基本上,您需要在开发者网站上创建一个应用程序,我将以 facebook 为例.您需要访问此网站:https://developers.facebook.com/,创建一个帐户,您将获得您的应用网址和密钥.您将在 .env 和 config/services 文件中使用它.

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. You'll use it on your .env and config/services files.

在您的 config/services 文件中,在 stripe 后添加:

In your config/services file add this after stripe :

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

在你的 .env 文件中:

And in your .env file :

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

接下来您需要一个控制器来处理身份验证过程,创建类似 SocialAuthController 的东西并将其放入:

Next you'll need a controller to handle the auth process, create something like SocialAuthController and put this in :

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;
}

当然,您需要在用户数据库和模型中添加 facebook_id 字段.在 User.php 中:

Of course you need to add a facebook_id field in your user database and model. In User.php :

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

];

我知道这个解决方案并不是真正动态的,因为它只适用于一个 api,我在 Laravel 还是个新手,这是我对 stackoverflowquestion 的第一个答案,但这对我来说是成功的:) 如果我忘记了一些事情,请随时告诉我,以便我可以更新此答案..

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..

我还建议您在 Laracasts 网站上关注 Jeffrey Way 关于社交身份验证的教程,它非常有启发性且清晰,多亏他,我才能搞定!

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 !

这篇关于Laravel 5 通过外部 API 对用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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