Skip to content

Instantly share code, notes, and snippets.

@mikekoro
Last active December 24, 2015 05:29
Show Gist options
  • Save mikekoro/6750439 to your computer and use it in GitHub Desktop.
Save mikekoro/6750439 to your computer and use it in GitHub Desktop.
Laravel 4: create login and registration functions
<?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