checkUser (method)

Last updated: Fri, Apr 14, 2023 5:18 pm
Return to Knowledgebase

Purpose

Checks if a user has Google oAuth credentials and updates their information accordingly.

Location

users/classes/User.php

Parameters

# Parameter Data Type Required Description
1 $oauth_provider string Yes The name of the oAuth provider (in this case, 'Google');
2 $oauth_uid string Yes The unique ID provided by the oAuth provider
3 $fname string Yes The first name of the user
4 $lname string Yes The last name of the user
5 $email string Yes The email address of the user
6 $gender string Yes The gender of the user
7 $locale string Yes The locale of the user
8 $link string Yes Link to the user's Google+ Profile
9 $picture string Yes Link to the user's profile picture

Further Documentation:

The function first sets up a database connection and sets some variables for the session and cookie names.

The function then checks if a user already has Google oAuth credentials by querying the database for a matching $oauth_provider and $oauth_uid. If a match is found, the function updates the user's information with the latest data from Google.

If no match is found, the function checks if the user has a regular UserSpice account with the same email address. If a match is found, the function does not create a new account and simply updates the user's information with the latest data from Google.

If no match is found for either Google oAuth or UserSpice credentials, the function creates a new user account with the provided data.

The function then queries the database for the user's information and returns it as a result object. The object also includes a boolean flag indicating whether the user account was newly created.

This function should be used in conjunction with Google oAuth login functionality in a web application that uses the Userspice framework. It should not be used for other purposes, as it is tightly integrated with the Userspice user management system.

// Assume $oauth_provider, $oauth_uid, $fname, $lname, $email, $gender, $locale, $link, and $picture are provided by Google oAuth

$user = new User(); // Instantiate the User class
$googleUser = $user->checkUser($oauth_provider, $oauth_uid, $fname, $lname, $email, $gender, $locale, $link, $picture); // Check if the user has Google oAuth credentials

if ($googleUser) {
if ($googleUser->isNewAccount) {
// New user account created
echo "Welcome, new user!";
} else {
// User account updated
echo "Welcome back, ".$googleUser->fname."!";
}
} else {
// Error handling
echo "Unable to retrieve user information.";
}


In this example, we create a new User object and call the "checkUser" function with the necessary parameters provided by Google oAuth. We then check if the function returned a valid user object and whether the user account was newly created. We display a welcome message to the user depending on whether their account is new or updated. If an error occurs, we display an error message.