Last active
July 1, 2019 04:42
-
-
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
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 | |
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