From 77d88950c13580eb133c3e00329866380c07a01b Mon Sep 17 00:00:00 2001 From: Prateek Karki Date: Mon, 10 Dec 2018 15:32:33 +0545 Subject: [PATCH] add users and auth --- app/Http/Controllers/Auth/AuthController.php | 63 +++++++++++++++ app/Http/Controllers/Auth/LoginController.php | 2 +- .../Controllers/Auth/RegisterController.php | 22 +++-- .../Auth/ResetPasswordController.php | 4 +- .../Auth/VerificationController.php | 6 +- app/Http/Controllers/HomeController.php | 28 +++++++ app/Http/Controllers/UserController.php | 57 +++++++++++++ .../Controllers/UserPasswordController.php | 41 ++++++++++ app/Http/Controllers/UserTokenController.php | 38 +++++++++ resources/views/auth/login.blade.php | 73 +++++++++++++++++ .../views/auth/passwords/email.blade.php | 47 +++++++++++ .../views/auth/passwords/reset.blade.php | 65 +++++++++++++++ resources/views/auth/register.blade.php | 77 ++++++++++++++++++ resources/views/auth/verify.blade.php | 24 ++++++ resources/views/home.blade.php | 23 ++++++ resources/views/layouts/app.blade.php | 80 +++++++++++++++++++ routes/web.php | 24 ++++++ 17 files changed, 655 insertions(+), 19 deletions(-) create mode 100644 app/Http/Controllers/Auth/AuthController.php create mode 100644 app/Http/Controllers/HomeController.php create mode 100644 app/Http/Controllers/UserController.php create mode 100644 app/Http/Controllers/UserPasswordController.php create mode 100644 app/Http/Controllers/UserTokenController.php create mode 100644 resources/views/auth/login.blade.php create mode 100644 resources/views/auth/passwords/email.blade.php create mode 100644 resources/views/auth/passwords/reset.blade.php create mode 100644 resources/views/auth/register.blade.php create mode 100644 resources/views/auth/verify.blade.php create mode 100644 resources/views/home.blade.php create mode 100644 resources/views/layouts/app.blade.php diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php new file mode 100644 index 0000000..d7a5d68 --- /dev/null +++ b/app/Http/Controllers/Auth/AuthController.php @@ -0,0 +1,63 @@ +redirect(); + } + + /** + * Obtain the user information from provider. + * + * @return Response + */ + public function handleProviderCallback($provider) + { + try { + $user = Socialite::driver($provider)->stateless()->user(); + } catch (Exception $e) { + return redirect()->route('login'); + } + + $authUser = $this->findOrCreateUser($user, $provider); + + Auth::login($authUser, true); + + return redirect()->route('home')->withSuccess(__('auth.logged_in_provider', ['provider' => $provider])); + } + + /** + * Return user if exists; create and return if doesn't + * + * @param $user + * @return User + */ + private function findOrCreateUser($user, $provider) + { + $authUser = User::where('provider_id', $user->id)->first(); + + if ($authUser) { + return $authUser; + } + + return User::create([ + 'name' => $user->name ?? $user->email, + 'email' => $user->email, + 'provider' => $provider, + 'provider_id' => $user->id + ]); + } +} diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index b2ea669..191b2b6 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -25,7 +25,7 @@ class LoginController extends Controller * * @var string */ - protected $redirectTo = '/home'; + protected $redirectTo = '/'; /** * Create a new controller instance. diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 0e8d66a..157b91c 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -2,11 +2,11 @@ namespace App\Http\Controllers\Auth; -use App\User; use App\Http\Controllers\Controller; -use Illuminate\Support\Facades\Hash; -use Illuminate\Support\Facades\Validator; +use App\Models\User; use Illuminate\Foundation\Auth\RegistersUsers; +use Illuminate\Support\Facades\Hash; +use Validator; class RegisterController extends Controller { @@ -24,11 +24,11 @@ class RegisterController extends Controller use RegistersUsers; /** - * Where to redirect users after registration. + * Where to redirect users after login / registration. * * @var string */ - protected $redirectTo = '/home'; + protected $redirectTo = '/'; /** * Create a new controller instance. @@ -43,25 +43,21 @@ public function __construct() /** * Get a validator for an incoming registration request. * - * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ - 'name' => ['required', 'string', 'max:255'], - 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], - 'password' => ['required', 'string', 'min:6', 'confirmed'], + 'name' => 'required|string|max:255|alpha_dash', + 'email' => 'required|string|email|max:255|unique:users', + 'password' => 'required|string|min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. - * - * @param array $data - * @return \App\User */ - protected function create(array $data) + protected function create(array $data): User { return User::create([ 'name' => $data['name'], diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php index cf726ee..21d387f 100644 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -21,11 +21,11 @@ class ResetPasswordController extends Controller use ResetsPasswords; /** - * Where to redirect users after resetting their password. + * Where to redirect users after reset password. * * @var string */ - protected $redirectTo = '/home'; + protected $redirectTo = '/'; /** * Create a new controller instance. diff --git a/app/Http/Controllers/Auth/VerificationController.php b/app/Http/Controllers/Auth/VerificationController.php index 23a43a8..52bec54 100644 --- a/app/Http/Controllers/Auth/VerificationController.php +++ b/app/Http/Controllers/Auth/VerificationController.php @@ -2,8 +2,8 @@ namespace App\Http\Controllers\Auth; -use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\VerifiesEmails; +use Illuminate\Routing\Controller; class VerificationController extends Controller { @@ -14,7 +14,7 @@ class VerificationController extends Controller | | This controller is responsible for handling email verification for any | user that recently registered with the application. Emails may also - | be re-sent if the user didn't receive the original email message. + | be resent if the user did not receive the original email message. | */ @@ -25,7 +25,7 @@ class VerificationController extends Controller * * @var string */ - protected $redirectTo = '/home'; + protected $redirectTo = '/'; /** * Create a new controller instance. diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php new file mode 100644 index 0000000..a3af7dd --- /dev/null +++ b/app/Http/Controllers/HomeController.php @@ -0,0 +1,28 @@ +middleware('auth'); + } + + /** + * Show the application dashboard. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + return view('home'); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..621e3a9 --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,57 @@ + $user, + 'posts_count' => $user->posts()->count(), + 'comments_count' => $user->comments()->count(), + 'likes_count' => $user->likes()->count(), + 'posts' => $user->posts()->withCount('likes', 'comments')->latest()->limit(5)->get(), + 'comments' => $user->comments()->with('post.author')->latest()->limit(5)->get() + ]); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(): View + { + $user = auth()->user(); + + $this->authorize('update', $user); + + return view('users.edit', [ + 'user' => $user, + 'roles' => Role::all() + ]); + } + + /** + * Update the specified resource in storage. + */ + public function update(UsersRequest $request): RedirectResponse + { + $user = auth()->user(); + + $this->authorize('update', $user); + + $user->update($request->validated()); + + return redirect()->route('users.edit')->withSuccess(__('users.updated')); + } +} diff --git a/app/Http/Controllers/UserPasswordController.php b/app/Http/Controllers/UserPasswordController.php new file mode 100644 index 0000000..b7c1cb3 --- /dev/null +++ b/app/Http/Controllers/UserPasswordController.php @@ -0,0 +1,41 @@ +user(); + + $this->authorize('update', $user); + + return view('users.password', ['user' => $user]); + } + + /** + * Update password for the specified resource in storage. + */ + public function update(UserPasswordsRequest $request): RedirectResponse + { + $user = auth()->user(); + + $this->authorize('update', $user); + + $request->merge([ + 'password' => Hash::make($request->input('password')) + ]); + + $user->update($request->only('password')); + + return redirect()->route('users.password')->withSuccess(__('users.password_updated')); + } +} diff --git a/app/Http/Controllers/UserTokenController.php b/app/Http/Controllers/UserTokenController.php new file mode 100644 index 0000000..53b47bc --- /dev/null +++ b/app/Http/Controllers/UserTokenController.php @@ -0,0 +1,38 @@ +user(); + + $this->authorize('api_token', $user); + + return view('users.token', ['user' => $user]); + } + + /** + * Generate a personnal access token for the specified resource in storage. + */ + public function update(): RedirectResponse + { + $user = auth()->user(); + + $this->authorize('api_token', $user); + + $user->update([ + 'api_token' => Token::generate() + ]); + + return redirect()->route('users.token')->withSuccess(__('tokens.updated')); + } +} diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php new file mode 100644 index 0000000..9edb920 --- /dev/null +++ b/resources/views/auth/login.blade.php @@ -0,0 +1,73 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
{{ __('Login') }}
+ +
+
+ @csrf + +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('password')) + + {{ $errors->first('password') }} + + @endif +
+
+ +
+
+
+ + + +
+
+
+ +
+
+ + + @if (Route::has('password.request')) + + {{ __('Forgot Your Password?') }} + + @endif +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/passwords/email.blade.php b/resources/views/auth/passwords/email.blade.php new file mode 100644 index 0000000..ccbee59 --- /dev/null +++ b/resources/views/auth/passwords/email.blade.php @@ -0,0 +1,47 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
{{ __('Reset Password') }}
+ +
+ @if (session('status')) + + @endif + +
+ @csrf + +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/passwords/reset.blade.php b/resources/views/auth/passwords/reset.blade.php new file mode 100644 index 0000000..bf27f4c --- /dev/null +++ b/resources/views/auth/passwords/reset.blade.php @@ -0,0 +1,65 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
{{ __('Reset Password') }}
+ +
+
+ @csrf + + + +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('password')) + + {{ $errors->first('password') }} + + @endif +
+
+ +
+ + +
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php new file mode 100644 index 0000000..ad95f2c --- /dev/null +++ b/resources/views/auth/register.blade.php @@ -0,0 +1,77 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
{{ __('Register') }}
+ +
+
+ @csrf + +
+ + +
+ + + @if ($errors->has('name')) + + {{ $errors->first('name') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('email')) + + {{ $errors->first('email') }} + + @endif +
+
+ +
+ + +
+ + + @if ($errors->has('password')) + + {{ $errors->first('password') }} + + @endif +
+
+ +
+ + +
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+@endsection diff --git a/resources/views/auth/verify.blade.php b/resources/views/auth/verify.blade.php new file mode 100644 index 0000000..c742cb4 --- /dev/null +++ b/resources/views/auth/verify.blade.php @@ -0,0 +1,24 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
{{ __('Verify Your Email Address') }}
+ +
+ @if (session('resent')) + + @endif + + {{ __('Before proceeding, please check your email for a verification link.') }} + {{ __('If you did not receive the email') }}, {{ __('click here to request another') }}. +
+
+
+
+
+@endsection diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php new file mode 100644 index 0000000..05dfca9 --- /dev/null +++ b/resources/views/home.blade.php @@ -0,0 +1,23 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
+
Dashboard
+ +
+ @if (session('status')) + + @endif + + You are logged in! +
+
+
+
+
+@endsection diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php new file mode 100644 index 0000000..ee7767c --- /dev/null +++ b/resources/views/layouts/app.blade.php @@ -0,0 +1,80 @@ + + + + + + + + + + {{ config('app.name', 'Laravel') }} + + + + + + + + + + + + +
+ + +
+ @yield('content') +
+
+ + diff --git a/routes/web.php b/routes/web.php index 810aa34..d6c3820 100644 --- a/routes/web.php +++ b/routes/web.php @@ -14,3 +14,27 @@ Route::get('/', function () { return view('welcome'); }); + +Auth::routes(['verify' => true]); + +Route::prefix('auth')->group(function () { + Route::get('{provider}', 'Auth\AuthController@redirectToProvider')->name('auth.provider'); + Route::get('{provider}/callback', 'Auth\AuthController@handleProviderCallback'); +}); + +Route::middleware(['auth', 'verified'])->group(function () { + Route::prefix('settings')->group(function () { + Route::get('account', 'UserController@edit')->name('users.edit'); + Route::match(['put', 'patch'], 'account', 'UserController@update')->name('users.update'); + + Route::get('password', 'UserPasswordController@edit')->name('users.password'); + Route::match(['put', 'patch'], 'password', 'UserPasswordController@update')->name('users.password.update'); + + Route::get('token', 'UserTokenController@edit')->name('users.token'); + Route::match(['put', 'patch'], 'token', 'UserTokenController@update')->name('users.token.update'); + }); + + Route::resource('newsletter-subscriptions', 'NewsletterSubscriptionController')->only('store'); +}); + +Route::get('/home', 'HomeController@index')->name('home');