Skip to content

Instantly share code, notes, and snippets.

@heinthanth
Last active July 21, 2019 08:35
Show Gist options
  • Save heinthanth/71c41f4e37ccd3fc9e07229fdec14ee8 to your computer and use it in GitHub Desktop.
Save heinthanth/71c41f4e37ccd3fc9e07229fdec14ee8 to your computer and use it in GitHub Desktop.
SocialLoginController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Socialite;
class SocialLoginController extends Controller
{
public function redirect($provider) {
return Socialite::driver($provider)->redirect();
}
public function callback(Request $request, $provider) {
if(!$request->has('code')) {
return redirect('/login');
}
try {
session()->put('state', $request->input('state'));
$info = Socialite::driver($provider)->user();
$user = User::where('provider_id', $info->id)->first();
if(!$user) {
$user = User::create([
'name' => $info->name,
'email' => $info->email,
'provider' => $provider,
'provider_id' => $info->id
]);
}
auth()->login($user);
return redirect('/home');
} catch(Exception $e) {
return $e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment