Laravel 5 and Socialite, how to save to the database and log in

After reading: http://laravel.com/docs/5.0/authentication I managed to get user information from the OAuth provider (Facebook) using Socialite:

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

But I could not find additional documentation on how to save the user in the database or register the user.

I want to make an equivalent:

 Auth::attempt(['email' => $email, 'password' => $password]) 

But for details received through Socialite (I don't have a password)

Can you show me an example of using "Auth" with user data obtained through Socialite?

+5
source share
1 answer

Add a new column to facebook’s user column: facebook_user_id . Each time a user tries to log in via Facebook, Facebook will return the same user ID.

 public function handleProviderCallback($provider) { $socialize_user = Socialize::with($provider)->user(); $facebook_user_id = $socialize_user->getId(); // unique facebook user id $user = User::where('facebook_user_id', $facebook_user_id)->first(); // register (if no user) if (!$user) { $user = new User; $user->facebook_id = $facebook_user_id; $user->save(); } // login Auth::loginUsingId($user->id); return redirect('/'); } 

How does Laravel Socialite work?

 public function redirectToProvider() { // 1. with this method you redirect user to facebook, twitter... to get permission to use user data return Socialize::with('github')->redirect(); } public function handleProviderCallback() { // 2. facebook, twitter... redirects user here, where you write code to log in user $user = Socialize::with('github')->user(); } 
+14
source

Source: https://habr.com/ru/post/1213206/


All Articles