Skip to content

Commit

Permalink
Done Social Login with Google
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Dec 17, 2020
1 parent 68bdcd1 commit 2fa8c51
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 2 deletions.
42 changes: 42 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

class LoginController extends Controller
{
Expand Down Expand Up @@ -37,4 +40,43 @@ 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()
{
$user = Socialite::driver('google')->user();

// Find User
$authUser = User::where('email', $user->email)->first();
if($authUser){
Auth::login($authUser);
return redirect()->route('home');
}
else{
$newUser = new User();
$newUser->email = $user->email;
$newUser->name = $user->name;
$newUser->userid = $user->id;
$newUser->password = uniqid(); // we dont need password for login
$newUser->save();

// Login
Auth::login($newUser);
return redirect()->route('home');
}

}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"guzzlehttp/guzzle": "^6.3",
"intervention/image": "^2.5",
"laravel/framework": "^7.0",
"laravel/socialite": "^5.1",
"laravel/tinker": "^2.0",
"laravel/ui": "^2.1"
},
Expand Down
141 changes: 139 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => 'http://localhost:8000/login/google/callback',
],

];
9 changes: 9 additions & 0 deletions resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@
</div>
</div>
</form>
<div class="row text-center">
<h5 class="mx-auto my-3">Or Login with</h5>

</div>
<div class="row d-flex justify-content-center space-between">
<a href="{{url('/login/google')}}" class="btn btn-outline-info m-2">Sing in with Google <i class="fab fa-google-plus" style="color: rgb(185, 57, 11);"></i></a><span class="text-dark font-weight-bold mt-3">

</div>

</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

Auth::routes();

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



Route::get('/', 'HomeController@index')->name('home');
Route::get('/posts', 'HomeController@posts')->name('posts');
Route::get('/post/{slug}', 'HomeController@post')->name('post');
Expand Down

0 comments on commit 2fa8c51

Please sign in to comment.