Skip to content

Instantly share code, notes, and snippets.

@tamr
Forked from coolhome/AuthController.php
Created May 17, 2013 08:09
Show Gist options
  • Save tamr/7e121a335c6303428b25 to your computer and use it in GitHub Desktop.
Save tamr/7e121a335c6303428b25 to your computer and use it in GitHub Desktop.
<?php
/**
* AuthController
*
* Handles
* - Login
* - Logout
* - Logout
*/
class AuthController extends BaseController
{
public $layout = "layouts.custom";
/**
* GET login
*
* If logged in redirect to dashboard.
* Render login form.
*/
public function getLogin()
{
if (Auth::check())
{
return Redirect::to('dashboard');
}
$this->layout->content = View::make('auth.login');
}
/**
* POST login
*
* Validate form -> attempt to login -> redirect to correct location.
*/
public function postLogin()
{
$credentials = \Forms\Auth::loginInput();
$validator = \Forms\Auth::login();
if ($validator->fails() or ! Auth::attempt($credentials))
{
return Redirect::to('auth/login')
->withErrors("Invalid username and password combination.")
->withInput(Input::only('email'));
}
return Redirect::intended('dashboard');
}
/**
* GET logout
*
* Logout user -> redirect to login
*/
public function getLogout()
{
Auth::logout();
return Redirect::to('auth/login');
}
/**
* GET register
*
* Render register page.
*/
public function getRegister()
{
$this->layout->content = View::make('auth.register');
}
/**
* POST register
*
* Validate form -> Create new User.
*/
public function postRegister()
{
$input = \Forms\Auth::registerInput();
$validator = \Forms\Auth::register();
if ($validator->fails())
{
return Redirect::to('auth/register')
->withErrors($validator)
->withInput(Input::except(array('password', 'passwordConfirm')));
}
//Push account
unset($input['passwordConfirm']);
unset($input['mailingCityState']);
$user = User::create($input);
return Redirect::to('auth/login');
}
}
?>
<?php
namespace Forms;
class Auth extends \Forms
{
public static $login = array(
'email' => array('required', 'min:4'),
'password' => array('required')
);
public static $register = array(
'email' => array('required', 'unique:users', 'email'),
'password' => array('required', 'between:6,32'),
'passwordConfirm' => array('required', 'same:password'),
'securityQuestion' => array('required', 'exists:security_questions,id'),
'securityAnswer' => array('required', 'between:5,64'),
'firstName' => array('required', 'max:64'),
'middleName' => array('max:64'),
'lastName' => array('required', 'max:64'),
'phone' => array('required', 'size:14'), // TODO: Add phone number validator
'cellphone' => array('size:14'),
'mailingAddress' => array('required', 'max:255'),
'mailingCityState' => array('required'),
'mailingState' => array('required', 'exists:states,StateFP'),
'mailingCity' => array('required', 'exists:places,PlaceFP'),
'mailingZipcode' => array('required', 'size:10'), // TODO: Add zipcode + 4 validator + check if exists
);
}
?>
<?php
use \Illuminate\Validation;
class Forms
{
public static function __callStatic($name, $arguments)
{
$self = get_called_class();
//Check if the called function is an array. If so process it as a validation.
if (property_exists($self, $name))
{
if(count($arguments) == 0) $arguments = $self::input($self::$$name);
return $self::validate($self::$$name, $arguments, $self . '.' . $name);
}
else if (substr($name, -5) === 'Input')
{
$name = substr($name, 0, -5);
if(property_exists($self, $name)) //Get the input of these fields only
{
return $self::input($self::$$name);
}
}
else if(substr($name, -6) === 'Fields')
{
$name = substr($name, 0, -6);
if(property_exists($self, $name)) //Get the input of these fields only
{
return $self::fields($self::$$name);
}
}
}
protected static function validate($rules, $input, $lang)
{
$validator = Validator::make($input, $rules);
$validator->setCustomMessages((array)Lang::get($lang . '.custom'));
$validator->setAttributeNames((array)Lang::get($lang . '.attributes'));
return $validator;
}
protected static function input($rules)
{
return Input::only(array_keys($rules));
}
protected static function fields($rules)
{
return array_keys($rules);
}
}
?>
<?php
return array(
/***************************************************
* Login Language
**************************************************/
'login' => array(
'custom' => array(
),
'attributes' => array(
),
'options' => array(
),
),
/***************************************************
* Register Language
**************************************************/
'register' => array(
/* Custom Messages */
'custom' => array(
),
/* Attribute names */
'attributes' => array(
'email' => 'E-Mail',
'password' => 'Password',
'passwordConfirm' => 'Confirm Password',
'securityQuestion' => 'Security Question',
'securityAnswer' => 'Security Answer',
'firstName' => 'First Name',
'middleName' => 'Middle Name',
'lastName' => 'Last Name',
'phone' => 'Phone',
'cellphone' => 'Cell Phone',
'mailingAddress' => 'Mailing Address',
'mailingCityState' => 'City / State',
'mailingZipcode' => 'Zipcode',
'propertyOwner1' => 'Additional Owner 1',
'propertyOwner2' => 'Additional Owner 2',
'propertyOwner3' => 'Additional Owner 3',
'propertyAddress' => 'Property Address',
'propertyCityState' => 'City / State',
'propertyZipcode' => 'Zipcode',
),
'options' => array(
'mailingCityState' => array(
'placeholder' => 'City, State'
),
//'propertyCityState.placeholder' => 'City, State',
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment