Home Angular 9 Angular 9 Laravel 7 Auth Login working tutorial Part 3

Angular 9 Laravel 7 Auth Login working tutorial Part 3

by therichpost
10 comments
login laravel api

Hello to all, welcome to therichpost.com. In this post, I will continue with Angular 9 Laravel 7 Auth Login working tutorial Part 3.

Here you can check the first and second part of this post from where, you can get the information regarding:

1. How to install and run Angular 9: Part1
2. How to install Laravel 7 and Laravel 7 auth setup: Part2

Post Working:

In this post, I will continue with laravel and I am adding Laravel Passport module because During API connection with Laravel and Angular, we will also need security with proper authentication and for this I am using laravel Passport and it will give us access token which will help us to check login user authentication.

In this post, I will use Laravel Passport Package to create api auth and here is the full and easy process:

1. Very first, we need to install Laravel 7 Passport Package by run below command into your terminal:
composer require laravel/passport
2. After run above command, you need to add below code into your config/app.php file:
'providers' => [
                .... 
                Laravel\Passport\PassportServiceProvider::class,
                ....
               ],
3. We need to run migration command to add Passport package table in our database:
php artisan migrate
4. After successfully run above command, you can see below tables in your database:
laravel migration tables
5. Next, we need to install passport with run below command into your terminal:

This will create security token keys:

php artisan passport:install
6. After run above command, we need to update app/User.php file with below code:
<?php

...
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    ...
    use HasApiTokens, Notifiable;

    ...
}
7.  After, update your app/Providers/AuthServiceProvider.php file with below code:
<?php

...
use Laravel\Passport\Passport;
...

class AppServiceProvider extends ServiceProvider
{
    ...
    public function boot()
    {
        Schema::defaultStringLength(191);
        Passport::routes();
    }

    ...
}
8.  After it, above your config/auth.php with below code:
return [ 
        ..... 
        'guards' => 
                 [ 'web' => 
                         [ 'driver' => 'session',
                           'provider' => 'users', 
                         ], 
                   'api' => 
                          [ 'driver' => 'passport', 
                            'provider' => 'users', 
                          ], 
                 ], 
         .....
      ]
9.  Finally, we need to create our API’S Routes with below code in routes/api.php:
<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::post('login', 'API\AuthController@login');
Route::post('register', 'API\AuthController@register');
10.  We need to create API Folder Into our app\Http\Controllers:
api folder
11. In app\Http\Controllers\API Folder, we need to create new file named AuthController.php file and add below code into this file:
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Auth; 
use App\User; 
use Validator;
class AuthController extends Controller 
{
  /** 
   * Login API 
   * 
   * @return \Illuminate\Http\Response 
   */ 
  public function login(Request $request){ 
    if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){ 
      $user = Auth::user(); 
      $success['token'] =  $user->createToken('LaraPassport')->accessToken; 
      return response()->json([
        'status' => 'success',
        'data' => $success
      ]); 
    } else { 
      return response()->json([
        'status' => 'error',
        'data' => 'Unauthorized Access'
      ]); 
    } 
  }

  /** 
   * Register API 
   * 
   * @return \Illuminate\Http\Response 
   */ 
  public function register(Request $request) 
  { 
    $validator = Validator::make($request->all(), [ 
      'name' => 'required', 
      'email' => 'required|email', 
      'password' => 'required', 
      'c_password' => 'required|same:password', 
    ]);
    if ($validator->fails()) { 
      return response()->json(['error'=>$validator->errors()]);
    }
    $postArray = $request->all(); 
    $postArray['password'] = bcrypt($postArray['password']); 
    $user = User::create($postArray); 
    $success['token'] =  $user->createToken('LaraPassport')->accessToken; 
    $success['name'] =  $user->name;
    return response()->json([
      'status' => 'success',
      'data' => $success,
    ]); 
  }
}
  1. After all this, we need to test our’s api’s by downloading the postman software:
https://www.getpostman.com/apps
16.  Here I tested Register Api by enter name, email, password, and c_password. c_password details:
register api laravel
17. Here, I tested Login Api:
login laravel api

Now, we are done with Rest Api Authentication in Laravel 7 Using Passport Package . If you have any query related to this post, then do comment below or ask questions.

After this, I will also tell you, how we can create custom token without passport.

Jassa

Thank you

You may also like

10 comments

jihene May 2, 2020 - 4:23 pm

hello, i need help 🙂 when i tested Register in postman , i have
“error”: {
“password-C”: [
“The password- c field is required.”
]

Reply
Ajay Malhotra May 2, 2020 - 4:26 pm

because you are sending details with empty password.

Reply
jihene May 4, 2020 - 8:48 pm

hello , i have this error
BadMethodCallException: Method Illuminate\Auth\RequestGuard::attempt does not exist

Reply
Ajay Malhotra May 5, 2020 - 4:58 am

Seems, you using some other methods?

Reply
gbonline June 2, 2020 - 11:10 am

I needed to add the following line to app/Providers/AuthServiceProvider.php

use Illuminate\Support\Facades\Schema;

Reply
Ajay Malhotra June 2, 2020 - 11:14 am

Okay fine

Reply
gbonline June 2, 2020 - 11:25 am

please complete the tutorial with part 4 where you join angular login / register pages with laravel backend done in part3 ? And please also add an angular example page with limited access to logged users

Reply
Ajay Malhotra June 2, 2020 - 11:34 am

Okay sure and I will come very soon. Thanks

Reply
Leonardo Penkz June 28, 2020 - 2:30 am

nice, thkz for the direct to the point explanation… this saves me a lot of time…

Reply
Ajay Malhotra June 28, 2020 - 6:15 am

Thank you and welcome.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.