1

I'm new to api authentication (passport) in laravel. Is it possible to guard api routes using

$this->middleware('auth:api');

even if used the laravel built in authentication (php artisan make:auth)?

8
  • Yes you can, here is a good tutorial medium.com/modulr/… Commented Sep 26, 2018 at 7:16
  • page not found. Commented Sep 26, 2018 at 7:16
  • It's working my end, try medium.com/modulr/… Commented Sep 26, 2018 at 7:19
  • he make his own controller for authentication. My question is, is it possible to guard the api route even if used the php artisan make:auth Commented Sep 26, 2018 at 7:23
  • 1
    @Beginner yes you can guard your api with auth:api, you just need to change the api guard driver to passport in config/auth.php Commented Sep 26, 2018 at 9:05

1 Answer 1

1

you can guard your api with auth:api, just need to change the api guard driver to passport in config/auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            //'driver' => 'token',
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],
Sign up to request clarification or add additional context in comments.

7 Comments

I see, sometimes it confused me because i see other people make their own controller for authentication to get the response token
they created it like this $tokenResult = $user->createToken('Personal Access Token'); on their login controller. And use the response token to make an api call on frontend.
Now how can I get the token in frontend, If I used the laravel's built in auth?
to generate token, either you have to use passport endpoint or need to create your own controller. Check this link stackoverflow.com/questions/50848892/…
not necessary always, check laravel passport has different route endpoints github.com/laravel/passport/blob/2.0/src/RouteRegistrar.php and can be use it to generate token. As i said depend on requirement
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.