LoginController.php

This handles user registration and login functionality, supporting both traditional username/password and social login with Google and Facebook.

loginUser

public function loginUser(Request $request)
{
    // Retrieve username and password from the request
    $username = $request->input('username');
    $password = $request->input('password');
    // Query the UserAccount model for a user with matching username and password
    $userAccount = UserAccount::where('username', $username)
        ->where('password', $password)
        ->first();
    // Check if a user account was found
    if (isset($userAccount)) {
        // Check if the user account is verified
        if ($userAccount->verified == false) {
            // User account is not verified, return a response indicating verification failure
            $response = [
                'SUCCESS' => 2, // 2 indicates verification failure
            ];
            return $response;
        } else {
            // User account is verified, return a success response with user details
            $response = [
                'SUCCESS' => 1, // 1 indicates successful login
                'ID' => $userAccount->id,
                'USERNAME' => $userAccount->username
            ];
            return $response;
        }
    } else {
        // No user account found, return a response indicating login failure
        $response = [
            'SUCCESS' => 0, // 0 indicates login failure
        ];
        return $response;
    }
}

To explain what this loginUser method does:

  1. Request Handling:

    • The function takes a Request object as a parameter, which contains data from the client (presumably, a username and password).

  2. Retrieve Username and Password:

    • It retrieves the username and password from the request.

  3. Query Database:

    • It queries the UserAccount model to find a user with the provided username and password.

  4. Check User Account Existence:

    • It checks if a user account was found.

  5. Verification Check:

    • If a user account is found, it checks if the account is verified (verified column is false).

  6. Generate and Return Response:

    • Based on the results, it generates a response array:

      • If the account is not found, it returns a response with SUCCESS set to 0 (login failure).

      • If the account is found but not verified, it returns a response with SUCCESS set to 2 (verification failure).

      • If the account is found and verified, it returns a success response with user details, including user ID and username.

authenticateGoogleAccountForLogin

To explain what this authenticateGoogleAccountForLogin method does:

  1. Google_Client Initialization:

    • It creates a new instance of Google_Client.

  2. Set Application Information:

    • It sets the application name for identification during Google authentication.

  3. Set Scopes:

    • It defines the requested Google API scopes for authentication, including 'profile' and 'email'.

  4. Set Access Type:

    • It sets the access type to 'offline' to obtain refresh tokens.

  5. Check Server Type:

    • It checks if the application is running on a live server.

  6. Set Authentication Configuration:

    • Based on the server type, it sets the Google authentication configuration using the appropriate credentials file.

  7. Create Google Authentication URL:

    • It creates the Google authentication URL.

  8. Sanitize URL:

    • It sanitizes the URL to ensure it is safe.

  9. Generate and Return Response:

    • It generates a response array:

      • SUCCESS is set to 1, indicating success.

      • AUTH_URL contains the Google authentication URL.

loginGoogleUser

To explain what this loginGoogleUser method does:

  1. Google_Client Initialization:

    • It creates a new instance of Google_Client.

  2. Set Application Information:

    • It sets the application name for identification during Google authentication.

  3. Set Scopes:

    • It defines the requested Google API scopes for authentication, including 'profile' and 'email'.

  4. Set Access Type:

    • It sets the access type to 'offline' to obtain refresh tokens.

  5. Check Server Type:

    • It checks if the application is running on a live server.

  6. Set Authentication Configuration:

    • Based on the server type, it sets the Google authentication configuration using the appropriate credentials file.

  7. Exchange Authorization Code for Access Token:

    • It exchanges the authorization code (received from the frontend) for an access token.

  8. Set Access Token:

    • It sets the obtained access token.

  9. Get User Information:

    • It creates an instance of Google_Service_Oauth2 to get user information using the access token.

  10. Extract User Details:

    • It extracts relevant user details, including Google ID, username, email, and password.

  11. Check Existing User:

    • It checks if a user with the same email exists in the database.

  12. Generate and Return Response:

    • If a user with the same email exists, it returns a success response with existing user details.

    • If no user with the same email exists, it creates a new user account, user profile, and associated directories and files.

    • It then returns a success response with the newly created user details.

authenticateFacebookAccountForLogin

To explain what this authenticateFacebookAccountForLogin method does:

  1. Retrieve Frontend URL:

    • Retrieve the frontend URL from the environment.

  2. Set Redirect URI:

    • Set the redirect URI for Facebook authentication by appending "/facebook-login" to the frontend URL.

  3. Create Facebook Authentication Provider:

    • Create a new instance of the Facebook authentication provider (\League\OAuth2\Client\Provider\Facebook).

    • Provide the necessary client ID, client secret, redirect URI, and specify the Graph API version.

  4. Generate Authorization URL:

    • Get the authorization URL for Facebook login with requested scopes, including 'email'.

  5. Construct Response:

    • Create a JSON response with the success status (1 for success) and the generated Facebook authentication URL.

  6. Return Response:

    • Return the constructed JSON response containing the success status and Facebook authentication URL.

loginFacebookUser

To explain what this loginFacebookUser method does:

  1. Retrieve Frontend URL:

    • It retrieves the frontend URL from the environment.

  2. Set Redirect URI:

    • It sets the redirect URI for Facebook authentication.

  3. Create Facebook Authentication Provider:

    • It creates a new instance of the Facebook authentication provider.

  4. Get Access Token:

    • It gets the access token from Facebook using the authorization code received from the frontend.

  5. Get User Details:

    • It gets the user details from Facebook using the obtained access token.

  6. Extract User Details:

    • It extracts relevant user details, including Facebook ID, username, email, and password.

  7. Check Existing User:

    • It checks if a user with the same email exists in the database.

  8. Generate and Return Response:

    • If a user with the same email exists, it returns a success response with existing user details.

    • If no user with the same email exists, it creates a new user account, user profile, and associated directories and files.

    • It then returns a success response with the newly created user details.

Last updated