Working on removing authentication boilerplate.
This commit is contained in:
@@ -1,128 +1,21 @@
|
||||
<?php namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\User;
|
||||
use App\Http\Requests;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Auth\PasswordBroker;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||||
|
||||
class PasswordController extends Controller {
|
||||
|
||||
/**
|
||||
* The Guard implementation.
|
||||
*
|
||||
* @var Guard
|
||||
*/
|
||||
protected $auth;
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset requests
|
||||
| and uses a simple trait to include this behavior. You're free to
|
||||
| explore this trait and override any methods you wish to tweak.
|
||||
|
|
||||
*/
|
||||
|
||||
/**
|
||||
* The password broker implementation.
|
||||
*
|
||||
* @var PasswordBroker
|
||||
*/
|
||||
protected $passwords;
|
||||
|
||||
/**
|
||||
* Create a new password controller instance.
|
||||
*
|
||||
* @param PasswordBroker $passwords
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Guard $auth, PasswordBroker $passwords)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
$this->passwords = $passwords;
|
||||
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the form to request a password reset link.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return view('auth.password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a reset link to the given user.
|
||||
*
|
||||
* @param EmailPasswordLinkRequest $request
|
||||
* @return Response
|
||||
*/
|
||||
public function postEmail(Requests\Auth\EmailPasswordLinkRequest $request)
|
||||
{
|
||||
switch ($response = $this->passwords->sendResetLink($request->only('email')))
|
||||
{
|
||||
case PasswordBroker::RESET_LINK_SENT:
|
||||
return redirect()->back()->with('status', trans($response));
|
||||
|
||||
case PasswordBroker::INVALID_USER:
|
||||
return redirect()->back()->withErrors(['email' =>trans($response)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the password reset view for the given token.
|
||||
*
|
||||
* @param string $token
|
||||
* @return Response
|
||||
*/
|
||||
public function getReset($token = null)
|
||||
{
|
||||
if (is_null($token))
|
||||
{
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
return view('auth.reset')->with('token', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the given user's password.
|
||||
*
|
||||
* @param ResetPasswordRequest $request
|
||||
* @return Response
|
||||
*/
|
||||
public function postReset(Requests\Auth\ResetPasswordRequest $request)
|
||||
{
|
||||
$credentials = $request->only(
|
||||
'email', 'password', 'password_confirmation', 'token'
|
||||
);
|
||||
|
||||
$response = $this->passwords->reset($credentials, function($user, $password)
|
||||
{
|
||||
$user->password = bcrypt($password);
|
||||
|
||||
$user->save();
|
||||
});
|
||||
|
||||
switch ($response)
|
||||
{
|
||||
case PasswordBroker::PASSWORD_RESET:
|
||||
return $this->loginAndRedirect($request->email);
|
||||
|
||||
default:
|
||||
return redirect()->back()
|
||||
->withInput($request->only('email'))
|
||||
->withErrors(['email' => trans($response)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login the user with the given e-mail address and redirect home.
|
||||
*
|
||||
* @param string $email
|
||||
* @return Response
|
||||
*/
|
||||
protected function loginAndRedirect($email)
|
||||
{
|
||||
$this->auth->login(User::where('email', $email)->firstOrFail());
|
||||
|
||||
return redirect('/home');
|
||||
}
|
||||
use ResetsPasswords;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user