Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:18:19+00:00 2026-06-17T12:18:19+00:00

I want the auth component to allow user to login entrying either username or

  • 0

I want the auth component to allow user to login entrying either username or email.
In my users table, both fields – userName and userEmail are unique.
At time of registration, the password is generated like:

sha1($username.$password);

The problem is that user is not able to login using email.

App Controller

 var $components = array('Auth');

 public function beforeFilter(){

if(isset($this->params['prefix']) && $this->params['prefix'] == 'webadmin') {

       $this->Auth->userModel = 'Admin';
      $this->Auth->logoutRedirect = $this->Auth->loginAction = array('prefix' => 'webadmin', 'controller' => 'login', 'action' => 'index');
        $this->Auth->loginError = 'Invalid Username/Password Combination!';
        $this->Auth->authError = 'Please login to proceed further!';
         $this->Auth->flashElement = "auth.front.message";
        $this->Auth->loginRedirect = array('prefix'=>'webadmin', 'controller'=>'dashboard', 'action'=>'index');
            }
          else{


         $this->layout="front";  

        //$this->Auth->autoRedirect = false;

        // $this->Auth->logoutRedirect = $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    //   $this->Auth->loginRedirect = array('controller'=>'blogs', 'action'=>'index');
         $this->Auth->fields = array(
            'username' => 'userName',
            'password' => 'password'
           );
          $this->Auth->userScope = array('User.status'=>1); 

         $this->Auth->loginError = "The username/email and password you entered doesn't match our records.";
         $this->Auth->authError = 'Please login to view this page!';
         $this->Auth->flashElement = "auth.front.message";
         $this->Auth->loginRedirect = array('controller'=>'profiles', 'action'=>'index');

    }

Users Controller: the login function goes like:

if(!empty($this->data))
{ 
   // Try to login with Email
    if (!empty($this->Auth->data)) {
    // save username entered in the login form
    $username = $this->Auth->data['User']['userName'];

    // find a user by e-mail
    $find_by_email = $this->User->find('first', array(
                    'conditions' => array('userEmail' => $this->Auth->data['User']['userName']),
                    'fields' => 'userName'));
        // found
        if (!empty($find_by_email))
        {

        $this->Auth->data['User']['userName'] = $find_by_email['User']['userName'];
        $this->data['User']['password']=$this->Auth->data['User']['password'];

          if (!$this->Auth->login($this->data)) {

            // login failed
            // bring back the username entered in the login form
            $this->Auth->data['User']['username'] = $username;
          } else {
          $this->Session->delete('Message.auth');
          // redirect
          if ($this->Auth->autoRedirect) {
          $this->redirect($this->Auth->redirect(), null, true);
          }
        }
       }
    }
}

Auth.php:(I have made some changes here in the way password is generated as I am using the cakephp session to auto-login to SMF forum.)

    function login($data = null) {
 $data['User.password'] = sha1(strtolower($data['User.userName']) . $_POST['data']['User']['password']);


        $this->__setDefaults();
        $this->_loggedIn = false;

        if (empty($data)) {
            $data = $this->data;
        }

        if ($user = $this->identify($data)) {

            $this->Session->write($this->sessionKey, $user);
            $this->_loggedIn = true;
        }
        return $this->_loggedIn;
    }

I took help from this link, but I am not getting username in $data[‘User.userName’] in auth.php, I getting email here, so the password goes wrong and results in login failure.

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:18:20+00:00Added an answer on June 17, 2026 at 12:18 pm

    The only way I could make it work is by modifying auth.php in cake.

    App Controller
    I added this to the code in before filter:

     $this->Auth->fields = array(
            'username' => 'userName',
        'email'=>'userEmail',
            'password' => 'password'
         );
    

    Users Controller I removed all the extra code.

    Auth.php
    I made changes to the identify function to check email after encrypting the password in //the way I wanted.

    function identify($user = null, $conditions = null) {
            if ($conditions === false) {
                $conditions = array();
            } elseif (is_array($conditions)) {
                $conditions = array_merge((array)$this->userScope, $conditions);
            } else {
                $conditions = $this->userScope;
            }
            $model =& $this->getModel();
    
    
            if (empty($user)) {
                $user = $this->user();
                if (empty($user)) {
                    return null;
                }
            } elseif (is_object($user) && is_a($user, 'Model')) {
                if (!$user->exists()) {
                    return null;
                }
                $user = $user->read();
                $user = $user[$model->alias];
            } elseif (is_array($user) && isset($user[$model->alias])) {
                $user = $user[$model->alias];
            }
    
            if (is_array($user) && (isset($user[$this->fields['username']]) || isset($user[$model->alias . '.' . $this->fields['username']]))) {
                if (isset($user[$this->fields['username']]) && !empty($user[$this->fields['username']])  && !empty($user[$this->fields['password']])) {
                    if (trim($user[$this->fields['username']]) == '=' || trim($user[$this->fields['password']]) == '=') {
                        return false;
                    }
                    $find = array(
                        $model->alias.'.'.$this->fields['username'] => $user[$this->fields['username']],
                        $model->alias.'.'.$this->fields['password'] => $user[$this->fields['password']]
                    );
                } elseif (isset($user[$model->alias . '.' . $this->fields['username']]) && !empty($user[$model->alias . '.' . $this->fields['username']])) {
                    if (trim($user[$model->alias . '.' . $this->fields['username']]) == '=' || trim($user[$model->alias . '.' . $this->fields['password']]) == '=') {
                        return false;
                    }
    
                // my code starts
                $user['User.userEmail']=$user['User.userName']; 
        //find username using email
            $find_by_email= $model->find('first', array(
                    'fields' => array('User.userName','User.realpass'), 
                    'conditions' => array($model->alias.'.'.$this->fields['email'] => $user[$model->alias . '.' . $this->fields['email']]),
                    'recursive' => 0
                )); 
    
    
    
            if(!empty($find_by_email))
            {
            $uname=strtolower($find_by_email['User']['userName']);
            $pwd=$user[$model->alias . '.' . $this->fields['password']];
            }
            else
            {
            $uname=strtolower($user[$model->alias . '.' . $this->fields['username']]);
            $pwd=$user[$model->alias . '.' . $this->fields['password']];
            }
             $thepassword = sha1($uname.$pwd); // encrypt password
    
    
                // find user where username or email equals to the username passed  
                    $find = array(
                        'OR' => array($model->alias.'.'.$this->fields['username'] => $user[$model->alias . '.' . $this->fields['username']], $model->alias.'.'.$this->fields['email'] => $user[$model->alias . '.' . $this->fields['username']]),
                        $model->alias.'.'.$this->fields['password'] => $thepassword
                    );
                } else {
                    return false;
                }
    
    
                $cond=array_merge($find, $conditions);
    
                $data = $model->find('first', array(
                    'conditions' => $cond,
                    'recursive' => 0
                ));
    
      // my code ends here
    
                if (empty($data) || empty($data[$model->alias])) {
                    return null;
                }
            } elseif (!empty($user) && is_string($user)) {
                $data = $model->find('first', array(
                    'conditions' => array_merge(array($model->escapeField() => $user), $conditions),
                ));
                if (empty($data) || empty($data[$model->alias])) {
                    return null;
                }
            }
    
            if (!empty($data)) {
                if (!empty($data[$model->alias][$this->fields['password']])) {
                    unset($data[$model->alias][$this->fields['password']]);
                }
                return $data[$model->alias];
            }
            return null;
        }
    

    Lastly, I commented the password encrypt code to return the simple password so that I can encrypt it the wat I needed in the above function.

    function password($password) {
    //return Security::hash($password, null, true);
     return $password;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Auth component for user login and registration. I want to add
I want to extend Ion Auth to only allow certain email addresses to register.
I am using the auth component but I don't want to use a username
I use the Auth component. I have an Ajax login form and I want
I am using the core Auth component. I have created a user login that
I want to convert username and password fields that I'm using in my auth
I am having trouble getting the Auth component do the redirects I want in
I have a Posts and a Users controller. I use the Auth Component and
If you want to store extra information about a user (django.contrib.auth.models.User) in Django you
I'm using CakePHP's Auth component and it's in my app_controller.php . Now I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.