Skip to content

Instantly share code, notes, and snippets.

@kevinruscoe
Last active January 1, 2021 15:23
Show Gist options
  • Save kevinruscoe/a8d3af69d8dee117062d253af77eb720 to your computer and use it in GitHub Desktop.
Save kevinruscoe/a8d3af69d8dee117062d253af77eb720 to your computer and use it in GitHub Desktop.
Array User Provider
<?php
namespace App;
use App\Models\User;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
class ArrayUserProvider implements UserProvider
{
private $users = [
[
'email' => 'bill@flowerpotmen.co.uk',
'password' => 'bensux'
],
[
'email' => 'ben@flowerpotmen.co.uk',
'password' => 'bensthebest'
]
];
public function __construct()
{
$this->castUsersToModels();
}
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
$user = array_filter($this->users, function($user) use ($identifier)
{
return $user->getAuthIdentifier() === $identifier;
});
return $this->getUserOrFail($user);
}
/**
* Retrieve a user by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token) {
throw new Exception("Unimplemented");
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token) {
throw new Exception("Unimplemented");
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials) {
$user = array_filter($this->users, function($user) use ($credentials)
{
return (
$user->getAuthIdentifier() === $credentials['email'] &&
$user->getAuthPassword() === $credentials['password']
);
});
return $this->getUserOrFail($user);
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials) {
return $user->getAuthPassword() === $credentials['password'];
}
private function getUserOrFail($candidate)
{
if (empty($candidate)) {
return null;
}
return reset($candidate); // return first
}
private function castUsersToModels()
{
$this->users = array_map(function($user){
return new User(
$user['email'],
$user['password']
);
}, $this->users);
}
}
<?php
namespace App;
use App\Models\User;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
class ArrayUserProvider implements UserProvider
{
private $users = [
[
'email' => 'bill@flowerpotmen.co.uk',
'password' => 'bensux'
],
[
'email' => 'ben@flowerpotmen.co.uk',
'password' => 'bensthebest'
]
];
public function __construct()
{
$this->castUsersToModels();
}
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
return $this->users->first(function($user) use ($identifier)
{
return $user->getAuthIdentifier() === $identifier;
});
}
/**
* Retrieve a user by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token)
{
throw new Exception("Unimplemented");
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token)
{
throw new Exception("Unimplemented");
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
return $this->users->first(function($user) use ($credentials)
{
return (
$user->getAuthIdentifier() === $credentials['email'] &&
$user->getAuthPassword() === $credentials['password']
);
});
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
return $user->getAuthPassword() === $credentials['password'];
}
/**
* Casts the array of usrrs to a collection of Users
*
* @return Collection
*/
private function castUsersToModels()
{
$this->users = collect($this->users)->map(function($user) {
return new User(
$user['email'],
$user['password']
);
});
}
}
<?php
namespace App;
use App\Models\User;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
class ArrayUserProvider implements UserProvider
{
private $users = [
[
'email' => 'bill@flowerpotmen.co.uk',
'password' => 'bensux'
],
[
'email' => 'ben@flowerpotmen.co.uk',
'password' => 'bensthebest'
]
];
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
$user = array_filter($this->users, function($user) use ($identifier)
{
return $user['email'] === $identifier;
});
return $this->getUserOrFail($user);
}
/**
* Retrieve a user by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token) {
throw new Exception("Unimplemented");
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token) {
throw new Exception("Unimplemented");
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials) {
$user = array_filter($this->users, function($user) use ($credentials)
{
return (
$user['email'] === $credentials['email'] &&
$user['password'] === $credentials['password']
);
});
return $this->getUserOrFail($user);
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials) {
return $user->getAuthPassword() === $credentials['password'];
}
public function getUserOrFail($candidate)
{
if (empty($candidate)) {
return null;
}
$candidate = reset($candidate); // first
return new User(
$candidate['email'],
$candidate['password']
);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function index(Request $request)
{
return view('auth.login');
}
public function create(Request $request)
{
$user = Auth::attempt([
'email' => $request->email,
'password' => $request->password,
]);
if ($user) {
return redirect()->to('/');
}
return back();
}
}
<form action="/login" method="post">
@csrf
<input type="text" name="email">
<input type="password" name="password">
<button type="submit">Login</button>
</form>
<?php
namespace App\Models;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
class User implements Authenticatable
{
protected $email;
protected $password;
public function __construct($email, $password) {
$this->email = $email;
$this->password = $password;
}
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName() {
return 'email';
}
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier() {
return $this->email;
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword() {
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken() {
throw new Exception("Unimplemented");
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value) {
throw new Exception("Unimplemented");
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName() {
throw new Exception("Unimplemented");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment