#5: Facebook social login now works. Added Facebook to the login screen template (I'm not 100% happy with this, may need a bit more work.)

This commit is contained in:
Andy Heathershaw 2018-08-13 22:03:12 +01:00
parent 564fcd4b09
commit 52473f846e
8 changed files with 113 additions and 29 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Auth;
use App\Facade\Theme; use App\Facade\Theme;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Socialite; use Socialite;
@ -78,11 +79,25 @@ class LoginController extends Controller
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function handleFacebookCallback() public function handleFacebookCallback(Request $request)
{ {
$user = Socialite::driver('facebook')->user(); $facebookUser = Socialite::driver('facebook')->user();
dd($user); $user = User::where('facebook_id', $facebookUser->id)->first();
// $user->token; if (is_null($user))
{
$request->getSession()->put('registerData', [
'name' => $facebookUser->name,
'email' => $facebookUser->email,
'facebook_id' => $facebookUser->id,
'is_activated' => true
]);
return redirect(route('register'));
}
$this->guard()->login($user);
return redirect(route('home'));
} }
} }

View File

@ -85,25 +85,22 @@ class RegisterController extends Controller
*/ */
protected function create(array $data) protected function create(array $data)
{ {
$activationData = [ if (!isset($data['is_activated']))
'is_activated' => true
];
if (UserConfig::get('require_email_verification'))
{ {
$activationData['is_activated'] = false; $data['is_activated'] = true;
$activationData['activation_token'] = MiscHelper::randomString();
if (UserConfig::get('require_email_verification'))
{
$data['is_activated'] = false;
$data['activation_token'] = MiscHelper::randomString();
}
} }
return User::create(array_merge( $data['password'] = bcrypt($data['password']);
[ $data['is_admin'] = false;
'name' => $data['name'], unset($data['password_confirmation']);
'email' => $data['email'],
'password' => bcrypt($data['password']), return User::create($data);
'is_admin' => false
],
$activationData
));
} }
public function register(Request $request) public function register(Request $request)
@ -115,8 +112,17 @@ class RegisterController extends Controller
$this->validator($request)->validate(); $this->validator($request)->validate();
$userData = $request->all();
// Social media login info
$registerData = $request->getSession()->get('registerData');
if (!is_null($registerData))
{
$userData = array_merge($registerData, $userData);
}
/** @var User $user */ /** @var User $user */
$user = $this->create($request->all()); $user = $this->create($userData);
if ($user->is_activated) if ($user->is_activated)
{ {
@ -137,15 +143,24 @@ class RegisterController extends Controller
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function showRegistrationForm() public function showRegistrationForm(Request $request)
{ {
if (!UserConfig::get('allow_self_registration')) if (!UserConfig::get('allow_self_registration'))
{ {
return redirect(route('home')); return redirect(route('home'));
} }
// Social media login info
$registerData = $request->getSession()->get('registerData');
if (is_null($registerData))
{
$registerData['name'] = '';
$registerData['email'] = '';
}
return Theme::render('auth.v2_unified', [ return Theme::render('auth.v2_unified', [
'active_tab' => 'register' 'active_tab' => 'register',
'register_data' => $registerData
]); ]);
} }
} }

View File

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

View File

@ -17,7 +17,7 @@ return [
'facebook' => [ 'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'), 'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => url('login/facebook/callback') 'redirect' => php_sapi_name() != 'cli' ? url('login/facebook/callback') : ''
], ],
'gitea' => [ 'gitea' => [

View File

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

View File

@ -22,6 +22,8 @@ return [
'Please click the link in this e-mail to activate your account.', 'Please click the link in this e-mail to activate your account.',
'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_register' => 'Alternatively, create an account using your e-mail address and a password:',
'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',
@ -29,5 +31,7 @@ return [
'recaptcha_failed_message' => 'The reCAPTCHA verfication failed. Please ensure you have completed the reCAPTCHA challenge and try again.', 'recaptcha_failed_message' => 'The reCAPTCHA verfication failed. Please ensure you have completed the reCAPTCHA challenge and try again.',
'register_page_title' => 'Create an account', 'register_page_title' => 'Create an account',
'reset_password_action' => 'Reset Password', 'reset_password_action' => 'Reset Password',
'reset_password_title' => 'Reset your password' 'reset_password_title' => 'Reset your password',
'social_login' => 'Feeling sociable? Login with:',
'social_register' => 'Feeling sociable? Register with:'
]; ];

View File

@ -1,3 +1,11 @@
<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>
<p class="mt-5 mb-4">@lang('auth.email_password_login')</p>
<form role="form" method="POST" action="{{ url('/login') }}"> <form role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }} {{ csrf_field() }}

View File

@ -1,3 +1,11 @@
<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>
<p class="mt-5 mb-4">@lang('auth.email_password_register')</p>
<form role="form" method="POST" action="{{ url('/register') }}"> <form role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }} {{ csrf_field() }}
@ -5,7 +13,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') }}" autofocus> <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' : '' }}>
@if ($errors->has('name')) @if ($errors->has('name'))
<div class="invalid-feedback"> <div class="invalid-feedback">
@ -19,7 +27,7 @@
<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') }}"> <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email', $register_data['email']) }}">
@if ($errors->has('email')) @if ($errors->has('email'))
<div class="invalid-feedback"> <div class="invalid-feedback">
@ -33,7 +41,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"{{ !empty($register_data['name']) ? ' autofocus' : '' }}>
@if ($errors->has('password')) @if ($errors->has('password'))
<div class="invalid-feedback"> <div class="invalid-feedback">