I need to log in users to web app in Javascript and PHP. So I have Javascript interface which send me accessToken to PHP which stores users data to DB and log user in. But the result code does not look like OOP but like a peace of garbage. May be I dont know how to write that code, so the question is if this peace of PHP code is ok or if it is possible to rewrite it to real OOP. Cause OOP exists because of people who use the code. It should make sense for humans. It seems this make sense only for authors of the code.
$code = $_POST['code'];
try
{
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/../model/JsonData/GoogleCredentials.json');
$client->setRedirectUri( self::REDIRECT_URI );
$accessToken = $client->fetchAccessTokenWithAuthCode( $code );
$client->setAccessToken( $accessToken );
$peopleService = new \Google_Service_PeopleService( $client );
$me = $peopleService->people->get('people/me', ['personFields' => 'emailAddresses,names,metadata,coverPhotos']); // This is extra feature which does not accept spaces....
}
catch ( \Exception $e )
{
Debugger::log( $e );
$this->sendJson( array( 'errCode' => 1, 'error' => 'Log in fails.' ) );
}
/** @var \Google_Service_PeopleService_EmailAddress $email */
foreach ( $me->getEmailAddresses() as $email )
{
if ( $email->getMetadata()->getPrimary() ) $user_email = $email->value; // Oh really? I want to log in user....
}
/** @var \Google_Service_PeopleService_Name $name */
foreach ( $me->getNames() as $name )
{
if ( $email->getMetadata()->getPrimary() ) $user_name = $name->displayName;
}
if ( ! isset( $user_email ) ) $this->sendJson( array( 'errCode' => 0, 'error' => 'Email is required.' ) );
if ( ! isset( $user_name ) ) $this->sendJson( array( 'errCode' => 0, 'error' => 'Name is required' ) );
// Hello ID is missing. How to uniquely identify user?
May be I am wrong but is it possible to write it better way to get relevant data for log in? Where is ID which can uniquely identify user?
Look at this JS interface. This looks like OOP https://developers.google.com/identity/sign-in/web/people