Users management with Laravel

Intro

  • Create the tree structure of your client application indicating all the cleint pages (html) and their potential link.

 

Part 1 – block the uri

Delievrable : otelo/mission4/…

  • create a view indicating that the user needs to sign in
@extends('layouts.app')

@section('content')
<div class="jumbotron">
    <h2 class="my-5 text-center"> authentification nécessaire </h2>
</div>
 @stop

 

  • in web.php, add middleware (‘auth’) on the route of creation of a booking
Route::get('/newReservation',[ReservationController::class, 'create'] ->middleware('auth');
Route::get('/failure',function () {
    return view('failure');
})->name('failure');
  • in middleware/Authenticate.php, modify the function
if (! $request->expectsJson()) {
            return route('failure');
        }
  • test the route, it is secured, sign in will be needed.

Note

It is also possible to secure all the routes of a controller by adding, in it :

public function __construct(){
        $this->middleware('auth');
    }

The middleware condition (‘auth’) in the route becomes useless.

 

Part 2 – authentication system

Deliverable : otelo/mission4/…

  • Generate the authentication skeleton
composer require laravel/ui
php artisan ui vue --auth
php artisan ui react --auth
php artisan migrate
  • Test the registration of a user with the uri register

 

  • Create the /auth/compte.blade.php view and the account uri
@extends('layouts.app')

@section('contenu')
    <div class="section">
        <h1 class="title is-1">Mon compte</h1>

        <p>Vous êtes bien connecté.</p>

        <a href="/deconnexion" class="button">Déconnexion</a>
    </div>
@endsection
  • Create the uri disconnection
Route::get('/deconnexion',function () {
   Auth::logout();
    return redirect('/');
});
  • Add the logout button to the uri /
  • Redirect the user to the root of the site by modifying the Login controller
//protected $redirectTo = RouteServiceProvider::HOME;
protected $redirectTo = '/';

 

 

Note

  • Test if the user is logged in
Auth::check();