Reputation: 11
I am learning now to use Laravel and Fortify to create an application. I wanted to create an Admin user that will create users from within the application. The app is not for public use, but an internal app as a company management tool. The login is working ok, but I have the register view in the app environment so, at the moment, any user can create other users (the app is in construction, not in use), but when I try to access to create a new user I get redirected to /home, and not to the register view. I understand this is because the user is already created and Fortify understands there is no need to register again (useful for public apps). Is there any way I can let users to register other users?? Thanks!!!
Upvotes: 0
Views: 1677
Reputation: 11
Finally I created an almost custom functionality. I created a resource UserController and added the registration form to a new view in the dashboard "Register user", added the link users.create to redirect to the registration view and included the {{ route('users.store') }} link in the form's action. Then, in the UserController's store method I used the code from Fortify of the RegisterUserController class, the method store, exactly the line "event(new Registered($user = $creator->create($request->all())));". And now I can register users from other users sessions!! Now I need to implement the roles so only admin users can register other users!!! Here is the code if you are interested on in!! Enjoy!!
routes/web.php
Route::resource('users', UserController::class);
App/Http/Controlles/UserController.php -> method store
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @param CreatesNewUsers $creator
* @return void
*/
public function store(Request $request, CreatesNewUsers $creator)
{
// Copied and pasted from the store method in the RegisterUserController class of Fortify
// The code in origin has been commented to avoid duplications
event(new Registered($user = $creator->create($request->all())));
}
navigation-dropdown.blade.php added one more link to the nav
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-jet-nav-link href="{{ route('users.create') }}" :active="request()->routeIs('dashboard')">
{{ __('Register user') }}
</x-jet-nav-link>
</div>
register-user.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Register user') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<div class="container p-5">
<form method="POST" action="{{ route('users.store') }}">
@csrf
<div>
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus autocomplete="name" />
</div>
<div class="mt-4">
<x-jet-label for="email" value="{{ __('Email') }}" />
<x-jet-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required />
</div>
<div class="mt-4">
<x-jet-label for="password" value="{{ __('Password') }}" />
<x-jet-input id="password" class="block mt-1 w-full" type="password" name="password" required autocomplete="new-password" />
</div>
<div class="mt-4">
<x-jet-label for="password_confirmation" value="{{ __('Confirm Password') }}" />
<x-jet-input id="password_confirmation" class="block mt-1 w-full" type="password" name="password_confirmation" required autocomplete="new-password" />
</div>
<div class="flex items-center justify-end mt-4">
<x-jet-button class="ml-4">
{{ __('Register user') }}
</x-jet-button>
</div>
</form>
</div>
</div>
</div>
</div>
Upvotes: 1