https://scotch.io/tutorials/laravel-social-authentication-with-socialite
Users migration:
Users migration:
Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password')->nullable(); $table->string('provider_id')->nullable(); $table->string('provider')->nullable(); $table->rememberToken(); $table->timestamps();});
app.php:
aliasis:
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Providers:
Laravel\Socialite\SocialiteServiceProvider::class,
login Controller:
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller;use App\User;use Illuminate\Foundation\Auth\AuthenticatesUsers;use Illuminate\Support\Facades\Auth;use Laravel\Socialite\Facades\Socialite; class LoginController extends Controller{ /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } public function redirect($provider) { return Socialite::driver($provider)->redirect(); } /** * Return a callback method from facebook api. * * @return callback URL from facebook */ public function Callback($provider) { $user=Socialite::driver($provider)->user(); $authUser=$this->findOrCreateUser($user,$provider); Auth::login($authUser,true);// auth()->login($user); return redirect()->to($this->redirectTo); } public function findOrCreateUser($user, $provider){ $authUser = User::where('provider_id', $user->id)->first();if ($authUser) { return $authUser;}return User::create(['name'=> $user->name,'email' => $user->email,'provider' => $provider,'provider_id' => $user->id]); }}
Services.php:
<?php return [ /* |-------------------------------------------------------------------------- | Third Party Services |-------------------------------------------------------------------------- | | This file is for storing the credentials for third party services such | as Stripe, Mailgun, SparkPost and others. This file provides a sane | default location for this type of information, allowing packages | to have a conventional place to find your various credentials. | */ 'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), ], 'ses' => [ 'key' => env('SES_KEY'), 'secret' => env('SES_SECRET'), 'region' => env('SES_REGION', 'us-east-1'), ], 'mandrill' => [ 'secret' => env('MANDRILL_KEY'), ], 'sparkpost' => [ 'secret' => env('SPARKPOST_SECRET'), ], 'stripe' => [ 'model' => App\User::class, 'key' => env('STRIPE_KEY'), 'secret' => env('STRIPE_SECRET'), ], 'facebook' => [ 'client_id' => '252819208792591', 'client_secret' => '9f12f88ba0ee05c00e651785f6914e91', 'redirect' => 'http://localhost:8000/auth/facebook/callback', ], 'google' => [ 'client_id' => '123877408113-fr7t5hbp2sfg314euqhcfb21s069lb30.apps.googleusercontent.com', 'client_secret' => 'qAbKPjj-LwZQO-9Xy8evzyzD', 'redirect' => 'http://localhost:8000/auth/google/callback', ], 'twitter' => [ 'client_id' => '728751140040-5v69at12podrmuml4tqsukul23qgde41.apps.googleusercontent.com', 'client_secret' => 'AIzaSyAuxPfJSS_ivBpBiLKLPAsCN2Lyqd3CDS0', 'redirect' => 'http://localhost:8000/auth/twitter/callback', ], ];
web.php:
Auth::routes();Route::get('auth/{provider}', 'Auth\LoginController@redirect');Route::get('auth/{provider}/callback', 'Auth\LoginController@callback');
Login.blade
{{--@extends('layouts.app') @section('content')<div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Login') }}</div> <div class="card-body"> <form method="POST" action="{{ route('login') }}"> @csrf <div class="form-group row"> <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus> @if ($errors->has('email')) <span class="invalid-feedback"> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> </div> <div class="form-group row"> <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required> @if ($errors->has('password')) <span class="invalid-feedback"> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div class="form-group row"> <div class="col-md-6 offset-md-4"> <div class="checkbox"> <label> <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> {{ __('Remember Me') }} </label> </div> </div> </div> <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button> <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> </div> </div> </form> </div> </div> </div> </div></div>@endsection--}} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Nagar Project | Log in</title> <!-- Tell the browser to be responsive to screen width --> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> <!-- Bootstrap 3.3.6 --> <link rel="stylesheet" href="{{asset('assets/bootstrap/css/bootstrap.min.css')}}"> <!-- Font Awesome --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css"> <!-- Ionicons --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css"> <!-- Theme style --> <link rel="stylesheet" href="{{asset('assets/dist/css/AdminLTE.min.css')}}"> <!-- iCheck --> <link rel="stylesheet" href="{{asset('assets/plugins/iCheck/square/blue.css')}}"> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--></head> <body class="hold-transition login-page"> <div class="login-box"> <div class="login-logo"> <a href="{{route('login')}}"><b>Nagar </b>Project</a> </div> <!-- /.login-logo --> <div class="login-box-body"> <p class="login-box-msg">Sign in to start your session</p> <form class="form-horizontal" method="POST" action="{{ route('login') }}"> {{ csrf_field() }} <div class="form-group has-feedback"> <input id="email" type="email" class="form-control" placeholder="Email" name="email" value="{{ old('email') }}" required autofocus> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> @if ($errors->has('email')) <span class="help-block"> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> <div class="form-group has-feedback"> <input id="password" type="password" class="form-control" name="password" placeholder="Password" required> <span class="glyphicon glyphicon-lock form-control-feedback"></span> @if ($errors->has('password')) <span class="help-block"> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> <div class="row"> <div class="col-xs-8"> <div class="checkbox icheck"> <label> <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me </label> </div> </div> <!-- /.col --> <div class="col-xs-4"> <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button> </div> <!-- /.col --> </div> <br /> <p style="margin-left:265px">OR</p> <br /> <hr> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <a href="{{ url('/auth/google') }}" class="btn btn-google"><i class="fa fa-google"></i> google</a> <a href="{{ url('/auth/twitter') }}" class="btn btn-twitter"><i class="fa fa-twitter"></i> Twitter</a> <a href="{{ url('/auth/facebook') }}" class="btn btn-facebook"><i class="fa fa-facebook"></i> Facebook</a> </div> </div> </form> <!-- /.social-auth-links --> <a href="{{ route('password.request')}}">Forgot Your Password?</a> <br> <a href="{{route('register')}}" class="text-center">Register a new membership</a> </div> <!-- /.login-box-body --></div> <!-- /.login-box --> <!-- jQuery 2.2.3 --><script src="{{asset('assets/plugins/jQuery/jquery-2.2.3.min.js')}}"></script> <!-- Bootstrap 3.3.6 --><script src="{{asset('assets/bootstrap/js/bootstrap.min.js')}}"></script> <!-- iCheck --><script src="{{asset('assets/plugins/iCheck/icheck.min.js')}}"></script> <script> $(function () { $('input').iCheck({ checkboxClass: 'icheckbox_square-blue', radioClass: 'iradio_square-blue', increaseArea: '20%' // optional }); }); </script> </body> </html>
Comments
Post a Comment