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

前端 未结 3 701
面向向阳花
面向向阳花 2020-12-01 21:57

I am trying to login using social sites in Laravel 5.1. I have installed socialite and following below tutorial:

https://mattstauffer.co/blog/using-github-authentica

3条回答
  •  情话喂你
    2020-12-01 22:28

    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.

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

    $user->getId() would be the unique id returned from Github.

    Save this field somewhere in your database. If you want to add them to your users table you could do something like this:

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

    Just remember, the ID returned is from Github not your 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.

提交回复
热议问题