Skip to content

Commit

Permalink
Revert back register to MVC route
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 14, 2024
1 parent 5d06acd commit 92a2f70
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
class RegisteredUserController extends Controller
{

/**
* Display the registration view.
*/
public function create(): View
{
return view('auth.register');
}
/**
* Handle an incoming registration request.
*
Expand Down
22 changes: 17 additions & 5 deletions app/Livewire/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,38 @@
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;
use Illuminate\View\View;
use Livewire\Attributes\Validate;
use Livewire\Component;

class Register extends Component
{

public $name;
public $email;
public $phone;
public $location;
public $password;
public $password_confirmation;

public function register(): Application|Redirector|\Illuminate\Contracts\Foundation\Application|RedirectResponse
public function mount(): void
{
$this->location = $this->getUserCountry();
}

public function getUserCountry(): string
{
try {
$response = Http::get('https://ipinfo.io/json');
$data = $response->json();
return $data['country'] ?? 'Unknown';
} catch (\Exception $e) {
return 'Unknown';
}
}

public function register(): Application|Redirector|\Illuminate\Contracts\Foundation\Application|RedirectResponse
{
$user_data_array = [
'name' => $this->name,
'email' => $this->email,
Expand All @@ -38,8 +52,6 @@ public function register(): Application|Redirector|\Illuminate\Contracts\Foundat
'password' => Hash::make($this->password),
];

dd($user_data_array);

$this->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
Expand Down
32 changes: 8 additions & 24 deletions resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div>
<x-guest-layout>
<form wire:submit.prevent="register">
@csrf

<!-- Name -->
<div>
<x-input-label for="name" :value="__('Name')"/>
Expand All @@ -14,14 +13,14 @@
<div>
<div class="flex items-end">
<!-- Phone Number -->
<div data-tip="Make sure we can contact you" class="mt-4 tooltip w-1/2 mr-2">
<x-input-label for="phone" :value="__('Phone Number')"></x-input-label>
<div data-tip="Make sure we can contact you" class="mt-4 flex flex-col justify-start tooltip w-1/2 mr-2">
<x-input-label class="text-start mb-1" for="phone" :value="__('Phone Number')"></x-input-label>
<x-text-input id="phone" class="block tooltip mt-1 w-full" type="tel" wire:model="phone" required
autocomplete="phone"/>
</div>

<!-- Location -->
<div class="mt-4 w-1/2 ml-2">
<div class="mt-4 ml-2">
<x-input-label for="location" :value="__('Location')"></x-input-label>
<x-text-input id="location" disabled class="block mt-1 w-full" type="text" wire:model="location" required
autocomplete="location"/>
Expand Down Expand Up @@ -71,28 +70,15 @@
</div>
</form>

@script
<script>
let phoneInputField = document.querySelector("#phone");
let phoneInput = window.intlTelInput(phoneInputField, {
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
initialCountry: "auto",
geoIpLookup: function (success, failure) {
fetch("https://ipinfo.io", {
headers: {
"Accept": "application/json",
}
}).then((resp) => {
if (resp.ok) {
return resp.json();
}
throw new Error("Failed to fetch location");
}).then((data) => {
success(data.country);
alert(data.country)
}).catch(() => {
failure("US");
});
data = {
ip: "{{ request()->ip() }}"
};
},
});
Expand All @@ -101,8 +87,6 @@
let countryData = phoneInput.getSelectedCountryData();
document.querySelector("#location").value = countryData.name;
// update wire location
@this.set('location', countryData.name);
});
</script>
@endscript
</div>
</x-guest-layout>
2 changes: 1 addition & 1 deletion routes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Illuminate\Support\Facades\Route;

Route::middleware('guest')->group(function () {
Route::get('register', Register::class)
Route::get('register', [RegisteredUserController::class, 'create'])
->name('register');

Route::post('register', [RegisteredUserController::class, 'store']);
Expand Down

0 comments on commit 92a2f70

Please sign in to comment.