Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit bba8193

Browse files
committed
Enable socialite to login with social network
1 parent a83e30a commit bba8193

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

app/Http/Controllers/Auth/LoginController.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6+
use App\User;
67
use Illuminate\Foundation\Auth\AuthenticatesUsers;
8+
use Socialite;
9+
use Illuminate\Support\Facades\Auth;
710

811
class LoginController extends Controller
912
{
@@ -36,4 +39,74 @@ public function __construct()
3639
{
3740
$this->middleware('guest')->except('logout');
3841
}
42+
43+
/**
44+
* Redirect the user to the Provider authentication page.
45+
*
46+
* @return \Illuminate\Http\Response
47+
*/
48+
public function redirectToProvider($provider)
49+
{
50+
$provider = strtolower($provider);
51+
52+
if ($provider === 'facebook') {
53+
return Socialite::driver('facebook')->with(['auth_type' => 'rerequest'])->redirect();
54+
}
55+
56+
return Socialite::driver($provider)->redirect();
57+
}
58+
59+
/**
60+
* Obtain the user information from provider.
61+
*
62+
* @return \Illuminate\Http\Response
63+
*/
64+
public function handleProviderCallback($provider)
65+
{
66+
$providerUser = Socialite::driver($provider)->user();
67+
68+
// Check if user email is null
69+
// Get user from the database with the good provider_id or email
70+
if (is_null($providerUser->getEmail())) {
71+
$this->redirectToProvider($provider);
72+
}
73+
74+
$user = User::where($provider.'_id', '=', $providerUser->getId())
75+
->Where('email', '=', $providerUser->getEmail())
76+
->first();
77+
78+
if (is_null($user)) {
79+
// Save user to the database
80+
$userId = $this->registerUser($provider, $providerUser);
81+
Auth::loginUsingId($userId);
82+
83+
return redirect()->intended($this->redirectPath());
84+
}
85+
86+
// Login user
87+
Auth::loginUsingId($user->id);
88+
89+
return redirect()->intended($this->redirectPath());
90+
}
91+
92+
/**
93+
* Resgiter user to the database and return ID.
94+
*
95+
* @param $provider
96+
* @param $user
97+
* @return int
98+
*/
99+
public function registerUser($provider, $user)
100+
{
101+
$user = User::create([
102+
'name' => $user->getName(),
103+
'email' => $user->getEmail(),
104+
'password' => bcrypt('password'),
105+
$provider.'_id' => $user->getId(),
106+
'created_at' => Carbon::now(),
107+
'updated_at' => Carbon::now(),
108+
]);
109+
110+
return $user->id;
111+
}
39112
}

app/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class User extends \TCG\Voyager\Models\User
1515
* @var array
1616
*/
1717
protected $fillable = [
18-
'name', 'email', 'password',
18+
'name', 'email', 'password', 'phone_number', 'facebook_id', 'google_id', 'github_id', 'avatar', 'github_profile', 'twitter_profile'
1919
];
2020

2121
/**

resources/views/auth/login.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@
6767
<div class="col-1"></div>
6868
<div class="col col-md-5">
6969
<h5 class="social-title">{{ __('Login using social network') }}</h5>
70-
<a href="javascript:;" class="btn btn-social btn-block btn-facebook">
70+
<a href="{{ url('/auth/facebook') }}" class="btn btn-social btn-block btn-facebook">
7171
<span class="fa fa-facebook"></span> {{ __('Login with Facebook') }}
7272
</a>
73-
<a href="javascript:;" class="btn btn-social btn-block btn-github">
73+
<a href="{{ url('/auth/github') }}" class="btn btn-social btn-block btn-github">
7474
<span class="fa fa-github"></span> {{ __('Login with Github') }}
7575
</a>
76-
<a href="javascript:;" class="btn btn-social btn-block btn-google">
76+
<a href="{{ url('/auth/google') }}" class="btn btn-social btn-block btn-google">
7777
<span class="fa fa-google"></span> {{ __('Login with Google') }}
7878
</a>
7979
</div>

resources/views/auth/register.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@
7070
<div class="col-1"></div>
7171
<div class="col col-md-5">
7272
<h5 class="social-title">{{ __('Register using social network') }}</h5>
73-
<a href="javascript:;" class="btn btn-social btn-block btn-facebook">
73+
<a href="{{ url('/auth/facebook') }}" class="btn btn-social btn-block btn-facebook">
7474
<span class="fa fa-facebook"></span> {{ __('Register with Facebook') }}
7575
</a>
76-
<a href="javascript:;" class="btn btn-social btn-block btn-github">
76+
<a href="{{ url('/auth/github') }}" class="btn btn-social btn-block btn-github">
7777
<span class="fa fa-github"></span> {{ __('Register with Github') }}
7878
</a>
79-
<a href="javascript:;" class="btn btn-social btn-block btn-google">
79+
<a href="{{ url('/auth/google') }}" class="btn btn-social btn-block btn-google">
8080
<span class="fa fa-google"></span> {{ __('Register with Google') }}
8181
</a>
8282
</div>

routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
{
1616
/** Authenticate Route */
1717
Auth::routes();
18+
Route::get('/auth/{provider}', 'Auth\LoginController@redirectToProvider');
19+
Route::get('/callback/{provider}', 'Auth\LoginController@handleProviderCallback');
1820
/** Profile Route */
1921
Route::group(['middleware' => 'auth'], function () {
2022
Route::get('/account', 'UserController@account')->name('users.account');

0 commit comments

Comments
 (0)