#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']); $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) protected function credentials(Request $request)
{ {
$result = $request->only($this->username(), 'password'); $result = $request->only($this->username(), 'password');
@ -62,7 +87,31 @@ class LoginController extends Controller
{ {
return Theme::render('auth.v2_unified', [ return Theme::render('auth.v2_unified', [
'active_tab' => 'login', '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(); $socialite = $this->setSocialiteConfigs();
$facebookUser = $socialite->driver('facebook')->user(); $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)
{ {
$request->getSession()->put('registerData', [ $userBySocialMediaId = User::where($socialMediaIdField, $socialMediaUser->getId())->first();
'name' => $facebookUser->name,
'email' => $facebookUser->email, if (!is_null($userBySocialMediaId))
'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 '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() private function setSocialiteConfigs()
{ {
// Force Socialite to use our config from the database instead of hard-coded in config/services.php // 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( $socialite->extend(
'facebook', 'facebook',
function ($app) use ($socialite) { function ($app) use ($socialite) {

View File

@ -115,10 +115,11 @@ class RegisterController extends Controller
$userData = $request->all(); $userData = $request->all();
// Social media login info // Social media login info
$registerData = $request->getSession()->get('registerData'); $registerData = $request->getSession()->get('ssoRegisterData');
if (!is_null($registerData)) if (!is_null($registerData))
{ {
$userData = array_merge($registerData, $userData); $userData = array_merge($registerData, $userData);
$request->getSession()->remove('ssoRegisterData');
} }
/** @var User $user */ /** @var User $user */
@ -150,16 +151,35 @@ class RegisterController extends Controller
return redirect(route('home')); 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 // Social media login info
$registerData = $request->getSession()->get('registerData'); $registerData = $request->getSession()->get('ssoRegisterData');
if (is_null($registerData)) if (is_null($registerData))
{ {
$registerData['name'] = ''; // No SSO data in session, use the normal registration screen
$registerData['email'] = ''; return redirect(route('register'));
} }
return Theme::render('auth.v2_unified', [ return Theme::render('auth.v2_unified', [
'active_tab' => 'register', 'active_tab' => 'register',
'is_sso' => true,
'register_data' => $registerData 'register_data' => $registerData
]); ]);
} }

View File

@ -23,7 +23,10 @@ return [
'change_password_action' => 'Change password', 'change_password_action' => 'Change password',
'change_password_title' => 'Change your password', 'change_password_title' => 'Change your password',
'email_password_login' => 'Alternatively, login with your e-mail address and 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' => '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_action' => 'Send Reset E-mail',
'forgot_password_link' => 'Forgotten your password?', 'forgot_password_link' => 'Forgotten your password?',
'forgot_password_title' => 'Send password reset link', 'forgot_password_title' => 'Send password reset link',

View File

@ -8,6 +8,7 @@
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">
<ul class="nav nav-tabs card-header-tabs"> <ul class="nav nav-tabs card-header-tabs">
@if (!$is_sso)
<li class="nav-item"> <li class="nav-item">
<a class="nav-link{{ $active_tab == 'login' ? ' active' : '' }}" href="{{ url('/login') }}">@lang('auth.login_page_title')</a> <a class="nav-link{{ $active_tab == 'login' ? ' active' : '' }}" href="{{ url('/login') }}">@lang('auth.login_page_title')</a>
</li> </li>
@ -16,6 +17,17 @@
<a class="nav-link{{ $active_tab == 'register' ? ' active' : '' }}" href="{{ url('/register') }}">@lang('auth.register_page_title')</a> <a class="nav-link{{ $active_tab == 'register' ? ' active' : '' }}" href="{{ url('/register') }}">@lang('auth.register_page_title')</a>
</li> </li>
@endif @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> </ul>
</div> </div>
<div class="card-body"> <div class="card-body">

View File

@ -1,10 +1,14 @@
@if (!$is_sso)
<p>@lang('auth.social_login')</p> <p>@lang('auth.social_login')</p>
<p class="text-center"> <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-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> </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') }}"> <form role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }} {{ csrf_field() }}
@ -13,7 +17,12 @@
<label for="email" class="col-md-4 col-form-label text-md-right">@lang('forms.email_label')</label> <label for="email" class="col-md-4 col-form-label text-md-right">@lang('forms.email_label')</label>
<div class="col-md-6"> <div class="col-md-6">
@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> <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" autofocus>
@endif
@if ($errors->has('email')) @if ($errors->has('email'))
<div class="invalid-feedback"> <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> <label for="password" class="col-md-4 col-form-label text-md-right">@lang('forms.password_label')</label>
<div class="col-md-6"> <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')) @if ($errors->has('password'))
<div class="invalid-feedback"> <div class="invalid-feedback">
@ -37,6 +46,7 @@
</div> </div>
</div> </div>
@if (!$is_sso)
<div class="form-group row"> <div class="form-group row">
<div class="col-md-4"><!-- --></div> <div class="col-md-4"><!-- --></div>
<div class="col-md-6"> <div class="col-md-6">
@ -47,6 +57,7 @@
</div> </div>
</div> </div>
</div> </div>
@endif
<div class="form-group row"> <div class="form-group row">
<div class="col-md-2"><!-- --></div> <div class="col-md-2"><!-- --></div>

View File

@ -1,10 +1,13 @@
@if (!$is_sso)
<p>@lang('auth.social_register')</p> <p>@lang('auth.social_register')</p>
<p class="text-center"> <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-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> </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') }}"> <form role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }} {{ csrf_field() }}
@ -13,7 +16,7 @@
<label for="name" class="col-md-4 col-form-label text-md-right">@lang('forms.realname_label')</label> <label for="name" class="col-md-4 col-form-label text-md-right">@lang('forms.realname_label')</label>
<div class="col-md-6"> <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')) @if ($errors->has('name'))
<div class="invalid-feedback"> <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> <label for="email" class="col-md-4 col-form-label text-md-right">@lang('forms.email_label')</label>
<div class="col-md-6"> <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')) @if ($errors->has('email'))
<div class="invalid-feedback"> <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> <label for="password" class="col-md-4 col-form-label text-md-right">@lang('forms.password_label')</label>
<div class="col-md-6"> <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')) @if ($errors->has('password'))
<div class="invalid-feedback"> <div class="invalid-feedback">

View File

@ -77,6 +77,8 @@ Route::group(['prefix' => 'install'], function () {
// Social media SSO // Social media SSO
Route::get('login/facebook', 'Auth\LoginController@redirectToFacebook')->name('login.facebook'); 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/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 // Gallery
Route::get('/', 'Gallery\DefaultController@index')->name('home'); Route::get('/', 'Gallery\DefaultController@index')->name('home');