Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Last active July 1, 2019 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dkesberg/8730404 to your computer and use it in GitHub Desktop.
Save dkesberg/8730404 to your computer and use it in GitHub Desktop.
Laravel UserController with basic login function which converts old passwords on the first login. Based on http://stackoverflow.com/a/19880068
<?php
use Illuminate\Support\MessageBag;
class UserController extends BaseController {
public function loginView()
{
$errors = new MessageBag();
if ($errorsOld = Input::old('errors')) {
$errors = $errorsOld;
}
$data = array(
'errors' => $errors
);
return View::make('user.login', $data);
}
public function login()
{
$validator = Validator::make(Input::all(), array(
'username' => 'required',
'password' => 'required'
));
if ($validator->passes()) {
$credentials = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if (Auth::attempt($credentials)) {
return Redirect::route('page.home');
}
$user = User::where('username', Input::get('username'))->first();
if (isset($user)) {
if ($user->password == sha1(Input::get('password'))) { // If their password is still SHA1
$user->password = Hash::make(Input::get('password')); // Convert to new format
$user->save();
Auth::login(Input::get('username'));
}
}
}
$data['errors'] = new MessageBag(array(
'password' => array(
'Login failed.'
)
));
return Redirect::route('user.login.view')->withInput($data);
}
public function logout()
{
Auth::logout();
return Redirect::route('user.login.view');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment