Skip to content

Commit

Permalink
Fifth commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkitRastogi238 committed Aug 10, 2021
1 parent 9a3c95b commit bd39d14
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 6 deletions.
71 changes: 71 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Models\VerifyUser;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Models\ResetPassword;
use Illuminate\Support\Carbon;
use App\Http\Controllers\Controller;
use App\Mail\ForgetPasswordMail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;

class LoginController extends Controller
{
Expand Down Expand Up @@ -53,4 +59,69 @@ public function verifyEmail($token)
return redirect(route('user.login'))->with('error', 'Something went wrong!!');
}
}

public function getForgetPassword()
{
return view('auth.forget_password');
}
public function postForgetPassword(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
]);

$user = User::where('email', $request->email)->first();

if (!$user) {
return redirect()->back()->with('error', 'User not found.');
} else {
$reset_code = Str::random(200);
ResetPassword::create([
'user_id' => $user->id,
'reset_code' => $reset_code
]);

Mail::to($user->email)->send(new ForgetPasswordMail($user->name, $reset_code));

return redirect()->back()->with('success', 'We have sent you a password reset link.Please check your mail.');
}
}

public function getResetPassword($reset_code)
{
$password_reset_code = ResetPassword::where('reset_code', $reset_code)->first();
if (!$password_reset_code || Carbon::now()->subMinutes(50) > $password_reset_code->created_at) {
return redirect()->route('getForgetPassword')->with('error', 'Invalid password reset link or link expired');
} else {
return view('emails.reset_password', compact('reset_code'));
}
}

public function postResetPassword($reset_code, Request $request)
{
$password_reset_code = ResetPassword::where('reset_code', $reset_code)->first();

if (!$password_reset_code || Carbon::now()->subMinutes(50) > $password_reset_code->created_at) {
return redirect()->route('getForgetPassword')->with('error', 'Invalid password reset link or link expired');
} else {
$request->validate([
'email' => 'required|email',
'password' => 'required',
'password_confirmation' => 'required|same:password',
]);

$user = User::find($password_reset_code->user_id);

if ($user->email != $request->email) {
return redirect()->back()->with('error', 'Enter Correct Email');
} else {
$password_reset_code->delete();
$user->update([
'password' => Hash::make($request->password)
]);

return redirect()->route('user.login')->with('success', 'Password succesfully reset');
}
}
}
}
40 changes: 40 additions & 0 deletions app/Mail/ForgetPasswordMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ForgetPasswordMail extends Mailable
{
public $user_name;
public $reset_code;

use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user_name, $reset_code)
{
$this->user_name = $user_name;
$this->reset_code = $reset_code;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.forget_pasword_mail')->with([
'user_name' => $this->user_name,
'reset_code' => $this->reset_code
]);
}
}
16 changes: 16 additions & 0 deletions app/Models/ResetPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ResetPassword extends Model
{
use HasFactory;

protected $fillable = [
'user_id',
'reset_code'
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateResetPasswordsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('reset_passwords', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('reset_code');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('reset_passwords');
}
}
5 changes: 4 additions & 1 deletion resources/css/app.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@tailwind base;

@tailwind components;
@tailwind utilities;

@tailwind utilities;

41 changes: 41 additions & 0 deletions resources/views/auth/forget_password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@extends('layouts.app')

@section('content')
<div class="flex justify-center">
<div class="w-4/12 bg-white p-6 rounded-lg">
@if (session('error'))
<div class="bg-red-500 p-4 rounded-lg mb-6 text-white text-center">
{{ session('error') }}
</div>
@endif
@if (session('success'))
<div class="bg-red-500 p-4 rounded-lg mb-6 text-white text-center">
{{ session('success') }}
</div>
@endif
@if (session('message'))
<div class="bg-red-500 p-4 rounded-lg mb-6 text-white text-center">
{{ session('message') }}
</div>
@endif
<form action="{{route('postForgetPassword')}}" method="POST">
@csrf

<div class="mb-4">
<label for="email" class="sr-only">Email</label>
<input type="text" name="email" id="email" placeholder="Your email" class="bg-gray-100 border-2 w-full p-4 rounded-lg @error('email') border-red-500 @enderror" value="{{ old('email') }}">

@error('email')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
@enderror
</div>

<div>
<button type="submit" class="bg-blue-500 text-white px-4 py-3 rounded font-medium w-full">Forget Password</button>
</div>
</form>
</div>
</div>
@endsection
3 changes: 2 additions & 1 deletion resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@
</div>
</div>

<div>
<div class="mb-4">
<button type="submit" class="bg-blue-500 text-white px-4 py-3 rounded font-medium w-full">Login</button>
</div>
<div class><a href="{{route('getForgetPassword')}}">Forgot Password ?</a></div>
</form>
</div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions resources/views/emails/forget_pasword_mail.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@component('mail::message')

Hello {{$user_name}}

Click here to reset your password

@component('mail::button', ['url' => route('getResetPassword',$reset_code)])
Reset Password
@endcomponent
<p>Or copy & paste the following link to your browser</p>
<p><a href="{{route('getResetPassword',$reset_code)}}">{{route('getResetPassword',$reset_code)}}</a></p>

Thanks,<br>
{{ config('app.name') }}
@endcomponent
47 changes: 47 additions & 0 deletions resources/views/emails/reset_password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@extends('layouts.app')

@section('content')
<div class="flex justify-center">
<div class="w-4/12 bg-white p-6 rounded-lg">
<form action="{{ route('postResetPassword',$reset_code) }}" method="post">
@csrf

<div class="mb-4">
<label for="email" class="sr-only">Email</label>
<input type="text" name="email" id="email" placeholder="Your email" class="bg-gray-100 border-2 w-full p-4 rounded-lg @error('email') border-red-500 @enderror" value="{{ old('email') }}">

@error('email')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
@enderror
</div>

<div class="mb-4">
<label for="password" class="sr-only">Password</label>
<input type="password" name="password" id="password" placeholder="Choose a password" class="bg-gray-100 border-2 w-full p-4 rounded-lg @error('password') border-red-500 @enderror" value="">

@error('password')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
@enderror
</div>

<div class="mb-4">
<label for="password_confirmation" class="sr-only">Confirm Password</label>
<input type="password" name="password_confirmation" id="password_confirmation" placeholder="Repeat your password" class="bg-gray-100 border-2 w-full p-4 rounded-lg @error('password_confirmation') border-red-500 @enderror" value="">

@error('password_confirmation')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
</div>
@enderror
</div>
<div>
<button type="submit" class="bg-blue-500 text-white px-4 py-3 rounded font-medium w-full">Submit</button>
</div>
</form>
</div>
</div>
@endsection
12 changes: 8 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
use App\Http\Controllers\PostLikeController;
use App\Http\Controllers\UserPostController;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\LogoutController;
use App\Http\Controllers\Auth\RegisterController;
use Illuminate\Foundation\Auth\EmailVerificationRequest;


Route::get('/', function () {
return view('home');
})->name('home');

// Auth::routes(['verify' => true]);

Route::get('/dashboard', [DashboardController::class, 'index'])
->name('dashboard');

Expand All @@ -27,6 +26,11 @@
Route::get('/login', [LoginController::class, 'index'])->name('user.login');
Route::post('/login/validate', [LoginController::class, 'validateLogin'])->name('user.validate');
Route::get('/user/verify/{token}', [LoginController::class, 'verifyEmail'])->name('user.verify');
Route::get('/forget-password', [LoginController::class, 'getForgetPassword'])->name('getForgetPassword');
Route::post('/forget-password', [LoginController::class, 'postForgetPassword'])->name('postForgetPassword');
Route::get('/reset-password/{reset_code}', [LoginController::class, 'getResetPassword'])->name('getResetPassword');
Route::post('/reset-password/{reset_code}', [LoginController::class, 'postResetPassword'])->name('postResetPassword');


Route::get('/register', [RegisterController::class, 'index'])->name('register');
Route::post('/register', [RegisterController::class, 'store']);
Expand Down

0 comments on commit bd39d14

Please sign in to comment.