2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
use App\Facade\Theme;
|
2018-08-14 12:57:41 +01:00
|
|
|
use App\Facade\UserConfig;
|
2016-09-01 16:23:39 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-08-13 22:03:12 +01:00
|
|
|
use App\User;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
2016-09-06 12:23:14 +01:00
|
|
|
use Illuminate\Http\Request;
|
2018-08-14 12:57:41 +01:00
|
|
|
use Laravel\Socialite\Two\FacebookProvider;
|
2018-08-13 14:25:56 +01:00
|
|
|
use Socialite;
|
2016-09-01 16:23:39 +01:00
|
|
|
|
|
|
|
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 / registration.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $redirectTo = '/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('guest', ['except' => 'logout']);
|
|
|
|
}
|
2016-09-02 10:42:05 +01:00
|
|
|
|
2018-08-15 14:22:13 +01:00
|
|
|
protected function attemptLogin(Request $request)
|
|
|
|
{
|
|
|
|
$isSuccessful = $this->guard()->attempt($this->credentials($request));
|
|
|
|
|
|
|
|
if ($isSuccessful)
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
|
|
|
$user = $this->guard()->user();
|
|
|
|
|
|
|
|
// Update the social media ID if successful login and it was referred by the SSO provider
|
|
|
|
$loginData = $request->getSession()->get('ssoLoginData');
|
|
|
|
if (!is_null($loginData))
|
|
|
|
{
|
|
|
|
unset($loginData['name']);
|
|
|
|
unset($loginData['email']);
|
|
|
|
$user->fill($loginData);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$request->getSession()->remove('ssoLoginData');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $isSuccessful;
|
|
|
|
}
|
|
|
|
|
2016-09-06 12:23:14 +01:00
|
|
|
protected function credentials(Request $request)
|
|
|
|
{
|
|
|
|
$result = $request->only($this->username(), 'password');
|
|
|
|
|
|
|
|
// Only allow activated users to login
|
|
|
|
$result['is_activated'] = true;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:42:05 +01:00
|
|
|
/**
|
|
|
|
* Show the application's login form.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2016-09-06 14:45:51 +01:00
|
|
|
public function showLoginForm(Request $request)
|
2016-09-02 10:42:05 +01:00
|
|
|
{
|
2017-03-23 04:29:29 +00:00
|
|
|
return Theme::render('auth.v2_unified', [
|
|
|
|
'active_tab' => 'login',
|
2018-08-15 14:22:13 +01:00
|
|
|
'info' => $request->session()->get('info'),
|
|
|
|
'is_sso' => false
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the application's login form (for a social media-linked account).
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function showLoginFormSso(Request $request)
|
|
|
|
{
|
|
|
|
// Social media login info
|
|
|
|
$loginData = $request->getSession()->get('ssoLoginData');
|
|
|
|
if (is_null($loginData))
|
|
|
|
{
|
|
|
|
// No SSO data in session, use the normal login screen
|
|
|
|
return redirect(route('login'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Theme::render('auth.v2_unified', [
|
|
|
|
'active_tab' => 'login',
|
|
|
|
'info' => $request->session()->get('info'),
|
|
|
|
'is_sso' => true,
|
|
|
|
'login_data' => $loginData
|
2016-09-06 14:45:51 +01:00
|
|
|
]);
|
2016-09-02 10:42:05 +01:00
|
|
|
}
|
2018-08-13 14:25:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect the user to the Facebook authentication page.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function redirectToFacebook()
|
|
|
|
{
|
2018-08-14 12:57:41 +01:00
|
|
|
$socialite = $this->setSocialiteConfigs();
|
|
|
|
return $socialite->driver('facebook')->redirect();
|
2018-08-13 14:25:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the user information from Facebook.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2018-08-13 22:03:12 +01:00
|
|
|
public function handleFacebookCallback(Request $request)
|
2018-08-13 14:25:56 +01:00
|
|
|
{
|
2018-08-14 12:57:41 +01:00
|
|
|
$socialite = $this->setSocialiteConfigs();
|
|
|
|
$facebookUser = $socialite->driver('facebook')->user();
|
2018-08-13 14:25:56 +01:00
|
|
|
|
2018-08-15 14:22:13 +01:00
|
|
|
return $this->processSocialMediaLogin($request, 'facebook_id', $facebookUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processSocialMediaLogin(Request $request, $socialMediaIdField, $socialMediaUser)
|
|
|
|
{
|
|
|
|
$userBySocialMediaId = User::where($socialMediaIdField, $socialMediaUser->getId())->first();
|
|
|
|
|
|
|
|
if (!is_null($userBySocialMediaId))
|
2018-08-13 22:03:12 +01:00
|
|
|
{
|
2018-08-15 14:22:13 +01:00
|
|
|
// We have an existing user for this Facebook ID - log them in
|
|
|
|
$this->guard()->login($userBySocialMediaId);
|
|
|
|
return redirect(route('home'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$userByEmailAddress = User::where('email', $socialMediaUser->email)->first();
|
|
|
|
|
|
|
|
if (!is_null($userByEmailAddress))
|
|
|
|
{
|
|
|
|
// We have an existing user with the e-mail address associated with the Facebook account
|
|
|
|
// Prompt for the password for that account
|
|
|
|
$request->getSession()->put('ssoLoginData', [
|
|
|
|
'name' => $socialMediaUser->getName(),
|
|
|
|
'email' => $socialMediaUser->getEmail(),
|
|
|
|
$socialMediaIdField => $socialMediaUser->getId(),
|
2018-08-13 22:03:12 +01:00
|
|
|
'is_activated' => true
|
|
|
|
]);
|
|
|
|
|
2018-08-15 14:22:13 +01:00
|
|
|
return redirect(route('auth.login_sso'));
|
2018-08-13 22:03:12 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 14:22:13 +01:00
|
|
|
// We don't have an existing user - prompt for registration
|
|
|
|
$request->getSession()->put('ssoRegisterData', [
|
|
|
|
'name' => $socialMediaUser->getName(),
|
|
|
|
'email' => $socialMediaUser->getEmail(),
|
|
|
|
$socialMediaIdField => $socialMediaUser->getId(),
|
|
|
|
'is_activated' => true
|
|
|
|
]);
|
2018-08-13 22:03:12 +01:00
|
|
|
|
2018-08-15 14:22:13 +01:00
|
|
|
return redirect(route('auth.register_sso'));
|
2018-08-13 14:25:56 +01:00
|
|
|
}
|
2018-08-14 12:57:41 +01:00
|
|
|
|
|
|
|
private function setSocialiteConfigs()
|
|
|
|
{
|
|
|
|
// Force Socialite to use our config from the database instead of hard-coded in config/services.php
|
2018-08-15 14:22:13 +01:00
|
|
|
$socialite = app()->make(\Laravel\Socialite\Contracts\Factory::class);
|
2018-08-14 12:57:41 +01:00
|
|
|
$socialite->extend(
|
|
|
|
'facebook',
|
|
|
|
function ($app) use ($socialite) {
|
|
|
|
$config = [
|
|
|
|
'client_id' => UserConfig::get('facebook_app_id'),
|
|
|
|
'client_secret' => decrypt(UserConfig::get('facebook_app_secret')),
|
|
|
|
'redirect' => route('login_callback.facebook')
|
|
|
|
];
|
|
|
|
return $socialite->buildProvider(FacebookProvider::class, $config);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $socialite;
|
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|