From 9083f48e974539606346c22f4276198aa76565af Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 29 Nov 2014 13:55:38 -0600 Subject: [PATCH] Working on removing authentication boilerplate. --- app/Http/Controllers/Auth/AuthController.php | 106 ++------------ .../Controllers/Auth/PasswordController.php | 131 ++---------------- app/Http/Controllers/HomeController.php | 11 ++ app/Http/Controllers/WelcomeController.php | 11 ++ .../Auth/EmailPasswordLinkRequest.php | 29 ---- app/Http/Requests/Auth/LoginRequest.php | 29 ---- app/Http/Requests/Auth/RegisterRequest.php | 31 ----- .../Requests/Auth/ResetPasswordRequest.php | 31 ----- app/Providers/AppServiceProvider.php | 5 +- app/Services/Registrar.php | 39 ++++++ 10 files changed, 89 insertions(+), 334 deletions(-) delete mode 100644 app/Http/Requests/Auth/EmailPasswordLinkRequest.php delete mode 100644 app/Http/Requests/Auth/LoginRequest.php delete mode 100644 app/Http/Requests/Auth/RegisterRequest.php delete mode 100644 app/Http/Requests/Auth/ResetPasswordRequest.php create mode 100644 app/Services/Registrar.php diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index 04454cea..6fbaab8b 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -1,103 +1,21 @@ 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('/'); - } + use AuthenticatesAndRegistersUsers; } diff --git a/app/Http/Controllers/Auth/PasswordController.php b/app/Http/Controllers/Auth/PasswordController.php index e4d74de5..4bff1854 100644 --- a/app/Http/Controllers/Auth/PasswordController.php +++ b/app/Http/Controllers/Auth/PasswordController.php @@ -1,128 +1,21 @@ 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; } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index beaa0b0b..c7ca983f 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -2,6 +2,17 @@ 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. * diff --git a/app/Http/Controllers/WelcomeController.php b/app/Http/Controllers/WelcomeController.php index 270b05dc..8a5ac6db 100644 --- a/app/Http/Controllers/WelcomeController.php +++ b/app/Http/Controllers/WelcomeController.php @@ -2,6 +2,17 @@ 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. * diff --git a/app/Http/Requests/Auth/EmailPasswordLinkRequest.php b/app/Http/Requests/Auth/EmailPasswordLinkRequest.php deleted file mode 100644 index 30fcb177..00000000 --- a/app/Http/Requests/Auth/EmailPasswordLinkRequest.php +++ /dev/null @@ -1,29 +0,0 @@ - 'required', - ]; - } - - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } - -} diff --git a/app/Http/Requests/Auth/LoginRequest.php b/app/Http/Requests/Auth/LoginRequest.php deleted file mode 100644 index cbe42d3e..00000000 --- a/app/Http/Requests/Auth/LoginRequest.php +++ /dev/null @@ -1,29 +0,0 @@ - 'required', 'password' => 'required', - ]; - } - - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } - -} diff --git a/app/Http/Requests/Auth/RegisterRequest.php b/app/Http/Requests/Auth/RegisterRequest.php deleted file mode 100644 index a1215732..00000000 --- a/app/Http/Requests/Auth/RegisterRequest.php +++ /dev/null @@ -1,31 +0,0 @@ - '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; - } - -} diff --git a/app/Http/Requests/Auth/ResetPasswordRequest.php b/app/Http/Requests/Auth/ResetPasswordRequest.php deleted file mode 100644 index e7823e5d..00000000 --- a/app/Http/Requests/Auth/ResetPasswordRequest.php +++ /dev/null @@ -1,31 +0,0 @@ - 'required', - 'email' => 'required', - 'password' => 'required|confirmed', - ]; - } - - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } - -} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 5790de5a..e66d8ea9 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -21,7 +21,10 @@ class AppServiceProvider extends ServiceProvider { */ public function register() { - // + $this->app->bind( + 'Illuminate\Contracts\Auth\Registrar', + 'App\Services\Registrar' + ); } } diff --git a/app/Services/Registrar.php b/app/Services/Registrar.php new file mode 100644 index 00000000..9f62ed55 --- /dev/null +++ b/app/Services/Registrar.php @@ -0,0 +1,39 @@ + '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']), + ]); + } + +}