Laravel Passport is a Laravel package that provides a simple and secure way to implement OAuth 2.0 authentication in your application. OAuth 2.0 is a popular authorization framework that allows users to grant third-party applications access to their protected resources without having to share their passwords.
There are many benefits to using Laravel Passport for authentication, including:
- It is easy to use. Laravel Passport provides a simple API that makes it easy to implement OAuth 2.0 authentication in your application.
- It is secure. Laravel Passport uses industry-standard security practices to protect your users’ credentials.
- It is scalable. Laravel Passport is designed to scale to meet the needs of even the most demanding applications.
Requirements for Laravel Passport
To implement Laravel Passport login, you will need the following:
- Laravel 8 or higher
- Composer
Installation
To install Laravel Passport, you can run the following command:
composer require laravel/passport
This will install the Laravel Passport package and its dependencies.
Configuration
Once Laravel Passport is installed, you will need to configure it in your config/auth.php
file. In the guards
section, add the following:
'api' => [ 'driver' => 'passport', ],
This tells Laravel that the api
guard will use Laravel Passport for authentication.
Creating a user model
Laravel Passport needs a user model to store the users’ credentials. You can create a user model by running the following command:
php artisan make:model User -m
This will create a new User
model and a migration file.
Open the User
model and extend the Laravel\Passport\AuthModel
class. This will allow Laravel Passport to interact with your user model.
Creating a login endpoint
Laravel Passport provides a login
endpoint that you can use to authenticate users. To create this endpoint, you can create a controller that extends the Laravel\Passport\Http\Controllers\AuthController
class.
In your controller, add the following method:
public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return $this->respondWithToken(Auth::user()->createToken()); } return response()->json(['error' => 'Unauthorized'], 401); }
This method will authenticate the user and return an access token if the credentials are valid. Have you ever get stucked in Laravel Development ? Contact me for Free
Creating a register endpoint
Laravel Passport also provides a register endpoint that you can use to register new users. To create this endpoint, you can create a controller that extends the Laravel\Passport\Http\Controllers\AuthController
class.
In your controller, add the following method:
public function register(Request $request) { $this->validate($request, [ 'name' => 'required|string', 'email' => 'required|email|unique:users', 'password' => 'required|string|min:6', ]); $user = User::create([ 'name' => $request->input('name'), 'email' => $request->input('email'), 'password' => bcrypt($request->input('password')), ]); return $this->respondWithToken($user->createToken()); }
This method will register a new user and return an access token.
Testing
Once you have implemented Laravel Passport login, you can test it by running the following commands:
php artisan make:test PassportTest php artisan test
This will create a new test class and run the tests.
Conclusion
This blog post has outlined the steps involved in implementing Laravel Passport login. By following these steps, you can easily add secure authentication to your Laravel API.
Reference Link :