Skip to content

Commit

Permalink
change password
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismael G Marin C committed Feb 11, 2020
1 parent 3321f23 commit 8259e30
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/Controllers/Http/UserController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
"use strict";
const { validateAll } = use("Validator");
const Hash = use("Hash");
class UserController {
show({ view }) {
return view.render("users.show");
}

showPassword({ view }) {
return view.render("users.show_password");
}

async editPassword({ request, session, response, auth }) {
const data = request.only([
"current_password",
"new_password",
"new_password_confirmation"
]);

const validation = await validateAll(data, {
new_password: "required|confirmed",
current_password: "required"
});

if (validation.fails()) {
session
.withErrors(validation.messages())
.flashExcept([
"current_password",
"new_password",
"new_password_confirmation"
]);

return response.redirect("back");
}

const verifyPassword = await Hash.verify(
data.current_password,
auth.user.password
);

if (!verifyPassword) {
session.flash({
notification: {
type: "danger",
message: "Password doesn't match"
}
});
return response.redirect("back");
}

auth.user.password = data.new_password;
await auth.user.save();
session.flash({
notification: {
type: "success",
message: "Password changed"
}
});

return response.redirect("/");
}

async edit({ request, session, response, auth }) {
const data = request.only(["name", "email"]);

Expand Down
69 changes: 69 additions & 0 deletions resources/views/users/show_password.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@layout('layouts.app')

@section('content')
@set('title', 'Change Password')

<section class="section">
<div class="container">
<div class="columns">
<div class="column is-3">
@include('users.partials._settings_menu')
</div>
<div class="column is-9">
<div class="box">
<h2 class="title">Change Password</h2>

<hr>

<form action="/settings/password?_method=PUT" method="post">
{{ csrfField() }}

<div class="field">
<label class="label">Current Password</label>

<div class="control">
<input
type="password"
class="input {{ hasErrorFor('current_password') ? 'is-danger' : '' }}"
name="current_password">
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('current_password'), hasErrorFor('current_password')) }}
</div>

<div class="field">
<label class="label">New Password</label>

<div class="control">
<input
type="password"
class="input {{ hasErrorFor('new_password') ? 'is-danger' : '' }}"
name="new_password">
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('new_password'), hasErrorFor('new_password')) }}
</div>

<div class="field">
<label class="label">Confirm Password</label>

<div class="control">
<input
type="password"
class="input {{ hasErrorFor('new_password_confirmation') ? 'is-danger' : '' }}"
name="new_password_confirmation">
</div>

{{ elIf('<p class="help is-danger">$self</p>', getErrorFor('new_password_confirmation'), hasErrorFor('new_password_confirmation')) }}
</div>

<div class="control">
<button type="submit" class="button is-primary">Change Password</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
@endsection
2 changes: 2 additions & 0 deletions start/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ Route.post("password/reset", "Auth/PasswordResetController/reset");
Route.group(() => {
Route.get("/account", "UserController.show").as("settings.account");
Route.put("/account", "UserController.edit");
Route.get("/password", "UserController.showPassword").as("settings.password");
Route.put("/password", "UserController.editPassword");
}).prefix("settings");

0 comments on commit 8259e30

Please sign in to comment.