Last active
December 24, 2015 05:29
-
-
Save mikekoro/6750439 to your computer and use it in GitHub Desktop.
Laravel 4: create login and registration functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function create() { | |
return View::make('registration'); | |
} | |
public function store() { | |
$rules = array( | |
'email' => array('required', 'email', 'unique:users'), | |
'password' => array('required', 'min:5'), | |
); | |
$messages = array( | |
'email.email' => 'Please enter a valid E-mail address.', | |
'email.required' => 'E-mail address is required.', | |
'password.required' => 'Password is required.', | |
'password.min' => 'Your password is too short! Min 5 symbols.', | |
'email.unique' => 'This E-mail has already been taken.', | |
); | |
$validation = Validator::make(Input::all(), $rules, $messages); | |
if ($validation->fails()) { | |
return Redirect::to('registration')->withInput()->withErrors($validation); | |
} | |
$user = new User; | |
$user->email = Input::get('email'); | |
$user->password = Hash::make(Input::get('password')); | |
$user->save(); | |
return Redirect::to('login'); | |
} | |
public function login() { | |
if (Input::server("REQUEST_METHOD") == "POST") { | |
$userdata = array( | |
'email' => Input::get('email'), | |
'password' => Input::get('password') | |
); | |
if ( Auth::attempt($userdata, true) ) { | |
return Redirect::to('dashboard'); | |
} else { | |
return Redirect::to('login')->withInput(); | |
} | |
} | |
return View::make('login'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment