#5: Implemented login with Google account

This commit is contained in:
Andy Heathershaw 2018-08-17 13:37:58 +01:00
parent c56fe271ef
commit 98ddb06b76
10 changed files with 137 additions and 5 deletions

View File

@ -106,6 +106,8 @@ class ConfigHelper
'enable_visitor_hits' => false,
'facebook_app_id' => '',
'facebook_app_secret' => '',
'google_app_id' => '',
'google_app_secret' => '',
'hotlink_protection' => false,
'items_per_page' => 12,
'items_per_page_admin' => 10,
@ -123,6 +125,7 @@ class ConfigHelper
'smtp_port' => 25,
'smtp_username' => '',
'social_facebook_login' => false,
'social_google_login' => false,
'social_twitter_login' => false,
'social_user_profiles' => false,
'theme' => 'default',

View File

@ -39,6 +39,7 @@ class DefaultController extends Controller
$this->passwordSettingKeys = [
'smtp_password',
'facebook_app_secret',
'google_app_secret',
'twitter_app_secret'
];
}
@ -230,6 +231,7 @@ class DefaultController extends Controller
'restrict_original_download',
'smtp_encryption',
'social_facebook_login',
'social_google_login',
'social_twitter_login',
'social_user_profiles'
];
@ -239,6 +241,8 @@ class DefaultController extends Controller
'date_format',
'facebook_app_id',
'facebook_app_secret',
'google_app_id',
'google_app_secret',
'sender_address',
'sender_name',
'smtp_server',

View File

@ -10,6 +10,7 @@ use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Laravel\Socialite\One\TwitterProvider;
use Laravel\Socialite\Two\FacebookProvider;
use Laravel\Socialite\Two\GoogleProvider;
use League\OAuth1\Client\Server\Twitter as TwitterServer;
use Socialite;
@ -128,6 +129,17 @@ class LoginController extends Controller
return $socialite->driver('facebook')->redirect();
}
/**
* Redirect the user to the Google authentication page.
*
* @return \Illuminate\Http\Response
*/
public function redirectToGoogle()
{
$socialite = $this->setSocialiteConfigs();
return $socialite->driver('google')->redirect();
}
/**
* Redirect the user to the Twitter authentication page.
*
@ -152,6 +164,19 @@ class LoginController extends Controller
return $this->processSocialMediaLogin($request, 'facebook_id', $facebookUser);
}
/**
* Obtain the user information from Google.
*
* @return \Illuminate\Http\Response
*/
public function handleGoogleCallback(Request $request)
{
$socialite = $this->setSocialiteConfigs();
$googleUser = $socialite->driver('google')->user();
return $this->processSocialMediaLogin($request, 'google_id', $googleUser);
}
/**
* Obtain the user information from Twitter.
*
@ -216,19 +241,30 @@ class LoginController extends Controller
'facebook',
function ($app) use ($socialite) {
$config = [
'client_id' => UserConfig::get('facebook_app_id'),
'client_secret' => decrypt(UserConfig::get('facebook_app_secret')),
'client_id' => trim(UserConfig::get('facebook_app_id')),
'client_secret' => trim(decrypt(UserConfig::get('facebook_app_secret'))),
'redirect' => route('login_callback.facebook')
];
return $socialite->buildProvider(FacebookProvider::class, $config);
}
);
$socialite->extend(
'google',
function ($app) use ($socialite) {
$config = [
'client_id' => trim(UserConfig::get('google_app_id')),
'client_secret' => trim(decrypt(UserConfig::get('google_app_secret'))),
'redirect' => route('login_callback.google')
];
return $socialite->buildProvider(GoogleProvider::class, $config);
}
);
$socialite->extend(
'twitter',
function ($app) use ($socialite) {
$config = [
'identifier' => UserConfig::get('twitter_app_id'),
'secret' => decrypt(UserConfig::get('twitter_app_secret')),
'identifier' => trim(UserConfig::get('twitter_app_id')),
'secret' => trim(decrypt(UserConfig::get('twitter_app_secret'))),
'callback_uri' => route('login_callback.twitter')
];
return new TwitterProvider($app['request'], new TwitterServer($config));

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', 'twitter_id'
'name', 'email', 'password', 'is_admin', 'is_activated', 'activation_token', 'enable_profile_page', 'profile_alias', 'facebook_id', 'twitter_id', 'google_id'
];
/**

View File

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

View File

@ -212,6 +212,7 @@ return [
'security_allow_self_registration' => 'Allow self-registration',
'security_allow_self_registration_description' => 'With this option enabled, users can sign up for their own accounts. You can grant permissions to accounts to allow users to upload their own photos or manage yours.',
'social_facebook' => 'Facebook',
'social_google' => 'Google',
'social_tab' => 'Social',
'social_twitter' => 'Twitter'
],

View File

@ -49,6 +49,10 @@ return [
'settings_social_facebook_app_secret' => 'Facebook App Secret:',
'settings_social_facebook_login' => 'Allow login/registration with a Facebook account.',
'settings_social_facebook_login_help' => 'With this option enabled, users can register (if enabled) and login with their Facebook account.',
'settings_social_google_app_id' => 'Google App ID:',
'settings_social_google_app_secret' => 'Google App Secret:',
'settings_social_google_login' => 'Allow login/registration with a Google account.',
'settings_social_google_login_help' => 'With this option enabled, users can register (if enabled) and login with their Google account.',
'settings_social_twitter_app_id' => 'Twitter App ID:',
'settings_social_twitter_app_secret' => 'Twitter App Secret:',
'settings_social_twitter_login' => 'Allow login/registration with a Twitter account',

View File

@ -418,6 +418,51 @@
</fieldset>
</div>
</div>
{{-- Google+ --}}
<div class="row">
<div class="col-2 col-sm-1">
<i class="fa fa-google fa-fw" style="font-size: xx-large;"></i>
</div>
<div class="col-10 col-sm-11">
<fieldset>
<legend class="mb-3">
@lang('admin.settings.social_google')
</legend>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="social-google-login" name="social_google_login" @if (old('social_google_login', $config['social_google_login']))checked="checked"@endif>
<label class="form-check-label" for="social-google-login">
<strong>@lang('forms.settings_social_google_login')</strong><br/>
@lang('forms.settings_social_google_login_help')
</label>
</div>
<div class="form-group mt-3">
<label class="form-control-label" for="google-app-id">@lang('forms.settings_social_google_app_id')</label>
<input type="text" class="form-control{{ $errors->has('google_app_id') ? ' is-invalid' : '' }}" id="google-app-id" name="google_app_id" value="{{ old('google_app_id', $config['google_app_id']) }}">
@if ($errors->has('google_app_id'))
<div class="invalid-feedback">
<strong>{{ $errors->first('google_app_id') }}</strong>
</div>
@endif
</div>
<div class="form-group mt-3">
<label class="form-control-label" for="google-app-secret">@lang('forms.settings_social_google_app_secret')</label>
<input type="text" class="form-control{{ $errors->has('google_app_secret') ? ' is-invalid' : '' }}" id="google-app-secret" name="google_app_secret" value="{{ old('google_app_secret', $config['google_app_secret']) }}">
@if ($errors->has('google_app_secret'))
<div class="invalid-feedback">
<strong>{{ $errors->first('google_app_secret') }}</strong>
</div>
@endif
</div>
</fieldset>
</div>
</div>
</div>
</div>

View File

@ -5,4 +5,7 @@
@if (UserConfig::get('social_twitter_login'))
<a href="{{ route('login.twitter') }}"><i class="fa fa-twitter fa-fw"></i></a>
@endif
@if (UserConfig::get('social_google_login'))
<a href="{{ route('login.google') }}"><i class="fa fa-google fa-fw"></i></a>
@endif
</p>

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/google', 'Auth\LoginController@redirectToGoogle')->name('login.google');
Route::get('login/google/callback', 'Auth\LoginController@handleGoogleCallback')->name('login_callback.google');
Route::get('login/sso', 'Auth\LoginController@showLoginFormSso')->name('auth.login_sso');
Route::get('login/twitter', 'Auth\LoginController@redirectToTwitter')->name('login.twitter');
Route::get('login/twitter/callback', 'Auth\LoginController@handleTwitterCallback')->name('login_callback.twitter');