Last active
September 26, 2017 17:15
-
-
Save ssi-anik/f9e2b9ad4355b32e2fc60d0d74699e77 to your computer and use it in GitHub Desktop.
snippet 3 from https://github.com/ssi-anik/laravel-custom-auth for medium article - https://medium.com/@sirajul.anik/laravel-api-authenticate-user-with-custom-driver-different-table-using-auth-middleware-fa2cabec2d61
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace App\Extensions; | |
| use App\Token; | |
| use App\User; | |
| use Illuminate\Contracts\Auth\Authenticatable; | |
| use Illuminate\Contracts\Auth\UserProvider; | |
| use Illuminate\Support\Str; | |
| class TokenToUserProvider implements UserProvider | |
| { | |
| private $token; | |
| private $user; | |
| public function __construct (User $user, Token $token) { | |
| $this->user = $user; | |
| $this->token = $token; | |
| } | |
| public function retrieveById ($identifier) { | |
| return $this->user->find($identifier); | |
| } | |
| public function retrieveByToken ($identifier, $token) { | |
| $token = $this->token->with('user')->where($identifier, $token)->first(); | |
| return $token && $token->user ? $token->user : null; | |
| } | |
| public function updateRememberToken (Authenticatable $user, $token) { | |
| // update via remember token not necessary | |
| } | |
| public function retrieveByCredentials (array $credentials) { | |
| // implementation upto user. | |
| // how he wants to implement - | |
| // let's try to assume that the credentials ['username', 'password'] given | |
| $user = $this->user; | |
| foreach ($credentials as $credentialKey => $credentialValue) { | |
| if (!Str::contains($credentialKey, 'password')) { | |
| $user->where($credentialKey, $credentialValue); | |
| } | |
| } | |
| return $user->first(); | |
| } | |
| public function validateCredentials (Authenticatable $user, array $credentials) { | |
| $plain = $credentials['password']; | |
| return app('hash')->check($plain, $user->getAuthPassword()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment