Commit 9083f48e authored by Taylor Otwell's avatar Taylor Otwell

Working on removing authentication boilerplate.

parent e0afdf4c
<?php namespace App\Http\Controllers\Auth; <?php namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller { class AuthController extends Controller {
/** /*
* The Guard implementation. |--------------------------------------------------------------------------
* | Registration & Login Controller
* @var Guard |--------------------------------------------------------------------------
*/ |
protected $auth; | This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
/** | a simple trait to add these behaviors. Why don't you explore it?
* Create a new authentication controller instance. |
* */
* @param Guard $auth
* @return void use AuthenticatesAndRegistersUsers;
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Show the application registration form.
*
* @return Response
*/
public function getRegister()
{
return view('auth.register');
}
/**
* Handle a registration request for the application.
*
* @param RegisterRequest $request
* @return Response
*/
public function postRegister(Requests\Auth\RegisterRequest $request)
{
$user = User::forceCreate([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
]);
$this->auth->login($user);
return redirect('/home');
}
/**
* Show the application login form.
*
* @return Response
*/
public function getLogin()
{
return view('auth.login');
}
/**
* Handle a login request to the application.
*
* @param LoginRequest $request
* @return Response
*/
public function postLogin(Requests\Auth\LoginRequest $request)
{
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember')))
{
return redirect('/home');
}
return redirect('/auth/login')
->withInput($request->only('email'))
->withErrors([
'email' => 'These credentials do not match our records.',
]);
}
/**
* Log the user out of the application.
*
* @return Response
*/
public function getLogout()
{
$this->auth->logout();
return redirect('/');
}
} }
<?php namespace App\Http\Controllers\Auth; <?php namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Contracts\Auth\PasswordBroker;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PasswordController extends Controller { class PasswordController extends Controller {
/** /*
* The Guard implementation. |--------------------------------------------------------------------------
* | Password Reset Controller
* @var Guard |--------------------------------------------------------------------------
*/ |
protected $auth; | 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
*/ use ResetsPasswords;
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');
}
} }
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
class HomeController extends Controller { class HomeController extends Controller {
/*
|--------------------------------------------------------------------------
| Home Controller
|--------------------------------------------------------------------------
|
| This controller renders your application's "dashboard" for users that
| are authenticated. Of course, you are free to change or remove the
| controller as you wish. It is just here to get your app started!
|
*/
/** /**
* Create a new controller instance. * Create a new controller instance.
* *
......
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
class WelcomeController extends Controller { class WelcomeController extends Controller {
/*
|--------------------------------------------------------------------------
| Welcome Controller
|--------------------------------------------------------------------------
|
| This controller renders the "marketing page" for the application and
| is configured to only allow guests. Like most of the other sample
| controllers, you are free to modify or remove it as you desire.
|
*/
/** /**
* Create a new controller instance. * Create a new controller instance.
* *
......
<?php namespace App\Http\Requests\Auth;
use App\Http\Requests\Request;
class EmailPasswordLinkRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
<?php namespace App\Http\Requests\Auth;
use App\Http\Requests\Request;
class LoginRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required', 'password' => 'required',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
<?php namespace App\Http\Requests\Auth;
use App\Http\Requests\Request;
class RegisterRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
<?php namespace App\Http\Requests\Auth;
use App\Http\Requests\Request;
class ResetPasswordRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'token' => 'required',
'email' => 'required',
'password' => 'required|confirmed',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
...@@ -21,7 +21,10 @@ class AppServiceProvider extends ServiceProvider { ...@@ -21,7 +21,10 @@ class AppServiceProvider extends ServiceProvider {
*/ */
public function register() public function register()
{ {
// $this->app->bind(
'Illuminate\Contracts\Auth\Registrar',
'App\Services\Registrar'
);
} }
} }
<?php namespace App\Services;
use App\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
class Registrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
public function create(array $data)
{
return User::forceCreate([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment