Skip to content

Instantly share code, notes, and snippets.

@henriquesousa
Created October 5, 2014 17:06
Show Gist options
  • Save henriquesousa/d77168ef22bcbd97a55c to your computer and use it in GitHub Desktop.
Save henriquesousa/d77168ef22bcbd97a55c to your computer and use it in GitHub Desktop.
Autenticação de usuários com tabela diferente ( Ex: tabela funcionarios ) desculpem a simplicidade mas o importante é que deu certo :)
<?php
//app/config/auth.php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'funcionarios', //specification table to be used
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
<?php
/*
| Metodos da Classe UserController
*/
Route::any('/login',[
"as" => "login",
"uses" => "UserController@login"
]);
Route::any('/logando',[
"as" => "logon",
"uses" => "UserController@logon"
]);
Route::get('/logout', 'UserController@logout');
//--------------------------------------
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'funcionarios';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
}
<?php
use Validators\FuncionarioValidator as FuncionarioValidator;
class UserController extends Controller
{
/**
* Apply the filter to each request type post
*/
public function __construct() {
$this->beforeFilter('csrf', array('on'=>'post'));
}
/**
* Login Funcionario
*
* @return view
*/
public function login()
{
return View::make('login');
}
/**
* Instance FuncionarioValidator - implementation of BaseValidator class Dayle Ress
* array $input and validation index
*
* return validation error or user not found
**/
public function logon()
{
if (Input::server("REQUEST_METHOD") == "POST")
{
$validator = new FuncionarioValidator;
if ($validator->validate($input, 'login'))
{
// validação OK
$credentials = [
"username" => Input::get("username"),
"password" => Input::get("password")
];
if (Auth::attempt($credentials)) {
return Redirect::route('funcionarios');
} else {
// falha na autenticacao
$errors = ['login' => 'Usuário não encontrado!'];
return Redirect::to('login')->withErrors($errors)->withInput();
}
}
// falha na validação
$errors = $validator->errors();
return Redirect::to('login')->withErrors($errors)->withInput();
}
else
{
return Redirect::to('login');
}
}
/**
* Logout Funcionario
*
* return view index
**/
public function logout()
{
Auth::logout(); // log the user out of our application
return Redirect::to('index'); // redirect the user to the login screen
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment