I am developing a website where, after the user login via Hybridauth for the first time (Facebook or Twitter), he/she needs to fill in more information. This is done by redirecting the user is via callback URL to the 'welcome.php' page. After filling in the remaining details, the user is then inserted into the DB.
The problem is that every time the user login via Hybridauth, the callback sends him to the 'welcome' page, what is not necessary anymore since all the info needed is already stored. I tried to solve this by doing a query and returning the user email and, if that match with the one stored in the DB, the user exists and should be sent to the 'index' page.
Somehow I don't think the callback URL is the best way to do it, but I want to avoid doing a check in the welcome page to redirect the user to index.
Is there a way to flag a first-time login via Hybridauth?
PS: I'm on Vanilla PHP, not using any web framework.
use Hybridauth\Hybridauth;
$callback_url = 'http://localhost/welcome.php';
//First step is to build a configuration array to pass to `Hybridauth\Hybridauth`
$config = [
//Location where to redirect users once they authenticate with a provider
'callback' => $callback_url,
//Providers specifics
'providers' => [
'Twitter' => ['enabled' => true, 'keys' => ['key' => 'U9MCuHyvFaHMVPmNcndFnWez7', 'secret' => 'emWZLMq5HiVL7iXs5OycHqTzRfWMLtBt0ckjuP4I6WDmUMKzlk']],
'Facebook' => ['enabled' => true, 'keys' => [ 'id' => '125169101446634', 'secret' => '55c35b473621a9e7822f0bcd40cec8d7'], "scope" => ['email']]
]
];
try{
//Feed configuration array to Hybridauth
$hybridauth = new Hybridauth($config);
//Attempt to authenticate users with a provider by name
$adapter = $hybridauth->authenticate('Facebook');
//Returns a boolean of whether the user is connected
$isConnected = $adapter->isConnected();
//Retrieve the user's profile
$userProfile = $adapter->getUserProfile();
// Setup user profile to welcome screen
$userFirstName = $userProfile->firstName;
$userLastName = $userProfile->lastName;
$userEmail = $userProfile->email;
$userImg = $userProfile->photoURL;
// Query user to select callback URL
$database = connectDB();
define('USER_AUTH_VALIDATION',"SELECT users.email FROM `users` WHERE users.email = :email");
$insert_user = $database->prepare(USER_AUTH_VALIDATION);
$insert_user->execute([
'email' => $userEmail
]);
if ($insert_user->rowCount() > 0) {
$callback_url = 'http://localhost/index.php';
} else {
$callback_url = 'http://localhost/welcome.php';
}
//Disconnect the adapter
$adapter->disconnect();
}
This is an old question, but I did it with a dynamic callback variable, setting it differently on certain pages. But remember, any callback page you want to use has to be listed in the provider developer's account (at least, this is true with Google). So you have to go to your Google Developers Console and set it in the Credentials/OAuth 2.0 Client IDs for your site.
So, in the hybridauth config file, I have:
and I set the $callback_url variable on the page before calling the config file, instead of in the config file like you have there.