Skip to content

Commit

Permalink
Added linkedin and google login
Browse files Browse the repository at this point in the history
  • Loading branch information
cool1209 committed Feb 19, 2018
1 parent f34cbd9 commit 874bcfd
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 1 deletion.
92 changes: 92 additions & 0 deletions app/Http/Controllers/Auth/GoogleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
use App\User;
use Illuminate\Support\Facades\Auth;
class GoogleController 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');
}

/**
* Redirect the user to the GitHub authentication page.
*
* @return \Illuminate\Http\Response
*/
public function redirectToProvider()
{
return Socialite::driver('google')->redirect();
}

/**
* Obtain the user information from GitHub.
*
* @return \Illuminate\Http\Response
*/
public function handleProviderCallback()
{
$userSocial = Socialite::driver('google')->user();


//check if user exists and log user in

$user = User::where('email', $userSocial->email)->first();
if($user){
if(Auth::loginUsingId($user->id)){
return redirect()->route('home');
}
}

//else sign the user up
$userSignup = User::create([
'name' => $userSocial->name,
'email' => $userSocial->email,
'password' => bcrypt('1234'),
'avatar'=> $userSocial->avatar,
'github_profile'=> $userSocial->user['url'],
'gender'=> $userSocial->user['gender']
]);


//finally log the user in
if($userSignup){
if(Auth::loginUsingId($userSignup->id)){
return redirect()->route('home');
}
}

}


}
91 changes: 91 additions & 0 deletions app/Http/Controllers/Auth/LinkedinController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
use App\User;
use Illuminate\Support\Facades\Auth;
class LinkedinController 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');
}

/**
* Redirect the user to the GitHub authentication page.
*
* @return \Illuminate\Http\Response
*/
public function redirectToProvider()
{
return Socialite::driver('linkedin')->redirect();
}

/**
* Obtain the user information from GitHub.
*
* @return \Illuminate\Http\Response
*/
public function handleProviderCallback()
{
$userSocial = Socialite::driver('linkedin')->user();


//check if user exists and log user in

$user = User::where('email', $userSocial->email)->first();
if($user){
if(Auth::loginUsingId($user->id)){
return redirect()->route('home');
}
}

//else sign the user up
$userSignup = User::create([
'name' => $userSocial->name,
'email' => $userSocial->email,
'password' => bcrypt('1234'),
'avatar'=> $userSocial->avatar,
'github_profile'=> $userSocial->user['link']
]);


//finally log the user in
if($userSignup){
if(Auth::loginUsingId($userSignup->id)){
return redirect()->route('home');
}
}

}


}
10 changes: 10 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,15 @@
'client_secret' => '6a1dd77e3c948e4f0f929fc5a85ee0ff318d5aa1', // Your github Client Secret
'redirect' => 'http://localhost:8000/login/github/callback',
],
'linkedin' => [
'client_id' => '77gpcnslk4r1nm', // Your linkedin Client ID
'client_secret' => 'PZd9r8QidEQxP9FW', // Your linkedin Client Secret
'redirect' => 'http://localhost:8000/login/linkedin/callback',
],
'google' => [
'client_id' => '366049614781-dsrvkl028r45n77u5p9diqm709vpaok5.apps.googleusercontent.com', // Your google Client ID
'client_secret' => 'WMmQNVvGohVMvpIx7o-doI2X', // Your google Client Secret
'redirect' => 'http://localhost:8000/login/google/callback',
],

];
18 changes: 18 additions & 0 deletions resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@
</div>
</div>

<div class="form-group row mb-0" style="margin-top: 10px;" >
<div class="col-md-8 offset-md-4">
<a href="/login/linkedin" class="btn btn-primary" >
<i class="fab fa-linkedin"></i> Sign in with Linkedin</a>

</div>
</div>


<div class="form-group row mb-0" style="margin-top: 10px;" >
<div class="col-md-8 offset-md-4">
<a href="/login/google" class="btn btn-primary" >
<i class="fab fa-google"></i> Sign in with Google</a>

</div>
</div>




</div>
Expand Down
9 changes: 8 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
Route::get('login/twitter/callback', 'Auth\TwitterController@handleProviderCallback');
//github
Route::get('login/github', 'Auth\GithubController@redirectToProvider');
Route::get('login/github/callback', 'Auth\GithubController@handleProviderCallback');
Route::get('login/github/callback', 'Auth\GithubController@handleProviderCallback');
//linkedin
Route::get('login/linkedin', 'Auth\LinkedinController@redirectToProvider');
Route::get('login/linkedin/callback', 'Auth\LinkedinController@handleProviderCallback');

//google
Route::get('login/google', 'Auth\GoogleController@redirectToProvider');
Route::get('login/google/callback', 'Auth\GoogleController@handleProviderCallback');

0 comments on commit 874bcfd

Please sign in to comment.