From f2279c02107f28b8c3cb1b8627c22a6fb2e7fc74 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 11 Oct 2014 22:46:51 -0500 Subject: [PATCH] Scaffold authentication as default example. --- app/Http/Controllers/Auth/AuthController.php | 107 ++++++++++++++++ .../Controllers/Auth/RemindersController.php | 114 ++++++++++++++++++ app/Http/Requests/Auth/LoginRequest.php | 29 +++++ app/Http/Requests/Auth/RegisterRequest.php | 30 +++++ .../2014_10_12_000000_create_users_table.php | 34 ++++++ ...100000_create_password_reminders_table.php | 33 +++++ 6 files changed, 347 insertions(+) create mode 100644 app/Http/Controllers/Auth/AuthController.php create mode 100644 app/Http/Controllers/Auth/RemindersController.php create mode 100644 app/Http/Requests/Auth/LoginRequest.php create mode 100644 app/Http/Requests/Auth/RegisterRequest.php create mode 100644 database/migrations/2014_10_12_000000_create_users_table.php create mode 100644 database/migrations/2014_10_12_100000_create_password_reminders_table.php diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php new file mode 100644 index 00000000..bcb1bbac --- /dev/null +++ b/app/Http/Controllers/Auth/AuthController.php @@ -0,0 +1,107 @@ +auth = $auth; + } + + /** + * Show the application registration form. + * + * @Get("auth/register") + * + * @return Response + */ + public function showRegistrationForm() + { + return view('auth.register'); + } + + /** + * Handle a registration request for the application. + * + * @Post("auth/register") + * + * @param RegisterRequest $request + * @return Response + */ + public function register(RegisterRequest $request) + { + // Registration form is valid, create user... + + $this->auth->login($user); + + return redirect('/'); + } + + /** + * Show the application login form. + * + * @Get("auth/login") + * + * @return Response + */ + public function showLoginForm() + { + return view('auth.login'); + } + + /** + * Handle a login request to the application. + * + * @Post("auth/login") + * + * @param LoginRequest $request + * @return Response + */ + public function login(LoginRequest $request) + { + if ($this->auth->attempt($request->only('email', 'password'))) + { + return redirect('/'); + } + + return redirect('/login')->withErrors([ + 'email' => 'The credentials you entered did not match our records. Try again?', + ]); + } + + /** + * Log the user out of the application. + * + * @Get("auth/logout") + * + * @return Response + */ + public function logout() + { + $this->auth->logout(); + + return redirect('/'); + } + +} diff --git a/app/Http/Controllers/Auth/RemindersController.php b/app/Http/Controllers/Auth/RemindersController.php new file mode 100644 index 00000000..56df4706 --- /dev/null +++ b/app/Http/Controllers/Auth/RemindersController.php @@ -0,0 +1,114 @@ +passwords = $passwords; + } + + /** + * Display the password reminder view. + * + * @Get("password/remind") + * + * @return Response + */ + public function showReminderForm() + { + return view('password.remind'); + } + + /** + * Handle a POST request to remind a user of their password. + * + * @Post("password/remind") + * + * @param Request $request + * @return Response + */ + public function sendPasswordResetEmail(Request $request) + { + switch ($response = $this->passwords->remind($request->only('email'))) + { + case PasswordBroker::INVALID_USER: + return redirect()->back()->with('error', trans($response)); + + case PasswordBroker::REMINDER_SENT: + return redirect()->back()->with('status', trans($response)); + } + } + + /** + * Display the password reset view for the given token. + * + * @Get("password/reset") + * + * @param string $token + * @return Response + */ + public function showPasswordResetForm($token = null) + { + if (is_null($token)) + { + throw new NotFoundHttpException; + } + + return view('password.reset')->with('token', $token); + } + + /** + * Handle a POST request to reset a user's password. + * + * @Post("password/reset") + * + * @param Request $request + * @return Response + */ + public function resetPassword(Request $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::INVALID_PASSWORD: + case PasswordBroker::INVALID_TOKEN: + case PasswordBroker::INVALID_USER: + return redirect()->back()->with('error', trans($response)); + + case PasswordBroker::PASSWORD_RESET: + return redirect()->to('/'); + } + } + +} diff --git a/app/Http/Requests/Auth/LoginRequest.php b/app/Http/Requests/Auth/LoginRequest.php new file mode 100644 index 00000000..23023ee5 --- /dev/null +++ b/app/Http/Requests/Auth/LoginRequest.php @@ -0,0 +1,29 @@ + '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 new file mode 100644 index 00000000..f219115d --- /dev/null +++ b/app/Http/Requests/Auth/RegisterRequest.php @@ -0,0 +1,30 @@ + 'required|email|unique:users', + 'password' => 'required|confirmed|min:8', + ]; + } + + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return true; + } + +} diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php new file mode 100644 index 00000000..aed156ed --- /dev/null +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -0,0 +1,34 @@ +increments('id'); + $table->string('email')->unique(); + $table->string('password', 60); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('users'); + } + +} diff --git a/database/migrations/2014_10_12_100000_create_password_reminders_table.php b/database/migrations/2014_10_12_100000_create_password_reminders_table.php new file mode 100644 index 00000000..dfbcf83f --- /dev/null +++ b/database/migrations/2014_10_12_100000_create_password_reminders_table.php @@ -0,0 +1,33 @@ +string('email')->index(); + $table->string('token')->index(); + $table->timestamp('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('password_reminders'); + } + +}