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') +