Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| GoogleLoginController | |
0.00% |
0 / 72 |
|
0.00% |
0 / 3 |
272 | |
0.00% |
0 / 1 |
| provider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| attemptLogin | |
0.00% |
0 / 58 |
|
0.00% |
0 / 1 |
72 | |||
| getCompanySlug | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v1\UserAuth; |
| 4 | |
| 5 | use App\Events\User\LoggedIn; |
| 6 | use App\Http\Controllers\v1\Auth\SocialLoginController; |
| 7 | use App\Http\Models\Auth\User; |
| 8 | use Illuminate\Http\JsonResponse; |
| 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\Http\Response; |
| 11 | use Google\Client; |
| 12 | use Illuminate\Support\Facades\Log; |
| 13 | |
| 14 | class GoogleLoginController extends SocialLoginController |
| 15 | { |
| 16 | public function provider() |
| 17 | { |
| 18 | return 'google'; |
| 19 | } |
| 20 | |
| 21 | public function attemptLogin(Request $request): JsonResponse |
| 22 | { |
| 23 | $googleClient = new Client(['client_id' => config('services.google.client_id')]); |
| 24 | $payload = $googleClient->verifyIdToken($request->token); |
| 25 | |
| 26 | if (empty($payload)) { |
| 27 | return response()->json(['error' => trans('auth.failed'), 'message' => 'Invalid Google User'], 400); |
| 28 | } |
| 29 | |
| 30 | $googleUserId = $payload['sub']; |
| 31 | $email = strtolower($payload['email']); |
| 32 | $user = User::withTrashed()->where('google_id', $googleUserId)->orWhere('email', $email)->first(); |
| 33 | |
| 34 | if (filled($user?->deleted_at)) { |
| 35 | return response()->json([ |
| 36 | "code" => "DEACTIVATED", |
| 37 | "error" => "Account is deactivated", |
| 38 | "message" => "Account is deactivated", |
| 39 | "admin_email" => $user?->company?->pocs()?->first()?->email |
| 40 | ], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 41 | } |
| 42 | |
| 43 | $client = $this->getOAuthClient(); |
| 44 | $authRequest = Request::create(route('passport.token'), 'POST', [ |
| 45 | 'grant_type' => 'social', |
| 46 | 'client_id' => $client->id, |
| 47 | 'client_secret' => $client->secret, |
| 48 | 'provider' => $this->provider(), |
| 49 | 'access_token' => $request->token // Use ID token instead of access token and override App\Http\Resolvers\SocialUserResolver for \Coderello\SocialGrant\Grants\SocialGrant::validateUser |
| 50 | ]); |
| 51 | $response = app()->handle($authRequest); |
| 52 | $data = json_decode($response->getContent(), true); |
| 53 | |
| 54 | if (empty($data)) { |
| 55 | return response()->json(['error' => trans('auth.failed'), 'message' => 'Oauth error'], 400); |
| 56 | } |
| 57 | |
| 58 | $about = $this->handleAdminInvitation($user); |
| 59 | |
| 60 | if ($about && $about['company_poc']) { |
| 61 | $data['is_company_poc'] = $about['company_poc']; |
| 62 | } |
| 63 | |
| 64 | if ($user) { |
| 65 | $user->is_poc = $user->isPOC(); |
| 66 | } |
| 67 | |
| 68 | $data = array_merge($data, [ |
| 69 | 'session_expires_in' => intval( |
| 70 | config('auth.passport.refresh_token_expiry') |
| 71 | ), |
| 72 | 'user_details' => $user, |
| 73 | 'company' => $this->getCompanySlug($user), |
| 74 | 'provider' => $this->provider() |
| 75 | ]); |
| 76 | |
| 77 | Log::info('SocialLoginController::attemptLogin', [ |
| 78 | 'user' => $user, |
| 79 | 'email' => $email, |
| 80 | 'provider' => $this->provider(), |
| 81 | 'company' => $user?->company?->slug, |
| 82 | 'request' => $request->all(), |
| 83 | ]); |
| 84 | |
| 85 | $requireExtension = $request->get('include_extension'); |
| 86 | if (!$requireExtension) { |
| 87 | $url = config('app.url') . '/romeo/api/v1/user/auth/login/google'; |
| 88 | |
| 89 | $request = Request::create($url, 'POST', [ |
| 90 | 'token' => $request->get('token'), |
| 91 | 'include_extension' => true |
| 92 | ]); |
| 93 | |
| 94 | $response = app()->handle($request); |
| 95 | $extensionData = json_decode($response->getContent(), true); |
| 96 | |
| 97 | $data['extension'] = $extensionData['result']; |
| 98 | } |
| 99 | |
| 100 | LoggedIn::dispatch($user, ['email' => $email, 'signin_source' => $this->provider()]); |
| 101 | |
| 102 | return response()->json($data); |
| 103 | } |
| 104 | |
| 105 | private function getCompanySlug($user) |
| 106 | { |
| 107 | $company = ''; |
| 108 | |
| 109 | if (!$user) { |
| 110 | return $company; |
| 111 | } |
| 112 | |
| 113 | if (is_array($user)) { |
| 114 | if (array_key_exists('company', $user)) { |
| 115 | $company = $user['company']; |
| 116 | } |
| 117 | } else { |
| 118 | $company = $user->company; |
| 119 | } |
| 120 | |
| 121 | if ($company) { |
| 122 | if (is_array($company)) { |
| 123 | if (array_key_exists('slug', $company)) { |
| 124 | $company = $company['slug']; |
| 125 | } |
| 126 | } else { |
| 127 | $company = $company->slug; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return $company; |
| 132 | } |
| 133 | } |