Skip to content

Instantly share code, notes, and snippets.

@ssi-anik
Last active September 26, 2017 17:15
Show Gist options
  • Select an option

  • Save ssi-anik/f9e2b9ad4355b32e2fc60d0d74699e77 to your computer and use it in GitHub Desktop.

Select an option

Save ssi-anik/f9e2b9ad4355b32e2fc60d0d74699e77 to your computer and use it in GitHub Desktop.
<?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