Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reset password functionality #711

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

use App\Libraries\UpdateUtils;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\PasswordReset;

class AuthController extends Controller
{
public function showLogin()
Expand Down Expand Up @@ -32,4 +38,63 @@ public function postLogin()
return redirect()->route('login')->with('login_failed', 'Invalid Username/Password');
}
}

public function showForgotPassword()
{
return view('auth.forgot-password');
}

public function postForgotPassword(Request $request)
{
$request->validate(['email' => 'required|email']);

$status = Password::sendResetLink(
$request->only('email')
);

switch ($status) {
case Password::RESET_LINK_SENT:
return back()->with(['success' => __($status)]);
break;

case Password::INVALID_USER:
return back()->with(['error' => __($status)]);
break;

default:
return back()->withErrors(['error' => __($status)]);
break;
}
}

public function showResetPassword($token)
{
return view('auth.reset-password', ['token' => $token]);
}

public function postResetPassword(Request $request)
{
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed',
]);

$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));

$user->save();

event(new PasswordReset($user));
}
);

return $status === Password::PASSWORD_RESET
? redirect()->route('login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
}
9 changes: 8 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract

class User extends Model implements AuthenticatableContract,
CanResetPasswordContract
{
use Notifiable, CanResetPassword;

/**
* The database table used by the model.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
};
6 changes: 5 additions & 1 deletion public/css/login.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ ul li {
text-transform: uppercase;
letter-spacing: 1.5px;
cursor: pointer;
}
}

.text-center {
text-align: center;
}
39 changes: 39 additions & 0 deletions resources/views/auth/forgot-password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign in &middot; Technic Solder</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}">
<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet' type='text/css'>
<!-- Le styles -->
{!! Html::style('css/login.css') !!}
</head>
<body class="login">
<img alt="Technic-logo" class="logo" height="70" src="{{ URL::asset('img/wrenchIcon.svg') }}">
<form class="vertical-form" method="post" action="">
<div style="margin:0;padding:0;display:inline;">
<legend>
Technic Solder
</legend>
@if (Session::has('error'))
<ul class="notice errors">
<li>{{ Session::get('error') }}</li>
</ul>
@endif
@if (Session::has('success'))
<ul class="notice success">
<li>{{ Session::get('success') }}</li>
</ul>
@endif
<input type="text" name="email" class="input-block-level" placeholder="Email Address" size="30" autocomplete="off">
<input name="login" type="submit" value="Reset password">
<div class="footer">
<p><a href="https://www.technicpack.net/">Powered by the Technic Platform</a></p>
</div>
</div>
</form>
</body>
</html>
40 changes: 40 additions & 0 deletions resources/views/auth/reset-password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign in &middot; Technic Solder</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}">
<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet' type='text/css'>
<!-- Le styles -->
{!! Html::style('css/login.css') !!}
</head>
<body class="login">
<img alt="Technic-logo" class="logo" height="70" src="{{ URL::asset('img/wrenchIcon.svg') }}">
<form class="vertical-form" method="post" action="{{ route('password.update') }}">
<div style="margin:0;padding:0;display:inline;">
<legend>
Technic Solder
</legend>
@if (Session::has('errors'))
<ul class="notice errors">
<li>{{ $errors->first('email') }}</li>
<li>{{ $errors->first('password') }}</li>
<li>{{ $errors->first('password_confirmation') }}</li>
</ul>
@endif
@csrf
<input type="hidden" name="token" value="{{ $token }}">
<input type="text" class="input-block-level" name="email" placeholder="Email Address" required autofocus>
<input type="password" class="input-block-level" name="password" placeholder="Password" required autofocus>
<input type="password" class="input-block-level" name="password_confirmation" placeholder="Confirm Password" required autofocus>
<input name="login" type="submit" value="Reset Password">
<div class="footer">
<p><a href="https://www.technicpack.net/">Powered by the Technic Platform</a></p>
</div>
</div>
</form>
</body>
</html>
8 changes: 6 additions & 2 deletions resources/views/dashboard/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@
<li>{{ Session::get('login_failed') }}</li>
</ul>
@endif
@if (Session::has('logout'))
@if (Session::has('logout') || Session::has('status'))
<ul class="notice success">
<li>{{ Session::get('logout') }}</li>
<li>{{ Session::get('status') }}</li>
</ul>
@endif
<input type="text" name="email" class="input-block-level" placeholder="Email Address" size="30" autocomplete="off">
<input type="password" name="password" class="input-block-level" placeholder="Password" size="30" autocomplete="off">
<input name="login" type="submit" value="Log In">
<label class="checkbox"><input type="checkbox" name="remember" value="1">Remember me</label>
@if (Route::has('password.request'))
<p class="text-center"><a href="{{ route('password.request') }}">{{ __('Forgot Password?') }}</a></p>
@endif
<div class="footer">
<p><a href="https://www.technicpack.net/">Powered by the Technic Platform</a></p>
</div>
</div>
</form>
</body>
</html>
</html>
11 changes: 11 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,14 @@

return Redirect::route('login')->with('logout', 'You have been logged out.');
})->name('logout');

Route::middleware('guest')->group(function () {
Route::get('forgot-password', [AuthController::class, 'showForgotPassword'])->name('password.request');
Route::post('forgot-password', [AuthController::class, 'postForgotPassword'])->name('password.email');

Route::get('reset-password/{token}', [AuthController::class, 'showResetPassword'])->name('password.reset');
Route::post('reset-password', [AuthController::class, 'postResetPassword'])->name('password.update');
});