Skip to content

Commit 24bc97a

Browse files
authored
Update account (#7)
* Create AccountController * Add Vue component for updating account * Allow updating of account password * Move Login and Register components into directories to keep things consistent * Add AccountController tests
1 parent 03a7db2 commit 24bc97a

File tree

15 files changed

+352
-11
lines changed

15 files changed

+352
-11
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Requests\Account\AccountUpdate;
6+
use App\Http\Resources\UserResource;
7+
use Illuminate\Support\Facades\Auth;
8+
use Inertia\Inertia;
9+
10+
class AccountController extends Controller
11+
{
12+
public function edit()
13+
{
14+
$user = UserResource::make(Auth::user());
15+
16+
return Inertia::render('Account/Edit', [
17+
'first_name' => $user->first_name,
18+
'last_name' => $user->last_name,
19+
'email' => $user->email,
20+
]);
21+
}
22+
23+
public function update(AccountUpdate $request)
24+
{
25+
$user = $request->user();
26+
27+
$user->update($request->only('first_name', 'last_name', 'email'));
28+
$user->updatePassword($request->validated('password'));
29+
30+
return redirect()->route('account.edit')->with('notice', [
31+
'type' => 'success',
32+
'message' => 'Your account has been updated.',
33+
]);
34+
}
35+
}

app/Http/Controllers/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function show(Request $request)
1616
{
1717
$isProd = App::environment('production');
1818

19-
return Inertia::render('Login', [
19+
return Inertia::render('Login/Show', [
2020
'email' => !$isProd ? \env('SEED_ADMIN_EMAIL') : '',
2121
'password' => !$isProd ? '12345' : '',
2222
'remember' => !$isProd ? true : false,

app/Http/Controllers/RegisterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function show()
1616
{
1717
$isProd = App::environment('production');
1818

19-
return Inertia::render('Register', [
19+
return Inertia::render('Register/Show', [
2020
'first_name' => !$isProd ? 'Jim' : '',
2121
'last_name' => !$isProd ? 'Gordon' : '',
2222
'email' => !$isProd ? 'test@test.com' : '',
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Account;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Validation\Rules\Password;
8+
9+
class AccountUpdate extends FormRequest
10+
{
11+
public function rules()
12+
{
13+
return [
14+
'first_name' => ['required', 'string', 'max:255'],
15+
'last_name' => ['required', 'string', 'max:255'],
16+
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,' . Auth::id()],
17+
'password' => [
18+
'nullable',
19+
Password::min(6)
20+
->mixedCase()
21+
->numbers()
22+
->symbols()
23+
->uncompromised(),
24+
],
25+
];
26+
}
27+
}

app/Models/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ public function currentOrganisation()
5858
{
5959
return $this->belongsTo(Organisation::class);
6060
}
61+
62+
public function updatePassword(?string $new_password = '')
63+
{
64+
if ($new_password && $new_password != $this->password) {
65+
$this->password = $new_password;
66+
67+
$this->save();
68+
}
69+
}
6170
}

resources/js/Layouts/App.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
<div class="app-page">
77
<div class="app-page__inner">
8+
<Link :href="route('home')">Home</Link>
9+
<br>
10+
<Link :href="route('account.edit')">Account</Link>
11+
<br>
12+
<Link :href="route('logout')" method="post" as="button"
13+
>Logout</Link
14+
>
15+
816
<slot />
917
</div>
1018
</div>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<template>
2+
<Head :title="title" />
3+
4+
<h1 v-text="title"></h1>
5+
6+
<form class="form" @submit.prevent="login">
7+
<div class="form__section">
8+
<div class="form__row">
9+
<div class="form__item">
10+
<label class="form__label" for="first_name"
11+
>First Name</label
12+
>
13+
<input
14+
id="first-name"
15+
type="text"
16+
v-model="form.first_name"
17+
tabindex="1"
18+
required
19+
/>
20+
</div>
21+
</div>
22+
23+
<div class="form__row">
24+
<div class="form__item">
25+
<label class="form__label" for="last_name">Last Name</label>
26+
<input
27+
id="last-name"
28+
type="text"
29+
v-model="form.last_name"
30+
tabindex="2"
31+
required
32+
/>
33+
</div>
34+
</div>
35+
36+
<div class="form__row">
37+
<div class="form__item">
38+
<label class="form__label" for="email">Email</label>
39+
<input
40+
id="email"
41+
type="email"
42+
v-model="form.email"
43+
tabindex="3"
44+
required
45+
/>
46+
</div>
47+
</div>
48+
49+
<div class="form__row">
50+
<div class="form__item">
51+
<label class="form__label" for="password">Password</label>
52+
<input
53+
id="password"
54+
type="password"
55+
v-model="form.password"
56+
tabindex="4"
57+
/>
58+
</div>
59+
</div>
60+
61+
<div class="form__row">
62+
<div class="form__item">
63+
<AppButton
64+
text="Update"
65+
tabindex="5"
66+
:disabled="form.processing"
67+
/>
68+
</div>
69+
</div>
70+
</div>
71+
</form>
72+
</template>
73+
74+
<script>
75+
import AppLayout from "@js/Layouts/App.vue";
76+
import AppButton from "@js/Components/AppButton.vue";
77+
78+
import { useForm } from "@inertiajs/inertia-vue3";
79+
80+
export default {
81+
layout: AppLayout,
82+
83+
components: {
84+
AppButton,
85+
},
86+
87+
props: {
88+
first_name: {
89+
type: String,
90+
default: "",
91+
},
92+
last_name: {
93+
type: String,
94+
default: "",
95+
},
96+
email: {
97+
type: String,
98+
default: "",
99+
},
100+
},
101+
102+
data() {
103+
return {
104+
title: "Update Account",
105+
form: useForm({
106+
first_name: this.first_name,
107+
last_name: this.last_name,
108+
email: this.email,
109+
password: "",
110+
}),
111+
};
112+
},
113+
114+
methods: {
115+
login() {
116+
let form = this.form;
117+
form.patch(route("account.update"), {
118+
...form,
119+
...{
120+
onSuccess: () => {
121+
form.clearErrors();
122+
},
123+
},
124+
});
125+
},
126+
},
127+
};
128+
</script>
File renamed without changes.
File renamed without changes.

resources/js/pages/Index.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
<h1 v-text="title"></h1>
55

66
<p v-if="userCan($page.props, 'create-posts')">User can create posts.</p>
7-
8-
<Link :href="route('logout')" method="post" as="button">Logout</Link>
97
</template>
108

119
<script>

0 commit comments

Comments
 (0)