How to make frontend api authentication in laravel?

Member

by addison , in category: PHP Frameworks , a month ago

How to make frontend api authentication in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 6 days ago

@addison 

To make frontend API authentication in Laravel, you can follow these steps:

  1. Install Laravel: If you haven't already, install Laravel using composer by running the following command:
1
composer create-project --prefer-dist laravel/laravel api-authentication


  1. Set up Passport: Laravel Passport is a package that provides API authentication using OAuth2. Install Passport by running the following commands:
1
2
3
composer require laravel/passport
php artisan migrate
php artisan passport:install


  1. Configure Passport: Add the Passport::routes() method within the boot method of your AuthServiceProvider (app/Providers/AuthServiceProvider.php).
1
2
3
4
5
6
use LaravelPassportPassport;

public function boot()
{
    Passport::routes();
}


  1. Create API routes: Create routes for API authentication in your routes/api.php file.
1
2
3
4
5
6
7
Route::post('login', 'AuthController@login');
Route::post('register', 'AuthController@register');

Route::middleware('auth:api')->group(function () {
    Route::get('user', 'AuthController@details');
    Route::post('logout', 'AuthController@logout');
});


  1. Create AuthController: Generate a new controller called AuthController using the following command:
1
php artisan make:controller AuthController


  1. Implement API authentication methods: In your AuthController, implement methods for login, register, user details, and logout using Passport's authentication methods.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use AppUser;
use IlluminateSupportFacadesAuth;
use Validator;

public function login()
{
    if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
        $user = Auth::user();
        $token = $user->createToken('MyApp')->accessToken;
        return response()->json(['token' => $token], 200);
    } else {
        return response()->json(['error' => 'Unauthorized'], 401);
    }
}

public function register(Request $request)
{
    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email',
        'password' => 'required'
    ]);

    if ($validator->fails()) {
        return response()->json(['error' => $validator->errors()], 401);
    }

    $input = $request->all();
    $input['password'] = bcrypt($input['password']);
    $user = User::create($input);
    $token = $user->createToken('MyApp')->accessToken;

    return response()->json(['token' => $token], 200);
}

public function details()
{
    $user = Auth::user();
    return response()->json(['user' => $user], 200);
}

public function logout()
{
    if (Auth::check()) {
        Auth::user()->token()->revoke();
        return response()->json(['message' => 'Successfully logged out'], 200);
    }
}


  1. Protect API routes: Use Passport's middleware to protect your API routes by adding the auth:api middleware in your routes or controller methods.
1
2
3
Route::middleware('auth:api')->group(function () {
    // Protected routes
});


  1. Make API requests from the frontend: Use libraries like Axios or Fetch to make API requests from your frontend application and handle authentication tokens accordingly.


That's it! You now have frontend API authentication set up in your Laravel project using Laravel Passport.