diff --git a/routes/api.php b/routes/api.php
index 112fdee4..b4e37cab 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -1,7 +1,6 @@
'socialite'], function () {
+ Route::get('/{provider}/redirect', [SocialiteAuthenticationController::class, 'redirect']);
+ Route::get('/{provider}/callback', [SocialiteAuthenticationController::class, 'callback']);
+});
// Password reset
Route::group(['prefix' => 'password'], function () {
diff --git a/src/App/Users/Actions/CreateNewUserAction.php b/src/App/Users/Actions/CreateNewUserAction.php
index e812d286..c94b1a8e 100644
--- a/src/App/Users/Actions/CreateNewUserAction.php
+++ b/src/App/Users/Actions/CreateNewUserAction.php
@@ -22,12 +22,14 @@ class CreateNewUserAction extends Controller
* Validate and create a new user.
*/
public function __invoke(
- RegisterUserRequest $request
+ $data
): Application | ResponseFactory | Response {
$settings = get_settings([
'storage_default', 'registration', 'user_verification',
]);
+ $socialite_auth = ! isset($data['password']) ?? true;
+
// Check if account registration is enabled
if (! intval($settings['registration'])) {
abort(401);
@@ -35,12 +37,12 @@ class CreateNewUserAction extends Controller
// Create user
$user = User::create([
- 'password' => bcrypt($request->input('password')),
- 'email' => $request->input('email'),
+ 'password' => ! $socialite_auth ? bcrypt($data['password']) : null,
+ 'email' => $data['email'],
]);
// Mark as verified if verification is disabled
- if (! intval($settings['user_verification'])) {
+ if (! intval($settings['user_verification']) || $socialite_auth) {
$user->markEmailAsVerified();
}
@@ -49,8 +51,9 @@ class CreateNewUserAction extends Controller
$user
->settings()
->create([
- 'name' => $request->input('name'),
+ 'name' => $data['name'],
'storage_capacity' => $settings['storage_default'],
+ 'avatar' => $data->avatar ? $data->avatar : null,
]);
UserSettings::reguard();
@@ -58,7 +61,7 @@ class CreateNewUserAction extends Controller
event(new Registered($user));
// Log in if verification is disabled
- if (! intval($settings['user_verification'])) {
+ if (! intval($settings['user_verification']) || $socialite_auth) {
$this->guard->login($user);
}
diff --git a/src/App/Users/Controllers/Authentication/RegisterAuthenticationController.php b/src/App/Users/Controllers/Authentication/RegisterAuthenticationController.php
new file mode 100644
index 00000000..c3e37a35
--- /dev/null
+++ b/src/App/Users/Controllers/Authentication/RegisterAuthenticationController.php
@@ -0,0 +1,19 @@
+createNewUser)($request);
+ }
+}
diff --git a/src/App/Users/Controllers/Authentication/SocialiteAuthenticationController.php b/src/App/Users/Controllers/Authentication/SocialiteAuthenticationController.php
new file mode 100644
index 00000000..cfe13d53
--- /dev/null
+++ b/src/App/Users/Controllers/Authentication/SocialiteAuthenticationController.php
@@ -0,0 +1,49 @@
+stateless()->redirect()->getTargetUrl();
+
+ return response()->json([
+ 'url' => $url
+ ]);
+ }
+
+ public function callback($provider)
+ {
+ // Get socialite user
+ $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 = get_socialite_avatar($provider_user->avatar);
+
+ // Create User
+ ($this->createNewUser)($provider_user);
+ }
+
+ return response('Loged in', 200);
+ }
+}
diff --git a/src/App/Users/Resources/UserResource.php b/src/App/Users/Resources/UserResource.php
index 52e8b74e..168e4ffe 100644
--- a/src/App/Users/Resources/UserResource.php
+++ b/src/App/Users/Resources/UserResource.php
@@ -27,6 +27,7 @@ class UserResource extends JsonResource
'email' => is_demo() ? obfuscate_email($this->email) : $this->email,
'role' => $this->role,
'two_factor_authentication' => $this->two_factor_secret ? true : false,
+ 'socialite_account' => $this->password ? false : true,
'folders' => $this->folder_tree,
'storage' => $this->storage,
'created_at_formatted' => format_date($this->created_at, '%d. %B. %Y'),
diff --git a/src/Support/helpers.php b/src/Support/helpers.php
index ec14688d..8ed9c739 100644
--- a/src/Support/helpers.php
+++ b/src/Support/helpers.php
@@ -1012,4 +1012,35 @@ if (! function_exists('replace_occurrence')) {
$string
);
}
+
+if(! function_exists('get_socialite_avatar')) {
+ /**
+ * Get socialite avatar create and store his thumbnails
+ */
+ function get_socialite_avatar($avatar)
+ {
+ $image = file_get_contents($avatar);
+
+ // Generate avatar name
+ $avatar_name = Str::uuid() . '.jpg';
+
+ // Create intervention image
+ $intervention = Image::make($image);
+
+ // Generate avatar sizes
+ collect(config('vuefilemanager.avatar_sizes'))
+ ->each(function ($size) use ($intervention, $avatar_name) {
+
+ // fit thumbnail
+ $intervention->fit($size['size'], $size['size'])->stream();
+
+ // Store thumbnail to disk
+ Storage::put("avatars/{$size['name']}-{$avatar_name}", $intervention);
+ });
+
+ // Return path to image
+ return $avatar_name;
+ }
+ }
+
}