#5: It's now possible to sign in/register with a Facebook account, and to link the FB account to an existing account by entering the account's password.

This commit is contained in:
Andy Heathershaw 2018-08-15 14:22:13 +01:00
parent 40fc25eba9
commit 8290bafb04
7 changed files with 173 additions and 45 deletions

View File

@ -43,6 +43,31 @@ class LoginController extends Controller
$this->middleware('guest', ['except' => 'logout']);
}
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;
}
protected function credentials(Request $request)
{
$result = $request->only($this->username(), 'password');
@ -62,7 +87,31 @@ class LoginController extends Controller
{
return Theme::render('auth.v2_unified', [
'active_tab' => 'login',
'info' => $request->session()->get('info')
'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
]);
}
@ -86,29 +135,52 @@ class LoginController extends Controller
{
$socialite = $this->setSocialiteConfigs();
$facebookUser = $socialite->driver('facebook')->user();
$user = User::where('facebook_id', $facebookUser->id)->first();
if (is_null($user))
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))
{
$request->getSession()->put('registerData', [
'name' => $facebookUser->name,
'email' => $facebookUser->email,
'facebook_id' => $facebookUser->id,
// 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(),
'is_activated' => true
]);
return redirect(route('register'));
return redirect(route('auth.login_sso'));
}
$this->guard()->login($user);
// 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
]);
return redirect(route('home'));
return redirect(route('auth.register_sso'));
}
private function setSocialiteConfigs()
{
// Force Socialite to use our config from the database instead of hard-coded in config/services.php
$socialite = app()->make('Laravel\Socialite\Contracts\Factory');
$socialite = app()->make(\Laravel\Socialite\Contracts\Factory::class);
$socialite->extend(
'facebook',
function ($app) use ($socialite) {

View File

@ -115,10 +115,11 @@ class RegisterController extends Controller
$userData = $request->all();
// Social media login info
$registerData = $request->getSession()->get('registerData');
$registerData = $request->getSession()->get('ssoRegisterData');
if (!is_null($registerData))
{
$userData = array_merge($registerData, $userData);
$request->getSession()->remove('ssoRegisterData');
}
/** @var User $user */
@ -150,16 +151,35 @@ class RegisterController extends Controller
return redirect(route('home'));
}
return Theme::render('auth.v2_unified', [
'active_tab' => 'register',
'is_sso' => false
]);
}
/**
* Show the application registration form (for a social media-linked account).
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationFormSso(Request $request)
{
if (!UserConfig::get('allow_self_registration'))
{
return redirect(route('home'));
}
// Social media login info
$registerData = $request->getSession()->get('registerData');
$registerData = $request->getSession()->get('ssoRegisterData');
if (is_null($registerData))
{
$registerData['name'] = '';
$registerData['email'] = '';
// No SSO data in session, use the normal registration screen
return redirect(route('register'));
}
return Theme::render('auth.v2_unified', [
'active_tab' => 'register',
'is_sso' => true,
'register_data' => $registerData
]);
}

View File

@ -23,7 +23,10 @@ return [
'change_password_action' => 'Change password',
'change_password_title' => 'Change your password',
'email_password_login' => 'Alternatively, login with your e-mail address and password:',
'email_password_login_sso' => 'The social media account you logged in with has not been used here before, however an account with the e-mail address already exists.',
'email_password_login_sso_2' => 'Please enter the password for this account to link your social media account to it.',
'email_password_register' => 'Alternatively, create an account using your e-mail address and a password:',
'email_password_register_sso' => 'Please confirm your name and set a password for your account:',
'forgot_password_action' => 'Send Reset E-mail',
'forgot_password_link' => 'Forgotten your password?',
'forgot_password_title' => 'Send password reset link',

View File

@ -8,13 +8,25 @@
<div class="card">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link{{ $active_tab == 'login' ? ' active' : '' }}" href="{{ url('/login') }}">@lang('auth.login_page_title')</a>
</li>
@if (UserConfig::get('allow_self_registration'))
@if (!$is_sso)
<li class="nav-item">
<a class="nav-link{{ $active_tab == 'register' ? ' active' : '' }}" href="{{ url('/register') }}">@lang('auth.register_page_title')</a>
<a class="nav-link{{ $active_tab == 'login' ? ' active' : '' }}" href="{{ url('/login') }}">@lang('auth.login_page_title')</a>
</li>
@if (UserConfig::get('allow_self_registration'))
<li class="nav-item">
<a class="nav-link{{ $active_tab == 'register' ? ' active' : '' }}" href="{{ url('/register') }}">@lang('auth.register_page_title')</a>
</li>
@endif
@else
@if ($active_tab == 'register')
<li class="nav-item">
<a class="nav-link active" href="{{ route('auth.register_sso') }}">@lang('auth.register_page_title')</a>
</li>
@elseif ($active_tab == 'login')
<li class="nav-item">
<a class="nav-link active" href="{{ route('auth.login_sso') }}">@lang('auth.login_page_title')</a>
</li>
@endif
@endif
</ul>
</div>

View File

@ -1,10 +1,14 @@
<p>@lang('auth.social_login')</p>
<p class="text-center">
<a href="{{ route('login.facebook') }}"><i class="fa fa-facebook fa-fw" style="font-size: xx-large;"></i></a>
{{--<a href="{{ route('login.facebook') }}"><i class="fa fa-twitter fa-fw" style="font-size: xx-large;"></i></a>--}}
</p>
@if (!$is_sso)
<p>@lang('auth.social_login')</p>
<p class="text-center">
<a href="{{ route('login.facebook') }}"><i class="fa fa-facebook fa-fw" style="font-size: xx-large;"></i></a>
</p>
<p class="mt-5 mb-4">@lang('auth.email_password_login')</p>
<p class="mt-5 mb-4">@lang('auth.email_password_login')</p>
@else
<p>@lang('auth.email_password_login_sso')</p>
<p class="mb-5">@lang('auth.email_password_login_sso_2')</p>
@endif
<form role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
@ -13,7 +17,12 @@
<label for="email" class="col-md-4 col-form-label text-md-right">@lang('forms.email_label')</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" autofocus>
@if ($is_sso)
<input type="hidden" name="email" value="{{ $login_data['email'] }}">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" value="{{ $login_data['email'] }}" readonly>
@else
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" autofocus>
@endif
@if ($errors->has('email'))
<div class="invalid-feedback">
@ -27,7 +36,7 @@
<label for="password" class="col-md-4 col-form-label text-md-right">@lang('forms.password_label')</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password"{{ $is_sso ? ' autofocus' : '' }}>
@if ($errors->has('password'))
<div class="invalid-feedback">
@ -37,16 +46,18 @@
</div>
</div>
<div class="form-group row">
<div class="col-md-4"><!-- --></div>
<div class="col-md-6">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="remember-me" name="remember">
<label class="form-check-label" for="remember-me">@lang('forms.remember_me_label')
</label>
@if (!$is_sso)
<div class="form-group row">
<div class="col-md-4"><!-- --></div>
<div class="col-md-6">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="remember-me" name="remember">
<label class="form-check-label" for="remember-me">@lang('forms.remember_me_label')
</label>
</div>
</div>
</div>
</div>
@endif
<div class="form-group row">
<div class="col-md-2"><!-- --></div>

View File

@ -1,10 +1,13 @@
<p>@lang('auth.social_register')</p>
<p class="text-center">
<a href="{{ route('login.facebook') }}"><i class="fa fa-facebook fa-fw" style="font-size: xx-large;"></i></a>
{{--<a href="{{ route('login.facebook') }}"><i class="fa fa-twitter fa-fw" style="font-size: xx-large;"></i></a>--}}
</p>
@if (!$is_sso)
<p>@lang('auth.social_register')</p>
<p class="text-center">
<a href="{{ route('login.facebook') }}"><i class="fa fa-facebook fa-fw" style="font-size: xx-large;"></i></a>
</p>
<p class="mt-5 mb-4">@lang('auth.email_password_register')</p>
<p class="mt-5 mb-4">@lang('auth.email_password_register')</p>
@else
<p class="mb-4">@lang('auth.email_password_register_sso')</p>
@endif
<form role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
@ -13,7 +16,7 @@
<label for="name" class="col-md-4 col-form-label text-md-right">@lang('forms.realname_label')</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name', $register_data['name']) }}"{{ empty($register_data['name']) ? ' autofocus' : '' }}>
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name', $is_sso ? $register_data['name'] : '') }}"{{ $is_sso ? ' autofocus' : '' }}>
@if ($errors->has('name'))
<div class="invalid-feedback">
@ -27,7 +30,12 @@
<label for="email" class="col-md-4 col-form-label text-md-right">@lang('forms.email_label')</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email', $register_data['email']) }}">
@if ($is_sso)
<input type="hidden" name="email" value="{{ $register_data['email'] }}">
<input id="email" type="email" class="form-control" value="{{ $register_data['email'] }}" readonly>
@else
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email', $is_sso ? $register_data['email'] : '') }}">
@endif
@if ($errors->has('email'))
<div class="invalid-feedback">
@ -41,7 +49,7 @@
<label for="password" class="col-md-4 col-form-label text-md-right">@lang('forms.password_label')</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password"{{ !empty($register_data['name']) ? ' autofocus' : '' }}>
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password"{{ $is_sso ? ' autofocus' : '' }}>
@if ($errors->has('password'))
<div class="invalid-feedback">

View File

@ -77,6 +77,8 @@ Route::group(['prefix' => 'install'], function () {
// Social media SSO
Route::get('login/facebook', 'Auth\LoginController@redirectToFacebook')->name('login.facebook');
Route::get('login/facebook/callback', 'Auth\LoginController@handleFacebookCallback')->name('login_callback.facebook');
Route::get('login/sso', 'Auth\LoginController@showLoginFormSso')->name('auth.login_sso');
Route::get('register/sso', 'Auth\RegisterController@showRegistrationFormSso')->name('auth.register_sso');
// Gallery
Route::get('/', 'Gallery\DefaultController@index')->name('home');