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 important because nowadays most of the users will connected with social network like twitter. so in this tutorial i will show you how to login with twitter 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 Google App :
n this step we need twitter Credentials like twitter client id, secret key and redirect URls. so, go to https://dev.twitter.com/rest/public and create twitter 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 twitter client id, secret and redirect URls.
return [....'twitter' => ['client_id' => 'app id','client_secret' => 'add secret','redirect' => 'http://localhost:8000/auth/twitter/callback',],]
5. Add twitter_id column in your users table :
Schema::table('users', function ($table) {$table->string('twitter_id');});
6. Add route :
After adding twitter_id column in your users table first we have to add new route for twitter login. so, add below route in routes.php file.
});
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 redirectToTwitter() { return Socialite::driver('twitter')->redirect(); } public function handleTwitterCallback() { try { $user = Socialite::driver('twitter')->user(); $userModel = new User; $createdUser = $userModel->addNew($user); Auth::loginUsingId($createdUser->id); return redirect()->route('home'); } catch (Exception $e) { return redirect('auth/facebook'); } } }
8. Login.blade.php
resources/views/auth/login.blade.php
0 comments:
Post a Comment