mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
Merge remote-tracking branch 'origin/social_authentication' into subscription
# Conflicts: # .env.example # composer.lock # public/mix-manifest.json # resources/js/views/User/Password.vue # routes/api.php # src/App/Users/Actions/CreateNewUserAction.php # tests/App/Users/UserAccountTest.php
This commit is contained in:
@@ -60,4 +60,7 @@ PAYSTACK_PUBLIC_KEY=
|
||||
PAYPAL_CLIENT_ID=
|
||||
PAYPAL_CLIENT_SECRET=
|
||||
|
||||
SANCTUM_STATEFUL_DOMAINS=localhost,localhost:8000,127.0.0.1,127.0.0.1:8000,::1
|
||||
SANCTUM_STATEFUL_DOMAINS=localhost,localhost:8000,127.0.0.1,127.0.0.1:8000,::1
|
||||
|
||||
FACEBOOK_CLIENT_ID=
|
||||
FACEBOOK_CLIENT_SECRET=
|
||||
@@ -23,6 +23,7 @@
|
||||
"laravel/fortify": "^1.8.3",
|
||||
"laravel/framework": "^8.77.1",
|
||||
"laravel/sanctum": "^2.12.1",
|
||||
"laravel/socialite": "^5.2",
|
||||
"laravel/tinker": "^2.6.2",
|
||||
"laravel/ui": "^3.3.1",
|
||||
"league/flysystem-aws-s3-v3": "^1.0.29",
|
||||
|
||||
@@ -161,6 +161,7 @@ return [
|
||||
Illuminate\View\ViewServiceProvider::class,
|
||||
|
||||
TeamTNT\Scout\TNTSearchScoutServiceProvider::class,
|
||||
Laravel\Socialite\SocialiteServiceProvider::class,
|
||||
Intervention\Image\ImageServiceProvider::class,
|
||||
App\Providers\FortifyServiceProvider::class,
|
||||
|
||||
@@ -228,6 +229,7 @@ return [
|
||||
'Image' => Intervention\Image\Facades\Image::class,
|
||||
'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class,
|
||||
'Crawler' => Jaybizzle\LaravelCrawlerDetect\Facades\LaravelCrawlerDetect::class,
|
||||
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
|
||||
],
|
||||
|
||||
'deploy_secret' => env('APP_DEPLOY_SECRET'),
|
||||
|
||||
@@ -33,4 +33,22 @@ return [
|
||||
'client_id' => env('PASSPORT_CLIENT_ID'),
|
||||
'client_secret' => env('PASSPORT_CLIENT_SECRET'),
|
||||
],
|
||||
|
||||
'google' => [
|
||||
'client_id' => env('GOOGLE_CLIENT_ID'),
|
||||
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
|
||||
'redirect' => env('APP_URL') . '/socialite/google/callback'
|
||||
],
|
||||
|
||||
'github' => [
|
||||
'client_id' => env('GITHUB_CLIENT_ID'),
|
||||
'client_secret' => env('GITHUB_CLIENT_SECRET'),
|
||||
'redirect' => env('APP_URL') . '/socialite/github/callback',
|
||||
],
|
||||
|
||||
'facebook' => [
|
||||
'client_id' => env('FACEBOOK_CLIENT_ID'),
|
||||
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
|
||||
'redirect' => env('APP_URL') . '/socialite/facebook/callback',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddOauthProviderToUsersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->string('oauth_provider')->nullable();
|
||||
$table->string('password')->nullable()->change();
|
||||
|
||||
$table->charset = 'utf8mb4';
|
||||
$table->collation = 'utf8mb4_unicode_ci';
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<template>
|
||||
<div class="wrapper flex flex-row w-1/2 ml-2">
|
||||
<div class="w-1/3 grid justify-items-center items-center cursor-pointer">
|
||||
<facebook-icon @click="socialiteRedirect('facebook')" />
|
||||
</div>
|
||||
|
||||
<div class="w-1/3 grid justify-items-center items-center cursor-pointer">
|
||||
<github-icon @click="socialiteRedirect('github')" />
|
||||
</div>
|
||||
|
||||
<div class="w-1/3 grid justify-items-center items-center cursor-pointer">
|
||||
<h1 @click="socialiteRedirect('google')">G</h1>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { FacebookIcon, GithubIcon } from 'vue-feather-icons'
|
||||
|
||||
export default {
|
||||
name:'SocialiteAuthenticationButtons',
|
||||
components: {
|
||||
FacebookIcon,
|
||||
GithubIcon,
|
||||
},
|
||||
methods: {
|
||||
socialiteRedirect(provider) {
|
||||
|
||||
this.isLoading = true
|
||||
|
||||
this.$store.dispatch('socialiteRedirect', provider)
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
||||
.wrapper {
|
||||
margin: 50px auto 0px auto;
|
||||
}
|
||||
|
||||
</style>
|
||||
9
resources/js/routes/routesAuth.js
vendored
9
resources/js/routes/routesAuth.js
vendored
@@ -1,4 +1,13 @@
|
||||
const routesAuth = [
|
||||
{
|
||||
name: 'SocialiteCallback',
|
||||
path: '/socialite/:provider/callback',
|
||||
component: () =>
|
||||
import(/* webpackChunkName: "chunks/email-verified" */ '../views/Auth/SocialiteCallback'),
|
||||
meta: {
|
||||
requiresAuth: false
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'SuccessfullyVerified',
|
||||
path: '/successfully-verified',
|
||||
|
||||
11
resources/js/store/modules/userAuth.js
vendored
11
resources/js/store/modules/userAuth.js
vendored
@@ -48,6 +48,17 @@ const actions = {
|
||||
router.push({name: 'Homepage'})
|
||||
})
|
||||
},
|
||||
socialiteRedirect: ({commit}, provider) => {
|
||||
|
||||
axios
|
||||
.get(`/api/socialite/${provider}/redirect`)
|
||||
.then((response) => {
|
||||
if(response.data.url) {
|
||||
window.location.href = response.data.url
|
||||
}
|
||||
})
|
||||
.catch(() => this.$isSomethingWrong())
|
||||
},
|
||||
addToFavourites: (context, folder) => {
|
||||
let addFavourites = []
|
||||
let items = [folder]
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
:disabled="isLoading" />
|
||||
</ValidationObserver>
|
||||
|
||||
<SocialiteAuthenticationButtons/>
|
||||
|
||||
<span v-if="config.userRegistration" class="additional-link">
|
||||
{{ $t('page_login.registration_text') }}
|
||||
<router-link class="text-theme" :to="{name: 'SignUp'}">
|
||||
@@ -151,6 +153,7 @@
|
||||
<script>
|
||||
import AuthContentWrapper from '/resources/js/components/Auth/AuthContentWrapper'
|
||||
import {ValidationObserver, ValidationProvider} from 'vee-validate/dist/vee-validate.full'
|
||||
import SocialiteAuthenticationButtons from '/resources/js/components/Auth/SocialiteAuthenticationButtons'
|
||||
import AuthContent from '/resources/js/components/Auth/AuthContent'
|
||||
import AuthButton from '/resources/js/components/Auth/AuthButton'
|
||||
import Spinner from '/resources/js/components/FilesView/Spinner'
|
||||
@@ -162,10 +165,11 @@
|
||||
export default {
|
||||
name: 'SignIn',
|
||||
components: {
|
||||
Headline,
|
||||
Headline,
|
||||
AuthContentWrapper,
|
||||
ValidationProvider,
|
||||
ValidationObserver,
|
||||
SocialiteAuthenticationButtons,
|
||||
AuthContent,
|
||||
AuthButton,
|
||||
Spinner,
|
||||
@@ -233,8 +237,15 @@
|
||||
|
||||
this.checkedAccount = response.data
|
||||
|
||||
// Show sign in password page
|
||||
this.goToAuthPage('sign-in')
|
||||
if(response.data.oauth_provider) {
|
||||
// Redirect user to socialite login if he's accout is registered by socialite
|
||||
this.$store.dispatch('socialiteRedirect', response.data.oauth_provider)
|
||||
|
||||
} else {
|
||||
// Show sign in password page
|
||||
this.goToAuthPage('sign-in')
|
||||
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
|
||||
|
||||
@@ -62,6 +62,8 @@
|
||||
</div>
|
||||
</ValidationObserver>
|
||||
|
||||
<SocialiteAuthenticationButtons/>
|
||||
|
||||
<span class="additional-link">{{ $t('page_registration.have_an_account') }}
|
||||
<router-link :to="{name: 'SignIn'}" class="text-theme">
|
||||
{{ $t('page_forgotten_password.password_remember_button') }}
|
||||
@@ -78,6 +80,7 @@
|
||||
import {ValidationProvider, ValidationObserver} from 'vee-validate/dist/vee-validate.full'
|
||||
import AuthContent from '/resources/js/components/Auth/AuthContent'
|
||||
import AuthButton from '/resources/js/components/Auth/AuthButton'
|
||||
import SocialiteAuthenticationButtons from '/resources/js/components/Auth/SocialiteAuthenticationButtons'
|
||||
import {required} from 'vee-validate/dist/rules'
|
||||
import {mapGetters} from 'vuex'
|
||||
import {events} from '/resources/js/bus'
|
||||
@@ -86,6 +89,7 @@
|
||||
export default {
|
||||
name: 'SignUp',
|
||||
components: {
|
||||
SocialiteAuthenticationButtons,
|
||||
AuthContentWrapper,
|
||||
ValidationProvider,
|
||||
ValidationObserver,
|
||||
|
||||
33
resources/js/views/Auth/SocialiteCallback.vue
Normal file
33
resources/js/views/Auth/SocialiteCallback.vue
Normal file
@@ -0,0 +1,33 @@
|
||||
<template>
|
||||
<div>
|
||||
<Spinner/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Spinner from '/resources/js/components/FilesView/Spinner'
|
||||
|
||||
export default {
|
||||
name: 'SocialiteCallback',
|
||||
components: {Spinner},
|
||||
created () {
|
||||
axios
|
||||
.get(`/api${this.$route.fullPath}`)
|
||||
.then(() => {
|
||||
|
||||
// Set login state
|
||||
this.$store.commit('SET_AUTHORIZED', true)
|
||||
|
||||
// Go to files page
|
||||
this.$router.push({name: 'Files'})
|
||||
})
|
||||
.catch((error) => {
|
||||
|
||||
this.$isSomethingWrong()
|
||||
|
||||
this.$router.push({name: 'Homepage'})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Domain\Zip\Controllers\ZipController;
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use Domain\Pages\Controllers\PagesController;
|
||||
use Domain\Sharing\Controllers\ShareController;
|
||||
use Domain\Trash\Controllers\DumpTrashController;
|
||||
@@ -23,6 +22,10 @@ use Domain\Browsing\Controllers\BrowseLatestFilesController;
|
||||
use Domain\Browsing\Controllers\BrowseSharedItemsController;
|
||||
use Domain\Browsing\Controllers\BrowseTrashContentController;
|
||||
use Domain\Homepage\Controllers\SendContactMessageController;
|
||||
use Domain\Browsing\Controllers\SearchFilesAndFoldersController;
|
||||
use App\Users\Controllers\Authentication\RegisterAuthenticationController;
|
||||
use App\Socialite\Controllers\SocialiteRedirectController;
|
||||
use App\Socialite\Controllers\SocialiteCallbackController;
|
||||
|
||||
// Pages
|
||||
Route::apiResource('/page', PagesController::class);
|
||||
@@ -32,7 +35,13 @@ Route::post('/contact', SendContactMessageController::class);
|
||||
Route::get('/settings', GetSettingsValueController::class);
|
||||
|
||||
// Register user
|
||||
Route::post('/register', CreateNewUserAction::class);
|
||||
Route::post('/register', RegisterAuthenticationController::class);
|
||||
|
||||
// Login via socialite
|
||||
Route::group(['prefix' => 'socialite'], function () {
|
||||
Route::get('/{provider}/redirect', SocialiteRedirectController::class);
|
||||
Route::get('/{provider}/callback', SocialiteCallbackController::class);
|
||||
});
|
||||
|
||||
// Password reset
|
||||
Route::group(['prefix' => 'password'], function () {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Socialite\Controllers;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use App\Users\DTO\CreateUserData;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
|
||||
class SocialiteCallbackController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected StatefulGuard $guard,
|
||||
public CreateNewUserAction $createNewUser,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke($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 {
|
||||
|
||||
$data = CreateUserData::fromArray([
|
||||
'name' => $provider_user->getname(),
|
||||
'email' => $provider_user->getEmail(),
|
||||
'avatar' => store_socialite_avatar($provider_user->getAvatar()),
|
||||
'oauth_provider' => $provider,
|
||||
]);
|
||||
|
||||
// Create User
|
||||
($this->createNewUser)($data);
|
||||
}
|
||||
|
||||
return response('Loged in', 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Socialite\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
|
||||
class SocialiteRedirectController extends Controller
|
||||
{
|
||||
public function __invoke($provider)
|
||||
{
|
||||
$url = Socialite::driver($provider)->stateless()->redirect()->getTargetUrl();
|
||||
|
||||
return response()->json([
|
||||
'url' => $url
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@ namespace App\Users\Actions;
|
||||
|
||||
use App\Users\Models\User;
|
||||
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;
|
||||
@@ -20,13 +20,14 @@ class CreateNewUserAction extends Controller
|
||||
/**
|
||||
* Validate and create a new user.
|
||||
*/
|
||||
public function __invoke(
|
||||
RegisterUserRequest $request
|
||||
): Application | ResponseFactory | Response {
|
||||
public function __invoke($data)
|
||||
{
|
||||
$settings = get_settings([
|
||||
'default_max_storage_amount', 'registration', 'user_verification',
|
||||
'storage_default', 'registration', 'user_verification',
|
||||
]);
|
||||
|
||||
$is_socialite = is_null($data->password);
|
||||
|
||||
// Check if account registration is enabled
|
||||
if (! intval($settings['registration'])) {
|
||||
abort(401);
|
||||
@@ -34,28 +35,33 @@ class CreateNewUserAction extends Controller
|
||||
|
||||
// Create user
|
||||
$user = User::create([
|
||||
'password' => bcrypt($request->input('password')),
|
||||
'email' => $request->input('email'),
|
||||
'password' => $is_socialite ? null : bcrypt($data->password),
|
||||
'oauth_provider' => $data->oauth_provider,
|
||||
'email' => $data->email,
|
||||
]);
|
||||
|
||||
// Mark as verified if verification is disabled
|
||||
if (! intval($settings['user_verification'])) {
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
UserSettings::unguard();
|
||||
|
||||
$user
|
||||
->settings()
|
||||
->create([
|
||||
'name' => $request->input('name'),
|
||||
'name' => $data->name,
|
||||
'storage_capacity' => $settings['storage_default'],
|
||||
'avatar' => $data->avatar,
|
||||
]);
|
||||
|
||||
UserSettings::reguard();
|
||||
|
||||
// Mark as verified if verification is disabled
|
||||
if ($is_socialite || ! intval($settings['user_verification'])) {
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
// Log in if verification is disabled
|
||||
if (! intval($settings['user_verification'])) {
|
||||
if ($is_socialite || ! intval($settings['user_verification'])) {
|
||||
$this->guard->login($user);
|
||||
}
|
||||
|
||||
return response('User registered successfully', 201);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,10 @@ class CheckAccountController extends Controller
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $user->settings->name,
|
||||
'avatar' => $user->settings->avatar,
|
||||
'verified' => $user->email_verified_at ? 1 : 0,
|
||||
'name' => $user->settings->name,
|
||||
'avatar' => $user->settings->avatar,
|
||||
'verified' => $user->email_verified_at ? 1 : 0,
|
||||
'oauth_provider' => $user->password ? null : $user->oauth_provider,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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;
|
||||
|
||||
class RegisterAuthenticationController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public CreateNewUserAction $createNewUser,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(RegisterUserRequest $request)
|
||||
{
|
||||
$data = CreateUserData::fromRequest($request);
|
||||
|
||||
($this->createNewUser)($data);
|
||||
}
|
||||
}
|
||||
36
src/App/Users/DTO/CreateUserData.php
Normal file
36
src/App/Users/DTO/CreateUserData.php
Normal file
@@ -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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
protected $fillable = [
|
||||
'email',
|
||||
'password',
|
||||
'oauth_provider',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
|
||||
@@ -35,6 +35,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' => format_date($this->created_at, '%d. %b. %Y'),
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use Carbon\Carbon;
|
||||
use ByteUnits\Metric;
|
||||
use App\Users\Models\User;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use Domain\Files\Models\File;
|
||||
use Domain\Sharing\Models\Share;
|
||||
@@ -358,14 +359,7 @@ if (! function_exists('store_avatar')) {
|
||||
$intervention = Image::make($image->getRealPath());
|
||||
|
||||
// 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);
|
||||
});
|
||||
generate_avatar_thumbnails($intervention, $avatar_name);
|
||||
|
||||
// Return path to image
|
||||
return $avatar_name;
|
||||
@@ -1021,4 +1015,46 @@ if (! function_exists('replace_occurrence')) {
|
||||
$string
|
||||
);
|
||||
}
|
||||
|
||||
if(! function_exists('get_socialite_avatar')) {
|
||||
/**
|
||||
* Get socialite avatar create and store his thumbnails
|
||||
*/
|
||||
function store_socialite_avatar($avatar)
|
||||
{
|
||||
// Get image from external source
|
||||
$image = Http::get($avatar)->body();
|
||||
|
||||
// Generate avatar name
|
||||
$avatar_name = Str::uuid() . '.jpg';
|
||||
|
||||
// Create intervention image
|
||||
$intervention = Image::make($image);
|
||||
|
||||
// Generate avatar sizes
|
||||
generate_avatar_thumbnails($intervention, $avatar_name);
|
||||
|
||||
// Return name of image
|
||||
return $avatar_name;
|
||||
}
|
||||
}
|
||||
|
||||
if(! function_exists('generate_avatar_thumbnails')) {
|
||||
/**
|
||||
* Create avatar thumbnails
|
||||
*/
|
||||
function generate_avatar_thumbnails($intervention, $avatar_name)
|
||||
{
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
96
tests/App/Socialite/SocialiteTest.php
Normal file
96
tests/App/Socialite/SocialiteTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\App\Socialite;
|
||||
|
||||
use DB;
|
||||
use Storage;
|
||||
use App\Users\Models\User;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Laravel\Socialite\Two\FacebookProvider;
|
||||
use Laravel\Socialite\Contracts\Factory as Socialite;
|
||||
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class SocialiteTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_socialite_redirect()
|
||||
{
|
||||
$response = $this->get('api/socialite/google/redirect');
|
||||
|
||||
$this->assertStringContainsString('accounts.google.com/o/oauth2/auth', $response['url']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_socialite_callback()
|
||||
{
|
||||
// Set default settings
|
||||
DB::table('settings')->insert([
|
||||
[
|
||||
'name' => 'registration',
|
||||
'value' => 1,
|
||||
], [
|
||||
'name' => 'storage_default',
|
||||
'value' => 5,
|
||||
]
|
||||
]);
|
||||
|
||||
// Create fake image
|
||||
$fakeImage = UploadedFile::fake()
|
||||
->image('fake-avatar.jpg');
|
||||
|
||||
Http::fake([
|
||||
'https://vuefilemanager.com/avatar.jpg' => Http::response($fakeImage->getContent()),
|
||||
]);
|
||||
|
||||
// Create fake user
|
||||
$socialiteUser = $this->createMock(\Laravel\Socialite\Two\User::class);
|
||||
$socialiteUser->token = 'fake_token';
|
||||
$socialiteUser->id = 'fake_id';
|
||||
$socialiteUser->name = 'Jane Doe';
|
||||
$socialiteUser->email = 'howdy@hi5ve.digital';
|
||||
$socialiteUser->avatar = 'https://vuefilemanager.com/avatar.jpg';
|
||||
|
||||
// Mock user with FB provider
|
||||
$provider = $this->createMock(FacebookProvider::class);
|
||||
$provider->expects($this->any())
|
||||
->method('user')
|
||||
->willReturn($socialiteUser);
|
||||
|
||||
// Mock socialite
|
||||
$stub = $this->createMock(Socialite::class);
|
||||
|
||||
$stub->expects($this->any())
|
||||
->method('driver')
|
||||
->willReturn($provider);
|
||||
|
||||
// Replace Socialite Instance with mock
|
||||
$this->app->instance(Socialite::class, $stub);
|
||||
|
||||
$this->getJson('/api/socialite/facebook/callback');
|
||||
|
||||
$this
|
||||
->assertDatabaseHas('users', [
|
||||
'email' => 'howdy@hi5ve.digital',
|
||||
'oauth_provider' => 'facebook',
|
||||
'password' => null,
|
||||
])
|
||||
->assertDatabaseHas('user_settings', [
|
||||
'name' => 'Jane Doe',
|
||||
]);
|
||||
|
||||
$user = User::first();
|
||||
|
||||
collect(config('vuefilemanager.avatar_sizes'))
|
||||
->each(
|
||||
fn($size) => Storage::disk('local')
|
||||
->assertExists("avatars/{$size['name']}-{$user->settings->getRawOriginal('avatar')}")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user