Add New registered user
This commit is contained in:
@@ -2,16 +2,24 @@
|
||||
|
||||
namespace App\Actions\Fortify;
|
||||
|
||||
use App\Actions\Teams\AcceptTeamInvite;
|
||||
use App\Concerns\PasswordValidationRules;
|
||||
use App\Concerns\ProfileValidationRules;
|
||||
use App\Models\TeamInvite;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Laravel\Fortify\Contracts\CreatesNewUsers;
|
||||
|
||||
class CreateNewUser implements CreatesNewUsers
|
||||
{
|
||||
use PasswordValidationRules, ProfileValidationRules;
|
||||
|
||||
public function __construct(
|
||||
private AcceptTeamInvite $acceptTeamInvite,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validate and create a newly registered user.
|
||||
*
|
||||
@@ -19,15 +27,43 @@ class CreateNewUser implements CreatesNewUsers
|
||||
*/
|
||||
public function create(array $input): User
|
||||
{
|
||||
Validator::make($input, [
|
||||
$validated = Validator::make($input, [
|
||||
...$this->profileRules(),
|
||||
'password' => $this->passwordRules(),
|
||||
'invite_token' => ['nullable', 'string', 'size:128'],
|
||||
])->validate();
|
||||
|
||||
return User::create([
|
||||
'name' => $input['name'],
|
||||
'email' => $input['email'],
|
||||
'password' => $input['password'],
|
||||
return DB::transaction(function () use ($validated): User {
|
||||
$invite = null;
|
||||
|
||||
if (! empty($validated['invite_token'])) {
|
||||
$invite = TeamInvite::findByPlainToken($validated['invite_token'])
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if (
|
||||
$invite === null
|
||||
|| mb_strtolower($invite->invited_email)
|
||||
!== mb_strtolower($validated['email'])
|
||||
) {
|
||||
throw ValidationException::withMessages([
|
||||
'invite_token' => 'Die Einladung ist ungültig oder gehört zu einer anderen E-Mail-Adresse.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$user = User::create([
|
||||
'name' => $validated['name'],
|
||||
'email' => $validated['email'],
|
||||
'password' => $validated['password'],
|
||||
]);
|
||||
|
||||
if ($invite !== null) {
|
||||
$invite->team->members()->attach($user);
|
||||
$invite->delete();
|
||||
}
|
||||
|
||||
return $user;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Team;
|
||||
use App\Models\TeamInvite;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -10,7 +11,6 @@ use Inertia\Inertia;
|
||||
|
||||
class TeamInviteController extends Controller
|
||||
{
|
||||
|
||||
public function store(Request $request, Team $team)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
@@ -19,9 +19,22 @@ class TeamInviteController extends Controller
|
||||
|
||||
abort_unless(auth()->user()->can('manage', $team), 403);
|
||||
|
||||
$invited = Arr::get($validated, 'email');
|
||||
$inviter = auth()->user()->id;
|
||||
|
||||
$exists = TeamInvite::whereInvitedEmail($invited)
|
||||
->whereTeamId($team->id)
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
Inertia::flash('warning', 'This Person is already invited to this team.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$team->invites()->create([
|
||||
'invited_email' => Arr::get($validated, 'email'),
|
||||
'invited_by_user_id' => auth()->user()->id,
|
||||
'invited_email' => $invited,
|
||||
'invited_by_user_id' => $inviter,
|
||||
]);
|
||||
|
||||
Inertia::flash('success', 'User invited to your team.');
|
||||
|
||||
@@ -60,6 +60,11 @@ class InviteToTeam extends Notification
|
||||
? 'register'
|
||||
: 'teams.invite.create',
|
||||
['inviteToken' => $invite->plainInviteToken()]
|
||||
+ (
|
||||
$user === null
|
||||
? ['email' => $invite->invited_email]
|
||||
: []
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -67,8 +67,10 @@ class FortifyServiceProvider extends ServiceProvider
|
||||
'status' => $request->session()->get('status'),
|
||||
]));
|
||||
|
||||
Fortify::registerView(fn () => Inertia::render('auth/Register', [
|
||||
Fortify::registerView(fn (Request $request) => Inertia::render('auth/Register', [
|
||||
'passwordRules' => Password::defaults()->toPasswordRulesString(),
|
||||
'inviteToken' => $request->query('inviteToken'),
|
||||
'email' => $request->query('email'),
|
||||
]));
|
||||
|
||||
Fortify::twoFactorChallengeView(fn () => Inertia::render('auth/TwoFactorChallenge'));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
<script lang="ts" setup>
|
||||
import {Form, Head} from '@inertiajs/vue3';
|
||||
import InputError from '@/components/InputError.vue';
|
||||
import PasswordInput from '@/components/PasswordInput.vue';
|
||||
@@ -12,6 +12,8 @@ import { store } from '@/routes/register';
|
||||
|
||||
defineProps<{
|
||||
passwordRules: string;
|
||||
inviteToken?: string;
|
||||
email?: string;
|
||||
}>();
|
||||
|
||||
defineOptions({
|
||||
@@ -26,23 +28,23 @@ defineOptions({
|
||||
<Head title="Register" />
|
||||
|
||||
<Form
|
||||
v-bind="store.form()"
|
||||
:reset-on-success="['password', 'password_confirmation']"
|
||||
v-slot="{ errors, processing }"
|
||||
:reset-on-success="['password', 'password_confirmation']"
|
||||
class="flex flex-col gap-6"
|
||||
v-bind="store.form()"
|
||||
>
|
||||
<div class="grid gap-6">
|
||||
<div class="grid gap-2">
|
||||
<Label for="name">Name</Label>
|
||||
<Input
|
||||
id="name"
|
||||
type="text"
|
||||
required
|
||||
autofocus
|
||||
:tabindex="1"
|
||||
autocomplete="name"
|
||||
autofocus
|
||||
name="name"
|
||||
placeholder="Full name"
|
||||
required
|
||||
type="text"
|
||||
/>
|
||||
<InputError :message="errors.name" />
|
||||
</div>
|
||||
@@ -51,12 +53,14 @@ defineOptions({
|
||||
<Label for="email">Email address</Label>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
required
|
||||
:model-value="email"
|
||||
:readonly="email !== null"
|
||||
:tabindex="2"
|
||||
autocomplete="email"
|
||||
name="email"
|
||||
placeholder="email@example.com"
|
||||
required
|
||||
type="email"
|
||||
/>
|
||||
<InputError :message="errors.email" />
|
||||
</div>
|
||||
@@ -65,12 +69,12 @@ defineOptions({
|
||||
<Label for="password">Password</Label>
|
||||
<PasswordInput
|
||||
id="password"
|
||||
required
|
||||
:passwordrules="passwordRules"
|
||||
:tabindex="3"
|
||||
autocomplete="new-password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
:passwordrules="passwordRules"
|
||||
required
|
||||
/>
|
||||
<InputError :message="errors.password" />
|
||||
</div>
|
||||
@@ -79,22 +83,32 @@ defineOptions({
|
||||
<Label for="password_confirmation">Confirm password</Label>
|
||||
<PasswordInput
|
||||
id="password_confirmation"
|
||||
required
|
||||
:passwordrules="passwordRules"
|
||||
:tabindex="4"
|
||||
autocomplete="new-password"
|
||||
name="password_confirmation"
|
||||
placeholder="Confirm password"
|
||||
:passwordrules="passwordRules"
|
||||
required
|
||||
/>
|
||||
<InputError :message="errors.password_confirmation" />
|
||||
</div>
|
||||
|
||||
<div class="grid gap-2">
|
||||
<PasswordInput
|
||||
id="invite_token"
|
||||
:model-value="inviteToken"
|
||||
name="invite_token"
|
||||
type="hidden"
|
||||
/>
|
||||
<InputError :message="errors.invite_token" />
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
class="mt-2 w-full"
|
||||
tabindex="5"
|
||||
:disabled="processing"
|
||||
class="mt-2 w-full"
|
||||
data-test="register-user-button"
|
||||
tabindex="5"
|
||||
type="submit"
|
||||
>
|
||||
<Spinner v-if="processing" />
|
||||
Create account
|
||||
@@ -105,8 +119,8 @@ defineOptions({
|
||||
Already have an account?
|
||||
<TextLink
|
||||
:href="login()"
|
||||
class="underline underline-offset-4"
|
||||
:tabindex="6"
|
||||
class="underline underline-offset-4"
|
||||
>Log in</TextLink
|
||||
>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user