BTemplates.com

Advertisement

YoGiTeCh....... Powered by Blogger.

About Me

My photo
Mumbai, Maharastra, India

How to login with twitter in laravel 5.4

In this Tutorial will show you how to user login with twitter using laravel/socialite packages. social authentication is very importan...

Contact Form

Name

Email *

Message *

Search This Blog

Subscribe

Translate

Recent Posts

Posts

Pages

Blogger templates

Popular Posts

Thursday, 30 March 2017

How to login with facebook in laravel 5.4



In this Tutorial will show you how to user login with facebook using laravel/socialite packages. social authentication is very important because nowadays most of the users will connected with social network like facebook. so in this tutorial i will show you how to login with facebook in laravel 5.4.

1. Install Socialite Package:
In first step install socialite package. so, first open command prompt and run below command :

composer require laravel/socialite
2. Add Providers and aliases :
After install socialite package we should add providers and aliases in config file. so, open config/app.php file and add providers and aliases.
'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
3.  Create Facebook App :
In this step we need facebook Credentials like facebook client id, secret key and redirect URls. so, go to https://developers.facebook.com/ and create facebook app.

4. Config/services.php :
After create app you can copy credentials like client id, secret key and redirect URls.
Now, go to config/services.php file and set facebook client id, secret and redirect URls.

return [
....
'facebook' => [
'client_id' => 'app id',
'client_secret' => 'add secret',
'redirect' => 'http://localhost:8000/auth/facebook/callback',
],
]
5. Add facebook_id column in your users table :

Schema::table('users', function ($table) {
$table->string('facebook_id');
});

6. Add route :
After adding facebook_id column in your users table first we have to add new route for facebook login. so, add below route in routes.php file.
Route::get('facebook', function () {
return view('facebookAuth');
});
Route::get('auth/facebook', 'Auth\RegisterController@redirectToFacebook');
Route::get('auth/facebook/callback', 'Auth\RegisterController@handleFacebookCallback');
7. RegisterController.php

app/Http/Controllers/Auth/RegisterController.php

<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; use Socialite; use Auth; use Exception; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } public function redirectToFacebook() { return Socialite::driver('facebook')->redirect(); } public function handleFacebookCallback() { try { $user = Socialite::driver('facebook')->user(); $userModel = new User; $createdUser = $userModel->addNew($user); Auth::loginUsingId($createdUser->id); return redirect()->route('home'); } catch (Exception $e) { return redirect('auth/google'); } } }
8. Login.blade.php

resources/views/auth/login.blade.php

0 comments:

Post a Comment