| Total Complexity | 53 |
| Total Lines | 371 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like LoginController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LoginController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class LoginController extends Controller |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var mixed |
||
| 31 | */ |
||
| 32 | public $maxAttempts; |
||
| 33 | use AuthenticatesUsers, Logout, Login { |
||
| 34 | Logout::logout insteadof AuthenticatesUsers; |
||
| 35 | Login::login insteadof AuthenticatesUsers; |
||
| 36 | } |
||
| 37 | |||
| 38 | // protected $redirectTo = '/'; |
||
| 39 | |||
| 40 | private ?User $user = null; |
||
| 41 | |||
| 42 | public function __construct() |
||
| 43 | { |
||
| 44 | $this->middleware('guest')->except('logout'); |
||
| 45 | |||
| 46 | $this->maxAttempts = config('enso.auth.maxLoginAttempts'); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function redirect($service) |
||
| 50 | { |
||
| 51 | return Socialite::driver($service)->redirect(); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function redirectToProvider($provider) |
||
| 55 | { |
||
| 56 | $validated = $this->validateProvider($provider); |
||
| 57 | if (! is_null($validated)) { |
||
| 58 | return $validated; |
||
| 59 | } |
||
| 60 | |||
| 61 | return Socialite::driver($provider)->stateless()->redirect(); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Obtain the user information from Provider. |
||
| 66 | * |
||
| 67 | * @param $provider |
||
| 68 | * @return JsonResponse |
||
| 69 | */ |
||
| 70 | public function handleProviderCallback($provider) |
||
| 71 | { |
||
| 72 | try { |
||
| 73 | $user = Socialite::driver($provider)->stateless()->user(); |
||
| 74 | } catch (Exception) { |
||
| 75 | return redirect(config('settings.clientBaseUrl').'/social-callback?token=&status=false&message=Invalid credentials provided!'); |
||
| 76 | } |
||
| 77 | |||
| 78 | $curUser = User::where('email', $user->getEmail())->first(); |
||
| 79 | |||
| 80 | if (! $curUser) { |
||
| 81 | try { |
||
| 82 | // create person |
||
| 83 | $person = new Person(); |
||
| 84 | $name = $user->getName(); |
||
| 85 | $person->name = $name; |
||
| 86 | $person->email = $user->getEmail(); |
||
| 87 | $person->save(); |
||
| 88 | // get user_group_id |
||
| 89 | $user_group = UserGroup::where('name', 'Administrators')->first(); |
||
| 90 | if ($user_group === null) { |
||
| 91 | // create user_group |
||
| 92 | $user_group = UserGroup::create(['name' => 'Administrators', 'description' => 'Administrator users group']); |
||
| 93 | } |
||
| 94 | |||
| 95 | // get role_id |
||
| 96 | $role = Role::where('name', 'free')->first(); |
||
| 97 | if ($role === null) { |
||
| 98 | $role = Role::create(['menu_id' => 1, 'name' => 'supervisor', 'display_name' => 'Supervisor', 'description' => 'Supervisor role.']); |
||
| 99 | } |
||
| 100 | |||
| 101 | $curUser = User::create( |
||
| 102 | [ |
||
| 103 | 'email' => $user->getEmail(), |
||
| 104 | 'person_id' => $person->id, |
||
| 105 | 'group_id' => $user_group->id, |
||
| 106 | 'role_id' => $role->id, |
||
| 107 | 'email_verified_at' => now(), |
||
| 108 | 'is_active' => 1, |
||
| 109 | ], |
||
| 110 | ); |
||
| 111 | |||
| 112 | $random = $this->unique_random('companies', 'name', 5); |
||
| 113 | $company = Company::create([ |
||
| 114 | 'name' => 'company'.$random, |
||
| 115 | 'email' => $user->getEmail(), |
||
| 116 | 'is_tenant' => 1, |
||
| 117 | 'status' => 1, |
||
| 118 | ]); |
||
| 119 | |||
| 120 | $person->companies()->attach($company->id, ['person_id' => $person->id, 'is_main' => 1, 'is_mandatary' => 1, 'company_id' => $company->id]); |
||
| 121 | |||
| 122 | // Dispatch Tenancy Jobs |
||
| 123 | CreateDBs::dispatch($company); |
||
| 124 | Migration::dispatch($company, $user->name, $user->email, $user->password); |
||
| 125 | } catch (Exception) { |
||
| 126 | return redirect(config('settings.clientBaseUrl').'/social-callback?token=&status=false&message=Something went wrong!'); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | try { |
||
| 131 | if ($this->needsToCreateSocial($curUser, $provider)) { |
||
| 132 | UserSocial::create([ |
||
| 133 | 'user_id' => $curUser->id, |
||
| 134 | 'social_id' => $user->getId(), |
||
| 135 | 'service' => $provider, |
||
| 136 | ]); |
||
| 137 | } |
||
| 138 | } catch (Exception) { |
||
| 139 | return redirect(config('settings.clientBaseUrl').'/social-callback?token=&status=false&message=Something went wrong!'); |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($this->loggableSocialUser($curUser)) { |
||
| 143 | Auth::guard('web')->login($curUser, true); |
||
| 144 | |||
| 145 | return redirect(config('settings.clientBaseUrl').'/social-callback?token='.csrf_token().'&status=success&message=success'); |
||
| 146 | } |
||
| 147 | |||
| 148 | return redirect(config('settings.clientBaseUrl').'/social-callback?token=&status=false&message=Something went wrong while we processing the login. Please try again!'); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function needsToCreateSocial(User $user, $service) |
||
| 152 | { |
||
| 153 | return ! $user->hasSocialLinked($service); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function unique_random($table, $col, $chars = 16) |
||
| 192 | } |
||
| 193 | |||
| 194 | public function providerLogin(Request $request, $provider) |
||
| 195 | { |
||
| 196 | $data = $request->all(); |
||
| 197 | |||
| 198 | return response()->json($data); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function confirmSubscription(Request $request) |
||
| 202 | { |
||
| 203 | $request->all(); |
||
| 204 | $user = $this->loggableUser($request); |
||
| 205 | |||
| 206 | return response()->json($user); |
||
| 207 | } |
||
| 208 | |||
| 209 | protected function attemptLogin(Request $request): bool |
||
| 210 | { |
||
| 211 | $this->user = $this->loggableUser($request); |
||
| 212 | |||
| 213 | if (! $this->user) { |
||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | if ($request->attributes->get('sanctum')) { |
||
| 218 | Auth::guard('web')->login($this->user, $request->input('remember')); |
||
| 219 | } |
||
| 220 | |||
| 221 | Event::dispatch($this->user, $request->ip(), $request->header('User-Agent')); |
||
| 222 | |||
| 223 | return true; |
||
| 224 | } |
||
| 225 | |||
| 226 | protected function sendLoginResponse(Request $request) |
||
| 227 | { |
||
| 228 | $this->clearLoginAttempts($request); |
||
| 229 | |||
| 230 | if ($request->attributes->get('sanctum')) { |
||
| 231 | $request->session()->regenerate(); |
||
| 232 | |||
| 233 | return [ |
||
| 234 | 'auth' => Auth::check(), |
||
| 235 | 'csrfToken' => csrf_token(), |
||
| 236 | 'role_id' => Auth::user()->role_id, |
||
| 237 | ]; |
||
| 238 | } |
||
| 239 | |||
| 240 | $token = $this->user->createToken($request->get('device_name')); |
||
| 241 | |||
| 242 | return response()->json(['token' => $token->plainTextToken]) |
||
| 243 | ->cookie('webview', true) |
||
| 244 | ->cookie('Authorization', $token->plainTextToken); |
||
| 245 | } |
||
| 246 | |||
| 247 | protected function authenticated(Request $request, $user) |
||
| 248 | { |
||
| 249 | return response()->json([ |
||
| 250 | 'auth' => Auth::check(), |
||
| 251 | 'csrfToken' => csrf_token(), |
||
| 252 | 'role_id' => $user->role_id, |
||
| 253 | ]); |
||
| 254 | } |
||
| 255 | |||
| 256 | protected function validateLogin(Request $request) |
||
| 257 | { |
||
| 258 | $attributes = [ |
||
| 259 | $this->username() => 'required|string', |
||
| 260 | 'password' => 'required|string', |
||
| 261 | ]; |
||
| 262 | |||
| 263 | if (! $request->attributes->get('sanctum')) { |
||
| 264 | $attributes['device_name'] = 'required|string'; |
||
| 265 | } |
||
| 266 | |||
| 267 | $request->validate($attributes); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param $provider |
||
| 272 | * @return JsonResponse |
||
| 273 | */ |
||
| 274 | protected function validateProvider($provider) |
||
| 275 | { |
||
| 276 | if (! in_array($provider, ['facebook', 'google', 'github'])) { |
||
| 277 | return response()->json(['error' => 'Please login using facebook or google or github'], 422); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | private function create_company($user) |
||
| 282 | { |
||
| 283 | $company_count = Company::count(); |
||
| 284 | |||
| 285 | $company = Company::create([ |
||
| 286 | 'name' => $user->email.($company_count + 1), |
||
| 287 | 'email' => $user->email, |
||
| 288 | // 'is_active' => 1, |
||
| 289 | 'is_tenant' => 1, |
||
| 290 | 'status' => 1, |
||
| 291 | ]); |
||
| 292 | |||
| 293 | $user->person->companies()->attach($company->id, ['person_id' => $user->person->id, 'is_main' => 1, 'is_mandatary' => 1, 'company_id' => $company->id]); |
||
| 294 | |||
| 295 | /** Tree::create([ |
||
| 296 | * 'name' => 'Default Tree', |
||
| 297 | * 'description' => 'Automatically created tree as only tree remaining was deleted.', |
||
| 298 | * 'user_id' => $user->id, |
||
| 299 | * 'company_id' => $company->id, |
||
| 300 | * ]);. |
||
| 301 | */ |
||
| 302 | $company_id = $company->id; |
||
| 303 | // \Log::debug('CreateDBs----------------------'.$company); |
||
| 304 | CreateDBs::dispatch($company); |
||
| 305 | // \Log::debug('Migration----------------------'.$company); |
||
| 306 | Migration::dispatch($company, $user->name, $user->email, $user->password); |
||
| 307 | } |
||
| 308 | |||
| 309 | private function loggableUser(Request $request) |
||
| 376 | } |
||
| 377 | |||
| 378 | private function loggableSocialUser($user): bool |
||
| 398 | } |
||
| 399 | } |
||
| 400 |