php - Laravel 4 - Hardcoded authentication -
i want create authentication mechanism without need database 1 person (admin) knows right username , password (which hardcode) able login. still want use auth::attempt(), auth::check() , other functions.
i found out create own user driver, seems me there should simpler.
maybe not nice solution, want simple possible website.
it may seem there should simpler, in fact that's simple can if want extend authentication system. methods you're using through auth
facade (like attempt
, check
, etc.), implemented within illuminate\auth\guard
class. class needs userproviderinterface
implementation injected constructor in order work. means in order use auth
facade either need use implemented databaseuserprovider
or eloquentuserprovider
, or implement own provider handles simple login want.
although article linked may lengthy, achieve need might away less code in provider might think. here's think need:
1. in app/config/auth.php
change driver simple
, append desired login credentials:
'driver' => 'simple', 'credentials' => array( 'email' => 'user@email.com', 'password' => 'yourpassword' )
2. create file in app
directory called simpleuserprovider.php
has code:
use illuminate\auth\userinterface; use illuminate\auth\genericuser; use illuminate\auth\userproviderinterface; class simpleuserprovider implements userproviderinterface { protected $user; public function __construct(array $credentials) { $this->user = new genericuser(array_merge($credentials, array('id' => null))); } // if need login via credentials following 3 methods // don't need implemented, need defined public function retrievebyid($identifier) { } public function retrievebytoken($identifier, $token) { } public function updateremembertoken(userinterface $user, $token) { } public function retrievebycredentials(array $credentials) { return $this->user; } public function validatecredentials(userinterface $user, array $credentials) { return $credentials['email'] == $user->email && $credentials['password'] == $user->password; } }
3. lastly you'll need register new provider authentication system. can append app/start/global.php
file:
auth::extend('simple', function($app) { return new simpleuserprovider($app['config']['auth.credentials']); });
this should give simple (no database) user authentication while still being able use laravel's facades.
Comments
Post a Comment