Laravel Authentication

1. Install the Laravel UI package: 

Install the Laravel UI official package for making auth scaffolding in Laravel

 composer require laravel/ui


2. Generate auth scaffolding: 

After installation of Laravel UI package. We are now able to scaffold our auth with Bootstrap, Vue, React etc.

php artisan ui:auth


Solution of "Vite manifest not found":

1. composer require laravel/breeze

2. php artisan breeze:install


If reset password not working, giving error: "Call to undefined function mb_strcut()":

  1. sudo apt-get install php8.2-mbstring

(php version will be changed accordingly)



=====================================================================

Authentication system with Laravel Sanctum:

1. install laravel new project_name

2. make migration for user table

3. install laravel sanctum using composer

composer require laravel/sanctum

4. publish the Sanctum configuration and migration files

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

5.  Migrate database, Sanctum will create one database table in which to store API tokens.

php artisan migrate

6. Update Http Karnel: you should add Sanctum's middleware to your api middleware group within your application's app/Http/Kernel.php file

'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

7. Issuing API Tokens: Sanctum allows you to issue API tokens / personal access tokens that may be used to authenticate API requests to your application.

use
    Laravel\Sanctum\HasApiTokens;

  class User extends Model

  {
 
  use
  HasApiTokens, HasFactory, Notifiable;

  }

8. Token creation: 

$token= $user->createToken('loginToken')->plainTextToken;


9. Protecting Routes: 

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

Group Route:

Route::middleware(['auth:sanctum'])->group(function () {
    Route::get('student', [StudentController::class, 'index']);
    Route::get('student/{id}', [StudentController::class, 'view']);
});


10. Logout by deleting Token:

auth()->user()->tokens()->delete();

Post a Comment

0 Comments

Visual Studio