Skip to content

Instantly share code, notes, and snippets.

@felnne
Created November 24, 2014 17:17
Show Gist options
  • Save felnne/58110a59f1cf2e71bbba to your computer and use it in GitHub Desktop.
Save felnne/58110a59f1cf2e71bbba to your computer and use it in GitHub Desktop.
Seeded password problem [Lions]
<?php
namespace Lions\Forms;
use Laracasts\Validation\FormValidator;
class LoginForm extends FormValidator {
protected $rules = [
'username' => 'required|alpha_num',
'password' => 'required',
'remember_me' => 'boolean'
];
protected $messages = [
'alpha_num' => "':attribute' may only contain letters and numbers and should match your XXX username.",
];
}
<?php
use Lions\Forms\LoginForm;
class SessionsController extends \BaseController {
/**
* @var LoginForm
*/
private $loginForm;
/**
* @param LoginForm $loginForm
*/
function __construct(LoginForm $loginForm)
{
$this->loginForm = $loginForm;
}
/**
* Show form for creating a new session (aka login).
* GET /sessions/create
*
* @return Response
*/
public function create()
{
// This project is designed to test how easily different authentication sources (drivers) can be used in Laravel.
// To aid this we return some extra meta-data about the current driver to the view.
// You wouldn't need this in a typical application and this doesn't alter the login process in any way.
return View::make('sessions.create');
}
/**
* Store a newly created session in storage.
* POST /sessions
*
* @return Response
*/
public function store()
{
$input = Input::only('username', 'password', 'remember_me');
$this->loginForm->validate($input);
$input['remember_me'] = boolval($input['remember_me']);
if (Auth::attempt(['username' => $input['username'], 'password' => $input['password']], $input['remember_me']))
{
return Redirect::intended('/');
}
Session::flash('flash_message_danger', 'Incorrect username and/or password');
return Redirect::back()->withInput();
}
/**
* Remove the specified session from storage (aka logout).
* DELETE /sessions/{id}
*
* @return Response
*/
public function destroy()
{
Auth::logout();
Session::flash('flash_message_info', 'Log out successful, authentication session destroyed.');
return Redirect::home();
}
}
<?php
namespace Lions\Forms;
use Laracasts\Validation\FormValidator;
class UserRegistrationForm extends FormValidator {
protected $rules = [
'username' => 'required|alpha_num|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|min:6|confirmed'
];
protected $messages = [
'alpha_num' => "':attribute' may only contain letters and numbers and should match your XXX username.",
];
/**
* If no email was provided assume it is $username@example.com
*
* @param array $input
* @return array
*/
public function autofillUsername(array $input) {
if ($input['email'] === '' && array_key_exists('username', $input) && $input['username'] !== '')
{
$input['email'] = $input['username'] . '@example.co.uk';
}
return $input;
}
}
<?php
class UsersTableSeeder extends Seeder {
public function run()
{
User::create([
'username' => 'user',
'email' => 'user@example.com',
'password' => Hash::make('password')
]);
}
}
<?php
use Lions\Forms\UserRegistrationForm;
class UsersController extends \BaseController {
/**
* @var UserRegistrationForm
*/
private $userRegistrationForm;
/**
* @param UserRegistrationForm $userRegistrationForm
*/
function __construct(UserRegistrationForm $userRegistrationForm)
{
//$this->beforeFilter('auth');
$this->userRegistrationForm = $userRegistrationForm;
}
/**
* Show form for creating a new User.
* GET /users/create
*
* @return Response
*/
public function create()
{
return View::make('users.create');
}
/**
* Store a newly created User in storage.
* POST /users
*
* @return Response
*/
public function store()
{
$input = Input::only('username', 'email', 'password', 'password_confirmation');
$input = $this->userRegistrationForm->autofillUsername($input);
$this->userRegistrationForm->validate($input);
User::create($input);
return Redirect::action('UsersController@index');
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique()->nullable();
$table->string('password', 60);
$table->string('remember_token')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment