├── src ├── AuthenServiceProvider.php ├── BootAuthenProvider.php ├── AuthenUser.php ├── Authen.php └── AuthenUserProvider.php └── composer.json /src/AuthenServiceProvider.php: -------------------------------------------------------------------------------- 1 | bootAuthenProvider(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/BootAuthenProvider.php: -------------------------------------------------------------------------------- 1 | make('hash'), $config['model']); 16 | }); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/AuthenUser.php: -------------------------------------------------------------------------------- 1 | getAuthIdentifiersName(); 17 | 18 | $query->where(static function ($query) use ($identifiers, $username) { 19 | foreach ($identifiers as $key) { 20 | $query->orWhere($key, '=', $username); 21 | } 22 | }); 23 | 24 | return $query; 25 | } 26 | 27 | /** 28 | * Get the name of the unique identifier for the user. 29 | * 30 | * @return array 31 | */ 32 | abstract public function getAuthIdentifiersName(): array; 33 | } 34 | -------------------------------------------------------------------------------- /src/Authen.php: -------------------------------------------------------------------------------- 1 | $credentials 15 | */ 16 | public function retrieveByCredentials(array $credentials): ?UserContract 17 | { 18 | $name = Authen::getIdentifierName(); 19 | 20 | if (! isset($credentials[$name]) || empty($credentials[$name])) { 21 | return parent::retrieveByCredentials($credentials); 22 | } 23 | 24 | $identifier = $credentials[$name]; 25 | unset($credentials[$name]); 26 | 27 | // First we will add each credential element to the query as a where clause. 28 | // Then we can execute the query and, if we found a user, return it in a 29 | // Eloquent User "model" that will be utilized by the Guard instances. 30 | 31 | $query = $this->createModel() 32 | ->newQuery() 33 | ->findByIdentifiers($identifier); 34 | 35 | foreach ($credentials as $key => $value) { 36 | if (! Str::contains($key, 'password')) { 37 | $query->where($key, $value); 38 | } 39 | } 40 | 41 | return $query->first(); 42 | } 43 | 44 | /** 45 | * Validate a user against the given credentials. 46 | * 47 | * @param array $credentials 48 | */ 49 | public function validateCredentials(UserContract $user, array $credentials): bool 50 | { 51 | $plain = $credentials['password']; 52 | $hashed = $user->getAuthPassword(); 53 | 54 | if (\strlen($hashed) === 0) { 55 | return false; 56 | } 57 | 58 | return password_verify($plain, $hashed) || $this->hasher->check($plain, $hashed); 59 | } 60 | } 61 | --------------------------------------------------------------------------------