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

  • Home
  • SEARCH
  • 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 9217279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:36:06+00:00 2026-06-18T02:36:06+00:00

I’m trying to use the cakephp built in Auth for a user login. I’ve

  • 0

I’m trying to use the cakephp built in Auth for a user login. I’ve managed to validate a user registration (which is located on the same view as the login) but not get the login working.

All i get when trying to login is my ‘Invalid username or password, try again’ error. I’ve gone through the blog tutorial but I’m new to cake/php and have only worked on messy projects in 1.3 that sue their own crude authentication.

MarshallsController.php

class MarshalsController extends AppController {
public $helpers     = array('Html', 'Form');
public $uses        = array("Marshal", "User");
public $components  = array("RequestHandler","Session", "Auth");

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('register', 'login');
}

public function index() {
    $this->set('users', $this->User->find('all',
            array(
                'conditions'=>array(
                    'User.marshall_id'=>$Marshall['Marshall']['id']
                )
            )));
}

//Run when Marshal attempts to register for login page
public function register(){
    if ($this->request->is('post')) {
        $this->Marshal->create();
        if ($this->Marshal->save($this->request->data)) {
            //if new marshall has been saved fetch all their data
            $marshal = $this->Marshal->find('first',
                array(
                    'conditions'=>array(
                        "Marshal.email"     => $this->data['Marshal']['email'],
                    )
                )
            );
            if(!empty($marshal)){
                //set marshal session data to track logged in users and their data
                $this->Session->write("Marshal",$marshal);
            }
            $this->Session->setFlash(__('The Marshal has been saved'));
            //redirect user to logged in page
            $this->redirect(array('controller' => 'pages', 'action' => 'home'));
        } else {
            $this->Session->setFlash(__('The Marshal could not be saved. Please, try again.'));
            echo $this->render('login');
            exit();
        }
    }
    else{
        //if Marshal has not attempted to login redirect the back to the login/register page
        echo $this->render('login');
        exit();
    }
}


public function login() {

    //if user has atempted a login
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            //If login detials are correct get user data
            $marshal = $this->Marshal->find('first',
                array(
                    'conditions'=>array(
                        "Marshal.email"     => $this->data['Marshal']['email'],
                    )
                )
            );
            if(!empty($marshal)){
                //set marshal session data to track logged in users and their data
                $this->Session->write("Marshal",$marshal);
            }
            //redirect user to the logged in page
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
            debug($this->Auth->request->data);
        }

Marshal model

class Marshal extends AppModel {

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}

public $hasMany = array(
    'User' => array(
        'className'     => 'User',
        'foreignKey'    => 'marshal_id',
        'conditions'    => array('User.status' => '1'),
    )
);

public $validate = array(
    'first_name' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A first name is required'
        )
    ),
    'last_name' => array(
        'required' => array(
            'rule' => array('notempty'),
            'message' => 'A last name is required'
        )
    ),
    'password' => array(
        'required' => array(
            'rule'    => array('minLength', '8'),
            'message' => 'Minimum 8 characters long'
        )
    ),
    'email' => 'email'
);

}

login.ctp

<div class="row">
<?php echo $this->Session->flash('auth'); ?>
<div class="sixcol">
    <?php
    echo $this->Form->create('Marshal', array('action' => 'login'));
    echo $this->Form->inputs(array(
        'legend' => __('Login'),
        'email',
        'password'
    ));
    echo $this->Form->end('Login');
    ?>
</div>
<div class="sixcol last">
    <?php
    echo $this->Form->create('Marshal', array('action' => 'register'));
    echo $this->Form->inputs(array(
        'legend' => __('register'),
        'first_name',
        'last_name',
        'email',
        'password'
    ));
    echo $this->Form->end('Register');
    ?>
</div>

  • 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-18T02:36:07+00:00Added an answer on June 18, 2026 at 2:36 am

    By default, CakePHP uses username and password fields but you have email instead of username. You need to specify it:

    public $components = array(
                'Auth' => array('authenticate' => array('Form' => array( 'userModel' => 'User',
                                        'fields' => array(
                                                            'username' => 'email',
                                                            'password' => 'password'
                                                            )
                                                    )
                                ),
                        'authorize' => array('Controller'),
                        'loginAction' => array('controller' => 'users', 'action' => 'login'),
                        'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
                        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
                        'authError' => 'You don\'t have access here.',
                ),
            );
    

    This is my working example, feel free to change it for your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.