Jump to content

Login to WhmCs with phone number instead of Email


Nasouh

Recommended Posts

Hello,

There are a lot of modules available to verify each login with a phone number but I can't find any that replace the email with phone number for the login. I believe it would be possible to do with a custom module but it's not a feature that is available by default.

Link to comment
Share on other sites

  • 6 months later...

You can use this hook:

<?php
// THIS IS BAD PRACTICE - MULTIPLE USERS CAN HAVE THE SAME PHONE NUMBER
// THIS IS BAD PRACTICE - MULTIPLE USERS CAN HAVE THE SAME PHONE NUMBER
// THIS IS BAD PRACTICE - MULTIPLE USERS CAN HAVE THE SAME PHONE NUMBER
// THIS IS BAD PRACTICE - MULTIPLE USERS CAN HAVE THE SAME PHONE NUMBER
// THIS IS BAD PRACTICE - MULTIPLE USERS CAN HAVE THE SAME PHONE NUMBER
// THIS IS BAD PRACTICE - MULTIPLE USERS CAN HAVE THE SAME PHONE NUMBER

use WHMCS\Database\Capsule;

add_hook('ClientLoginShare', 1, function ($vars) {

    // Define username and password. Username is whatever the user enters in the 'email address' field.
    $username = $vars['username'];
    $password = $vars['password'];
    
    // Let's see if the user is logging in using an email address. If not, let's see if we can find the user's phone number in the database and allow them to login this way.
    if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
        $users = Capsule::table('tblclients')
            ->select('email')
            ->where('phonenumber', $username)
            ->get();

        // If there's multiple users with the same phone number, allow _none_ of them to login by using their phone numbers. 
        // They will only be able to login using email address in that case
        if (sizeof($users) > 1) {
            return false;
        }

        // Set the user's email address
        foreach($users as $user){
            $email = $user->email;
        }

        // Documentation: https://developers.whmcs.com/api-reference/validatelogin/
        $command = 'ValidateLogin';
        $postData = array(
            'email' => $email,
            'password2' => $password,
        );
        
        // Check if login is valid - if it is, log them in using email address
        $isValid = localAPI($command, $postData);
        if($isValid['result'] == "true") {
            return array(
                'email' => $email,
            );
        }
    }
});


add_hook('ClientAreaFooterOutput', 1, function($vars) {
    // Change login input field to allow any text
    $changeLoginInput = '<script>$( document ).ready(function() {
        $("#inputEmail").prop({type:"text"});
    });</script>';
    return $changeLoginInput;
});

 

Although it's very bad practice. Multiple users can have the same phone number. I've implemented a piece of code that will force users to login with email address if the same phone number exists for multiple accounts - but use it at your own risk.
I haven't tested the script 100% - please make sure you test it before you use it.

Link to comment
Share on other sites

Yes, Multiple users can have the same phone number.

But if they can login using only phone no. or only a Username - example for all accounts.

Username - exampleadmin

Phone - 999990000

A single user may have four companies and he / she has to remember all four usernames / email id's .  If  such users can login using only one mobile number and a single password then it would always be helpful in my opinion. 

Link to comment
Share on other sites

There's no way you can do that. No way whatsoever.

Each user is its own user. You can't log in to multiple accounts at once.

I might be misunderstanding you. How would you think it should work?

 

Edit:

With WHMCS v8., you'll be able to access multiple accounts from one user. What you're suggesting doesn't work, though.

Edited by DennisHermannsen
Link to comment
Share on other sites

  • 2 weeks later...

Hi @DennisHermannsen

Still unable to login, I also changed to default code you have provide in this post using Telephone.  What should be the problem ?

I want to use this Hook but failing drastically... Thanks

Note: I am able to login properly with the email id used for the contact but not the mobile number unfortunately... 

diniGBr.png

Edited by VirtualWorldGlobal
Link to comment
Share on other sites

No there are no same values, I have checked, also with email address and same password I am able to login...

It's only two values  in both in post code and phone number fields

1. Primary - ******3123

2. Contact - ******9000

I removed all hooks from includes  thinking that it may clash but no same problem, what can go wrong ?

juCYAFk.png

USBtNmF.png

 

 

Edited by VirtualWorldGlobal
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated