mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-26 02:20:39 +00:00
refactoring v1
This commit is contained in:
@@ -6,7 +6,6 @@ use Illuminate\Http\Response;
|
||||
use App\Users\Models\UserSettings;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use App\Users\Requests\RegisterUserRequest;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
@@ -21,14 +20,13 @@ class CreateNewUserAction extends Controller
|
||||
/**
|
||||
* Validate and create a new user.
|
||||
*/
|
||||
public function __invoke(
|
||||
$data
|
||||
): Application | ResponseFactory | Response {
|
||||
public function __invoke($data)
|
||||
{
|
||||
$settings = get_settings([
|
||||
'storage_default', 'registration', 'user_verification',
|
||||
]);
|
||||
|
||||
$socialite_auth = ! isset($data['password']) ?? true;
|
||||
$is_socialite = is_null($data->password);
|
||||
|
||||
// Check if account registration is enabled
|
||||
if (! intval($settings['registration'])) {
|
||||
@@ -37,9 +35,9 @@ class CreateNewUserAction extends Controller
|
||||
|
||||
// Create user
|
||||
$user = User::create([
|
||||
'password' => ! $socialite_auth ? bcrypt($data['password']) : null,
|
||||
'oauth_provider' => $socialite_auth ? $data->oauth_provider : null,
|
||||
'email' => $data['email'] ?? $data->email,
|
||||
'password' => $is_socialite ? null : bcrypt($data->password),
|
||||
'oauth_provider' => $data->oauth_provider,
|
||||
'email' => $data->email,
|
||||
]);
|
||||
|
||||
UserSettings::unguard();
|
||||
@@ -47,25 +45,23 @@ class CreateNewUserAction extends Controller
|
||||
$user
|
||||
->settings()
|
||||
->create([
|
||||
'name' => $data['name'] ?? $data->name,
|
||||
'name' => $data->name,
|
||||
'storage_capacity' => $settings['storage_default'],
|
||||
'avatar' => $data->avatar ? $data->avatar : null,
|
||||
'avatar' => $data->avatar,
|
||||
]);
|
||||
|
||||
UserSettings::reguard();
|
||||
|
||||
// Mark as verified if verification is disabled
|
||||
if (! intval($settings['user_verification']) || $socialite_auth) {
|
||||
if ($is_socialite || ! intval($settings['user_verification'])) {
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
// Log in if verification is disabled
|
||||
if (! intval($settings['user_verification']) || $socialite_auth) {
|
||||
if ($is_socialite || ! intval($settings['user_verification'])) {
|
||||
$this->guard->login($user);
|
||||
}
|
||||
|
||||
return response('User registered successfully', 201);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace App\Users\Controllers\Authentication;
|
||||
|
||||
use App\Users\DTO\CreateUserData;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use App\Users\Requests\RegisterUserRequest;
|
||||
@@ -14,6 +15,8 @@ class RegisterAuthenticationController extends Controller
|
||||
|
||||
public function __invoke(RegisterUserRequest $request)
|
||||
{
|
||||
($this->createNewUser)($request);
|
||||
$data = CreateUserData::fromRequest($request);
|
||||
|
||||
($this->createNewUser)($data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
namespace App\Users\Controllers\Authentication;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
|
||||
class SocialiteAuthenticationController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected StatefulGuard $guard,
|
||||
public CreateNewUserAction $createNewUser,
|
||||
) {
|
||||
}
|
||||
|
||||
public function redirect($provider)
|
||||
{
|
||||
$url = Socialite::driver($provider)->stateless()->redirect()->getTargetUrl();
|
||||
|
||||
return response()->json([
|
||||
'url' => $url
|
||||
]);
|
||||
}
|
||||
|
||||
public function callback($provider)
|
||||
{
|
||||
// Get socialite user
|
||||
if (app()->runningInConsole()) {
|
||||
$provider_user = Socialite::driver($provider)->user();
|
||||
} else {
|
||||
$provider_user = Socialite::driver($provider)->stateless()->user();
|
||||
}
|
||||
|
||||
// Check if user exist already
|
||||
$user = User::whereEmail($provider_user->email)->first();
|
||||
|
||||
if($user) {
|
||||
// Login User
|
||||
$this->guard->login($user);
|
||||
|
||||
} else {
|
||||
// Add user avatar from socialite
|
||||
$provider_user->avatar = store_socialite_avatar($provider_user->avatar);
|
||||
|
||||
// Add provider name
|
||||
$provider_user->oauth_provider = $provider;
|
||||
|
||||
// Create User
|
||||
($this->createNewUser)($provider_user);
|
||||
}
|
||||
|
||||
return response('Loged in', 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
|
||||
class CreateUserData extends DataTransferObject
|
||||
{
|
||||
public $name;
|
||||
public $email;
|
||||
public $password;
|
||||
public $oauth_provider;
|
||||
public $avatar;
|
||||
|
||||
public static function fromRequest($request): self
|
||||
{
|
||||
return new self([
|
||||
'name' => $request->input('name'),
|
||||
'email' => $request->input('email'),
|
||||
'avatar' => $request->input('avatar') ?? null,
|
||||
'password' => $request->input('password'),
|
||||
'oauth_provider' => $request->input('oauth_provider') ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self([
|
||||
'name' => $array['name'] ?? null,
|
||||
'email' => $array['email'],
|
||||
'avatar' => $array['avatar'],
|
||||
'password' => $array['password'] ?? null,
|
||||
'oauth_provider' => $array['oauth_provider'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user