Search code examples
angularlaravellaravel-passport

Use Laravel Passport with Angular 6


I have a Laravel application in which I'm using Passport for authentication. I have multiple questions about the Passport authentication:

  • Why does Passport store all tokens ( in table oauth_access_tokens ) even if the user has a new token and can I avoid this huge storage?
  • How to consume this token in Angular part? Can I just save it in LocalStorage or Cookies and redirect the user? But how to verify if it's a valid token?

Thanks


Solution

  • Tokens can be removed by using one of the artisan commands that come with it:

    php artisan passport:purge --expired
    

    So you could call this artisan command using the scheduler, say once a day.

    See https://laravel.com/docs/7.x/passport#purging-tokens for additional info.

    As for your second question, well there are various ways to do that. One way is explained here https://laravel.com/docs/7.x/passport#consuming-your-api-with-javascript , which explains how you can add a middleware which attaches a laravel_token cookie to each request. This cookie is then automatically sent from your JavaScript requests, and will authenticate the user.