Pārlūkot izejas kodu

Add password reset routes

tags/v0.1.0
Immanuel Onyeka pirms 3 gadiem
vecāks
revīzija
d0716a9516
6 mainītis faili ar 95 papildinājumiem un 6 dzēšanām
  1. +43
    -0
      app/Http/Controllers/UserController.php
  2. +2
    -0
      database/migrations/2014_10_12_000000_create_users_table.php
  3. +1
    -1
      resources/js/main.js
  4. +17
    -0
      resources/views/request-reset.blade.php
  5. +17
    -0
      resources/views/reset-password.blade.php
  6. +15
    -5
      routes/web.php

+ 43
- 0
app/Http/Controllers/UserController.php Parādīt failu

@@ -7,6 +7,7 @@ use App\Models\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Password;

class UserController extends Controller
{
@@ -21,6 +22,7 @@ class UserController extends Controller
$user = new User;
$user->name = $request->name;
$user->email = $request->email;
$user->role = "client";
$user->password = Hash::make($request->password);
$user->save();

@@ -29,4 +31,45 @@ class UserController extends Controller
"status" => "success"
]);
}

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

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

if ($status === Password::RESET_LINK_SENT) {
return response()->json([
"status" => "success"
])
} else {
return response()->json($errors->all())
}
}

public function resetPassword(Request $request) {

$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
]);

$status = Password::reset(
$request->only('email', 'password',
'password_confirmation', 'token'),
function ($user, $password) use ($request) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60))
});
if ($status == Password::PASSWORD_RESET) {
return response()->json([
"status" => "success"
]);
} else {
return response()->json([__($status)]);
}
}
}

+ 2
- 0
database/migrations/2014_10_12_000000_create_users_table.php Parādīt failu

@@ -19,6 +19,8 @@ class CreateUsersTable extends Migration
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('role');
$table->bigInteger('credits');
$table->rememberToken();
$table->timestamps();
});


+ 1
- 1
resources/js/main.js Parādīt failu

@@ -18,7 +18,7 @@ function register(event) {
'X-XSRF-TOKEN': token},
body: JSON.stringify({"name": document.getElementById("register-name").value,
"email": document.getElementById("register-email").value,
"password": document.getElementById("register-password").value})
"password": document.getElementById("register-password").value}),
"password_confirmation": document.getElementById("confirm-password").value})
}).then(response => {
console.log('sent registration');


+ 17
- 0
resources/views/request-reset.blade.php Parādīt failu

@@ -0,0 +1,17 @@
@EXTEnds('master')

@section('title', 'Password Reset')

@section('head-metas')
@parent
<link rel="stylesheet" href="{{asset('main.css')}}">
@endsection

@section('content')
Reset form goes here.
@endsection

@section('scripts')
@parent
<script src="main.js"></script>
@endsection

+ 17
- 0
resources/views/reset-password.blade.php Parādīt failu

@@ -0,0 +1,17 @@
@extends('master')

@section('title', 'Password Reset')

@section('head-metas')
@parent
<link rel="stylesheet" href="{{asset('main.css')}}">
@endsection

@section('content')
Reset form goes here.
@endsection

@section('scripts')
@parent
<script src="main.js"></script>
@endsection

+ 15
- 5
routes/web.php Parādīt failu

@@ -35,10 +35,20 @@ Route::post('/resend-verification', function (Request $request) {
$request->user()->sendEmailVerificationNotification();

return back()->with('message', 'Verification link sent!');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send')

Route::get('/login', function () {
return view('home');
});
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');

Route::post('/register', [UserController::class, 'create']);

Route::get('/forgot-password', function () {
return view('request-reset');
})->middleware('guest')->name('password.request');

Route::post('/forgot-passowrd', [UserController::class,
'forgotPassword'])->middleware('guest')->name('password.email');

Route::get('/reset-password/{token}', function ($token) {
return view('reset-password', ['token' => $token]);
})->middleware('guest')->name('password.reset');

Route::post('/reset-passowrd', [UserController::class,
'resetPassword'])->middleware('guest')->name('password.email');

Notiek ielāde…
Atcelt
Saglabāt