简体   繁体   中英

Laravel Auth external data for login and register

I am using the Laravel 5.2 Auth system coming up with this command :

php artisan make:auth

Although this works totally fine as is, my goal is to use an external API to perform login, registering and changing password while still being able to use the core function of the Auth class.

So taking the login for example, I want to use something like

function login(ApiController $api) {
     // This function return data or error code and message in JSON
     $login = $api->login([ $credentials['email'], $credentials['password']]);
     if($login->success) 
       // login successfully like normal Auth would do
     else 
       // redirect to main page with $login->message
}

By the way, I want to pass fields coming up from $login to the Auth class, like we can actually do Auth::user()->email giving us the email, I'd want to set value like "database field" but with my API JSON fields behind

I looked on the Internet and found something to do inside AuthController and something related to ServiceProvider, but I don't know how to follow my exact needs

Adding a custom user provider would help in this case. There is no need to play with AuthController here. Check this Laravel Docs page .

You will need to create a new User Provider which implements Illuminate\\Contracts\\Auth\\UserProvider , specify it in AuthServiceProvider and update your auth config file accordingly.

Here are the links to the framework's default User Providers for reference :

1) DatabaseUserProvider

2) EloquentUserProvider

I ended up coding my own Auth system...

Using session() and Input()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM