简体   繁体   English

如何在Laravel 5.1中使用Github,Facebook,Gmail和Twitter登录?

[英]How to login using Github, Facebook, Gmail and Twitter in Laravel 5.1?

I am trying to login using social sites in Laravel 5.1. 我正在尝试使用Laravel 5.1中的社交网站登录。 I have installed socialite and following below tutorial: 我已经安装了社交网站并遵循以下教程:

https://mattstauffer.co/blog/using-github-authentication-for-login-with-laravel-socialite https://mattstauffer.co/blog/using-github-authentication-for-login-with-laravel-socialite

http://tutsnare.com/social-authentication-in-laravel-5/ http://tutsnare.com/social-authentication-in-laravel-5/

I have followed every step, when try to login using Github, it redirect to Github and after login it redirect to URL callback, but my problem is it won't store user data in my database. 我已经按照每一步,当尝试使用Github登录时,它重定向到Github并在登录后重定向到URL回调,但我的问题是它不会将用户数据存储在我的数据库中。

Update-1 更新1

Now my problem is how to authenticate user using social network. 现在我的问题是如何使用社交网络验证用户。 Following is my code but it throw an error saying undefined index password. 以下是我的代码,但它会抛出一个错误,说明未定义的索引密码。 That error because Auth::attempt() has passsword parameter. 该错误,因为Auth::attempt()具有passsword参数。

if (Auth::attempt(['email' => $email, 'user_id' => $user_id])) {
  return redirect()->intended('user/UserDashboard');
} else {
  here i am going to insert if user not logged in already
}

Update-2 更新2

After some research i realized that i need to create Custom Authentication Drivers. 经过一些研究,我意识到我需要创建自定义身份验证驱动程序。 If any one have idea please post your answers 如果有任何想法请发表您的答案

same question asked one year ago 一年前提出同样的问题

laravel login with google account laravel使用谷歌帐户登录

Well the very first thing that I assume is that you have ran php artisan migrate (And that may be your main problem). 那么我假设的第一件事就是你运行了php artisan migrate (这可能是你的主要问题)。

Next the problem that you have while storing data to database, do you have a working connection to your database from that Class \\App\\User ? 接下来在将数据存储到数据库时遇到的问题,您是否从该Class \\App\\User连接到数据库? If you have named it something different I request you to change that part after the first \\App\\[YourClassName] . 如果你已经命名了不同的东西,我请求你在第一个\\App\\[YourClassName]之后更改那个部分。

Alright if that's working properly, you might have problem with getting data back. 好吧,如果它正常工作,你可能会遇到重新获取数据的问题。 Just try doing somewhat like this in some different route: 试着在某种不同的路线上尝试这样做:

<?php
    //Routes.php

    Route::get('trygithub', function(){
        $user = Socialize::with('github')->user()
        dd($user);
    });

And if you get some output on the screen, well that's OK. 如果你在屏幕上得到一些输出,那就没关系。 If you do not get anything, try to check your github configurations. 如果您没有得到任何东西,请尝试检查您的github配置。

And yeah just one more thing... Still if you do not get desired output, try to comment below. 还有一件事......如果你没有获得所需的输出,请尝试在下面评论。 But I am pretty much sure that this ones will definitely work. 但我非常肯定这些肯定会奏效。

Update-1 answer : Update-1答案

If you want to attempt login for your user, and you are using any social profile like let's say FB, you need to go like this: 如果您想尝试为您的用户登录,并且您正在使用任何社交个人资料,例如让我们说FB,您需要这样: https://maxoffsky.com/word/wp-content/uploads/2013/08/Social-Logon.png

And that's not just one case of FB, you get some specific codes like in Google, you get one refresh token code and in twitter a permanent access token and secret. 而且这不仅仅是FB的一个案例,你得到一些像谷歌这样的特定代码,你得到一个刷新令牌代码,在twitter中有一个永久访问令牌和秘密。 So just try to go like that. 所以试着去尝试。 Or, have one specific term as a password for your user. 或者,将一个特定术语作为用户的密码。 str_random() may be helpful in that way and I strongly recommend that instead of these two. str_random()可能会有所帮助,我强烈建议而不是这两个。

Update-2 answer : 更新-2回答

I do not recommend going through this because you will not get anything by reinventing the wheel. 我不建议通过这个,因为你不会通过重新发明轮子得到任何东西。 But still if you want to go through that nasty process, here 's the link. 但是如果你想要经历那个令人讨厌的过程,那么就是链接。

http://laravel.com/docs/5.1/authentication#adding-custom-authentication-drivers http://laravel.com/docs/5.1/authentication#adding-custom-authentication-drivers

It's up to you to manage that. 这取决于你管理它。

You can find an interesting discussion that shows a few ways to do what you need. 你可以找到一个有趣的讨论 ,展示一些你需要的方法。

Once you get redirected from Github you should be able to access the details like so. 一旦你从Github重定向,你应该能够访问这样的细节。

 $user->getId();
 $user->getNickname();
 $user->getName();
 $user->getEmail();
 $user->getAvatar();`

$user->getId() would be the unique id returned from Github. $user->getId()将是从Github返回的唯一ID。

Save this field somewhere in your database. 将此字段保存在数据库中的某个位置。 If you want to add them to your users table you could do something like this: 如果要将它们添加到users表,您可以执行以下操作:

$newUser = new \App\User;
$newUser->name=$user->getName();
//Or use the nickname
//$newUser->name=$user->getNickname();
$newUser->email=$user->getEmail();
$newUser->social_id=$user->getId();
$newUser->save();

I'm not recommending this as the way to do it, but it should give you enough of an example to make it work. 我不建议这样做,但它应该给你足够的一个例子来使它工作。

When someone tries to login with Github, if that ID doesn't exist, either create a new account for them or throw an error. 当有人尝试使用Github登录时,如果该ID不存在,则为其创建新帐户或抛出错误。

Just remember, the ID returned is from Github not your webapp. 请记住,返回的ID来自Github而不是您的webapp。 So your user would have an "id" for your web app and a "github_id" that you store so you know what user they belong to. 因此,您的用户将拥有您的Web应用程序的“id”和您存储的“github_id”,以便您知道他们属于哪个用户。

The idea is to keep a table for each user's connected accounts. 我们的想法是为每个用户的连接帐户保留一个表格。 The table will contain the user id , social account type and social account id . 该表将包含user idsocial account typesocial account id

User => has Many "social accounts" 用户=>有很多“社交帐号”

social account => has one "User" 社交帐户=>有一个“用户”

When a user logs in with a social account we will search for the social account with the social account id and type . 当用户使用社交帐户登录时,我们将搜索具有social account idtypesocial account id get the user object and login the user immediately. 获取用户对象并立即登录用户。

If a user is not found and if the social network api returns the user's email, we will get the user by that email and login the user, also connect the new social account to the user object. 如果找不到用户并且社交网络api返回用户的电子邮件,我们将通过该email获取用户并登录用户,还将新的社交帐户连接到用户对象。 However i do not recommend this, because it is a security issue, if someone changed their social account email to an existing user's email in your app, he can gain access to the account easily. 但是我不建议这样做,因为这是一个安全问题,如果有人将他们的社交帐户电子邮件更改为您应用中的现有用户的电子邮件,他可以轻松访问该帐户。

If all above fails we need to direct the user to the signup screen, with the email we already have from the social network api (if available) and the social account type , that if we need to connect after successful signup. 如果以上都失败了,我们需要将用户引导到注册屏幕,我们已经从社交网络API(如果可用)和social account type获得了email ,如果我们需要在成功注册后进行连接。

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

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