#5: Twitter login is now working completely. If the Twitter app is not authorised to access the user's e-mail address, they still have to create a new account.

This commit is contained in:
Andy Heathershaw 2018-08-16 14:01:56 +01:00
parent 1ed4f297d2
commit c56fe271ef
4 changed files with 69 additions and 17 deletions

View File

@ -152,6 +152,19 @@ class LoginController extends Controller
return $this->processSocialMediaLogin($request, 'facebook_id', $facebookUser);
}
/**
* Obtain the user information from Twitter.
*
* @return \Illuminate\Http\Response
*/
public function handleTwitterCallback(Request $request)
{
$socialite = $this->setSocialiteConfigs();
$twitterUser = $socialite->driver('twitter')->user();
return $this->processSocialMediaLogin($request, 'twitter_id', $twitterUser);
}
private function processSocialMediaLogin(Request $request, $socialMediaIdField, $socialMediaUser)
{
$userBySocialMediaId = User::where($socialMediaIdField, $socialMediaUser->getId())->first();
@ -163,20 +176,25 @@ class LoginController extends Controller
return redirect(route('home'));
}
$userByEmailAddress = User::where('email', $socialMediaUser->email)->first();
if (!is_null($userByEmailAddress))
// Some providers (*cough*Twitter*cough*) don't give e-mail addresses without explicit permission/additional
// verification
if (!is_null($socialMediaUser->email))
{
// 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
]);
$userByEmailAddress = User::where('email', $socialMediaUser->getEmail())->first();
return redirect(route('auth.login_sso'));
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('auth.login_sso'));
}
}
// We don't have an existing user - prompt for registration

View File

@ -16,7 +16,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'is_admin', 'is_activated', 'activation_token', 'enable_profile_page', 'profile_alias', 'facebook_id'
'name', 'email', 'password', 'is_admin', 'is_activated', 'activation_token', 'enable_profile_page', 'profile_alias', 'facebook_id', 'twitter_id'
];
/**

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddUserTwitterIdColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table)
{
$table->string('twitter_id')->nullable(true);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table)
{
$table->dropColumn('twitter_id');
});
}
}

View File

@ -16,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', $is_sso ? $register_data['name'] : '') }}"{{ $is_sso ? ' 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">
@ -30,11 +30,11 @@
<label for="email" class="col-md-4 col-form-label text-md-right">@lang('forms.email_label')</label>
<div class="col-md-6">
@if ($is_sso)
@if ($is_sso && !empty($register_data['email']))
<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'] : '') }}">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email', $is_sso ? $register_data['email'] : '') }}"{{ $is_sso && empty($register_data['email']) ? ' autofocus' : '' }}>
@endif
@if ($errors->has('email'))
@ -49,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"{{ $is_sso ? ' autofocus' : '' }}>
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password"{{ $is_sso && !empty($register_data['email']) ? ' autofocus' : '' }}>
@if ($errors->has('password'))
<div class="invalid-feedback">