Skip to content

Instantly share code, notes, and snippets.

@WendellAdriel
Created March 26, 2019 15:12
Show Gist options
  • Save WendellAdriel/557c4c0733c02ff76998ea8f8aca6bfa to your computer and use it in GitHub Desktop.
Save WendellAdriel/557c4c0733c02ff76998ea8f8aca6bfa to your computer and use it in GitHub Desktop.
<?php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class UserProvider extends EloquentUserProvider
{
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('senha', $credentials))) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'senha')) {
$query->where($key, $value);
}
}
return $query->first();
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['senha'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment