Google Login link to user (v2024)

I am using PHPMaker 2024 and my first time to create goole login

Now successfully intigrated google login. When I open my website It has a option for Google login. and after clicking to it, it redirect to https://mydomain/index and got an error as
401

Unauthorized

You do not have permission to access /index.

  1. How to map this google user login with my PHPMaker user table.
  2. And when a new user is login from google how can I record the data to my user table

You may use User_CustomValidate server event to change the google user name to the user name in your database’s user table.

Thank.
The login work when changing the username.

some more query

  1. inside User_CustomValidate(&$usr, &$pwd)
    the IsAuthenticated() always return false. But in the help file of PHPMaker it is written as -
    external provider (Google, Facebook, Microsoft, or SAML), the user is pre-authenticated when this server event is fired. You can use IsAuthenticated() to verify.

  2. How can I get all the data pass from google. e.g. firstname, lastname etc.

  3. When a new user is login using google, how can i add the userdata to the user table

Because the user is not validated, yet in this server event.

However, if you put this code in User_Validated server event, then that IsAuthenticated() global function will always return true.

FlashBag()->set("success", "Message: " . json_encode(IsAuthenticated()));

My requirement is
check whether the login come from google login
If if come from google login I have to change the $usr in

function User_CustomValidate(&$usr, &$pwd){
 $user = ExecuteScalar("SELECT Username FROM Users WHERE Email = '$usr'");
if($user){
$usr = $user; return true;
}
return false;
}

so as to match with my usertable. But I want to run the above code only if the login comes from the google login only.

#How can I get the name of the google login users

You may try:

if (TwoFactorAuthenticationType() == "Google") {
    // your code goes here ...
    // ...
}

You may check the $usr variable in that event; whether it is username/email returned from Google Authenticator.

Got an error as-
/home/xxx/public_html/subdomain/mobile/src/AdvancedSecurity.php(1423): Call to undefined function PHPMaker2024\Mobile\TwoFactorAuthenticationType()

#How can I get the name of the google login users

Now I have create a code to check google login and the google user data as follows -

public function userCustomValidate(&$usr, &$pwd)
    {
        // Enter your custom code to validate user, return true if valid.
		if (isset($_GET['code']) && strpos($_SERVER['REQUEST_URI'], '/login/Google') !== false) {
			$username = ExecuteScalar("SELECT Username FROM Users WHERE Email = '$usr'");
			if($username){
				$usr = $username;
				return true;
			}
			else {
				$config = [
					'callback' => 'LOGIN URL',
					'providers' => [
						'Google' => [
							'enabled' => true,
							'keys' => [
								'id' => 'GOOGLE CLIENT ID',
								'secret' => 'GOOGLE CLIENT SECRET'
							],
							'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid',
						],
					],
				];

				try {
					$hybridauth = new \Hybridauth\Hybridauth($config);
					$adapter = $hybridauth->authenticate('Google');

					if ($adapter->isConnected()) {
						$userProfile = $adapter->getUserProfile();

						$identifier = $userProfile->identifier;
						$photo = $userProfile->photoURL;
						$displayName = $userProfile->displayName;
						$firstName = $userProfile->firstName;
						$lastName = $userProfile->lastName;
						$email = $userProfile->emailVerified ?? $userProfile->email;
						var_dump($email);
						exit();
					}

				} catch (\Exception $e) {
					if (session_status() === PHP_SESSION_NONE) {
						session_start();
					}
					session_destroy();
					header("Location: /logout.php");
					exit();
				}
			}
		}

		return false;
    }

Here in my case I can get the google user data
Now If the new user login I want to insert the data to the UserTable and allow the user to login and process asking them to enter the mobile no. and password etc.

There is no need to get the user profile yourself, you should be able to get them from the properties of CurrentUser(), you may var_dump() it to check first.

public function userCustomValidate(&$usr, &$pwd)
    {
		var_dump(CurrentUser());
		exit();
                 return false;
}

I got NULL value after login from google

Now If the new user login I want to insert the data to the UserTable and allow the user to login and process asking them to enter the mobile no. and password etc.

I have mistaken that you are using v2025.

Login and Google data scope has been working.

Here I am confuse what I have to do -
After google successful login, if the user is new or not found in the database then I want to collect mobile no. and password and continue the login process. So that the user does not disturb in the login process

You may try redirect user to your own page for collecting additional info and redirect user back to the login page after submission of your page.