PATH:
home
/
rwabteecom
/
project_11
/
app
/
Http
/
Controllers
/
User
/
Web
/
Editing: GoogleController.php
<?php namespace App\Http\Controllers\User\Web; use App\Enums\UserEnum; use App\Http\Controllers\Controller; use App\Http\Requests\User\User\UserRequest; use App\Models\Domain; use App\Models\DomainTheme; use App\Models\Theme; use App\Models\User; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Laravel\Socialite\Facades\Socialite; use Illuminate\Support\Str; class GoogleController extends Controller { public function redirectToGoogle($id = 'google') { return Socialite::driver($id)->redirect(); } public static function quickRandom($length = 16) { $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; return substr(str_shuffle(str_repeat($pool, $length)), 0, $length); } public function handleGoogleCallback($id = 'google') { $googleUser = Socialite::driver($id)->stateless()->user(); $user = User::where('email', $googleUser->email)->first(); if (!$user) { $domain = Domain::create( [ 'domain' => $this->quickRandom(5) ] ); $user = User::create([ 'name' => $googleUser->name, 'domain_id' => $domain->id, 'email' => $googleUser->email, 'is_owner' => 1, 'email_verified_at' => Carbon::now(), 'password' => \Hash::make(rand(100000, 999999)) ]); $fileName = Str::uuid() . "-" . Str::slug(UserEnum::PROFILE_IMAGE->value) . "." . pathinfo($googleUser->avatar, PATHINFO_EXTENSION); $user->addMediaFromUrl($googleUser->avatar)->usingFileName($fileName)->toMediaCollection(UserEnum::PROFILE_IMAGE->value); $theme = Theme::first(); $theme_id = $theme->id; if ($theme) { $data = Arr::except($theme, ["id", 'updated_at', 'created_at', 'deleted_at', 'status'])->toArray(); $data['theme_id'] = $theme_id; $data['domain_id'] = $domain->id; $DomainTheme = DomainTheme::create($data); $domain->update(['domain_theme_id' => $DomainTheme->id]); } } Auth::login($user); return redirect()->route('dashboard.index'); } }
SAVE
CANCEL