4

Firstly I'm a real beginner with Laravel so I will try to describe my problem as best as I can.

I am building a website using Laravel however the information on users will not be stored on my server but rather externally on another server.

The only way to access the user's data is through an external API; I am not allowed access to their database. The API request returns a token and I use this token to check with their server to see if the user is logged in.

My question is: how do I authenticate the user so that I can still use Laravel's out of the box guards.

It's really handy to use methods like Auth::check() to determine if the user is still logged in.

Colin Cheung
  • 148
  • 2
  • 13
  • You can see an example with jwt package here: https://github.com/tymondesigns/jwt-auth/wiki/Authentication – GONG Jun 29 '16 at 16:07
  • 1
    I had a look at this but this JWTAuth seems like its used to make your own API using Laravel. I am not trying to make my own tokens, but rather I am given one through the external API – Colin Cheung Jun 29 '16 at 16:54
  • Here is potentialy relevant answer to the question: https://laracasts.com/discuss/channels/laravel/authenticating-with-eloquent-and-without-database-how-to-handle-user-roles-and-permissions – gandra404 Jan 29 '17 at 09:47
  • Possible answer: https://stackoverflow.com/q/44021077/1665504 (FWIW its a bad idea) – Caio Wilson May 29 '19 at 05:19
  • Did you get it to work? – hugo411 Feb 21 '20 at 15:14
  • This has been answered here https://stackoverflow.com/questions/61980446/how-do-we-implement-custom-api-only-authentication-in-laravel/61980447#61980447 I hope it helps – quinny May 30 '20 at 01:20

1 Answers1

3

You'll either need to modify Laravel's default authentication middleware in app/Http/middleware/Authenticate.php or you'll need to create your own middleware class that runs the authentication that you need. Create a class in the app/Http/middleware folder and register that middleware. https://laravel.com/docs/master/middleware

roric32
  • 31
  • 2