- set social login credentials

- disallow registration refactoring
This commit is contained in:
Čarodej
2022-01-11 13:22:25 +01:00
parent 62cbcd14ed
commit 1c188081b3
23 changed files with 891 additions and 148 deletions
@@ -18,28 +18,40 @@ class SocialiteCallbackController extends Controller
public function __invoke($provider)
{
$isAllowedRegistration = intval(get_settings('registration'));
// Get socialite user
if (app()->runningUnitTests()) {
$provider_user = Socialite::driver($provider)->user();
$socialite = Socialite::driver($provider)->user();
} else {
$provider_user = Socialite::driver($provider)->stateless()->user();
$socialite = Socialite::driver($provider)->stateless()->user();
}
// Check if user exist already
$user = User::where('email', $provider_user->email)->first();
// Get user by email
$user = User::where('email', $socialite->email);
// Login User
if ($user) {
$this->guard->login($user);
// Login user when exists
if ($user->exists()) {
$this->guard->login(
$user->first()
);
return response('User logged in', 201);
}
// Check if account registration is enabled
if (! $isAllowedRegistration) {
return response([
'type' => 'error',
'message' => 'User registration is not allowed',
], 401);
}
// Create data user data object
$data = CreateUserData::fromArray([
'name' => $provider_user->getName(),
'email' => $provider_user->getEmail(),
'avatar' => store_socialite_avatar($provider_user->getAvatar()),
'name' => $socialite->getName(),
'email' => $socialite->getEmail(),
'avatar' => store_socialite_avatar($socialite->getAvatar()),
'oauth_provider' => $provider,
]);
@@ -1,6 +1,7 @@
<?php
namespace App\Users\Actions;
use App\Users\DTO\CreateUserData;
use App\Users\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\Registered;
@@ -17,17 +18,12 @@ class CreateNewUserAction extends Controller
/**
* Validate and create a new user.
*/
public function __invoke($data)
public function __invoke(CreateUserData $data)
{
$settings = get_settings([
'registration', 'user_verification', 'subscription_type',
'user_verification', 'subscription_type',
]);
// Check if account registration is enabled
if (! intval($settings['registration'])) {
abort(401);
}
// Create user
$user = User::create([
'password' => $data->password ? bcrypt($data->password) : null,
@@ -10,11 +10,18 @@ class RegisterUserController extends Controller
{
public function __construct(
public CreateNewUserAction $createNewUser,
) {
}
) {}
public function __invoke(RegisterUserRequest $request)
{
// Check if account registration is enabled
if (! intval(get_settings('registration'))) {
return response([
'type' => 'error',
'message' => 'User registration is not allowed',
], 401);
}
// Map registration data
$data = CreateUserData::fromRequest($request);