cakephp error: invalid username and password,try again - cakephp

I stuck with this that my cakephp login is not working.I am creating login and signup on same view. Looking forward for help.here is my code.
public function login(){
(isset($this->request->data['User'])) { // Check if the login Form was submitted
if (!empty($this->request->data['User']) && $this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->request->data['User']['password'] = '';
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
The Model for this fiunction is
class User extends AppModel{
public $validate = array(
'email'=>'email',
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),);
the view is:
<div><?php echo $this->Session->flash(); ?></div>
<?php echo $this->Form->create("User"); ?>
<?php echo $this->Form->input('email' ,array('label'=>"Email :")); ?>
<?php echo $this->Form->input('password',array('label'=>"Password"));?>
<?php echo $this->Form->end('Login'); ?>
The hash password is correctly savig in database using signup, but when i enter password in login,it gives invalid username and password error.
My AppController is
class AppController extends Controller {
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' =>'email','password'=>'password')
)
)
),
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'users',
'action' => 'profileindex'
),
//'authorize' => array('Controller'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'users', 'action' => 'profileindex'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You don\'t have access here.',
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);

<?php
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public function beforeSave($options = array()) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
return true;
}
}

Related

issue Login cakephp

i'm new cakephp and i have 1 question, please help me!
I have 1 table name quan_tri_viens same users, but i don't use table users
i read and follow the instructions
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
but i can not sign in
Model: QuanTriViensController
<?php
App::uses('AppController', 'Controller');
class QuanTriViensController extends AppController {
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('add', 'logout','login');
}
public function login() {
if ($this->request->is('post'))
{
if ($this->Auth->login())
{
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function view($id = null) {
$this->QuanTriVien->id = $id;
if (!$this->QuanTriVien->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('quantrivien', $this->QuanTriVien->read(null, $id));
}
public function index() {
$this->QuanTriVien->recursive = 0;
$this->set('quantriviens', $this->paginate());
}
public function add() {
if ($this->request->is('post')) {
$this->QuanTriVien->create();
if ($this->QuanTriVien->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
}
}
}
AppController
<?php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'tins',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view','login');
}
}
login.ctp
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('QuanTriVien'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
Model: QuanTriVien.php
<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class QuanTriVien extends AppModel {
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
public function beforeSave($options = array())
{
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']
);
}
return true;
}
}
Model: AppModel.php
<?php
App::uses('Model', 'Model');
class AppModel extends Model {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
Last :Invalid username or password, try again
witd: id: 123
pass: 123 -> $2a$10$y2RsvsN5N0COAdnAEhNeW.BYNTfqk.RBISReRHb.a12qrEKTYb6Ui
Add the userModel setting to use a model other than 'User' for your login/users:
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'userModel' => 'QuanTriVien' // <-- ADD THIS
)
)
Details here.

User in added to the database but there is no way to login cakePHP2.6.0

does any one can give me a hint why me login doesn't work?
It is possible to add a user, also the Blowfish password is created and the dataset in in saved into the database, but i can not login.
MODEL
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class Benutzer extends AppModel{
public $useTable = 'Benutzer';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'benötigtes Feld'
)
),
'passwort' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'benötigtes Feld'
)
),
'rolle' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author', 'borrow', 'archive')),
'message' => 'Please enter a valid role',
'allowEmpty' => false)
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['passwort'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['passwort'] = $passwordHasher->hash($this->data[$this->alias]['passwort']);
}
return true;
}
AppContorler
public $components = array(
'DebugKit.Toolbar',
'Session',
'Paginator' => array(
'className' => 'Bancha.BanchaPaginator'),
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
the login
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
LOGIN ctp-File
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Benutzer'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username', array('lable' => 'Username', 'type' => 'text'));?>
<?php echo $this->Form->input('passwort', array('lable' => 'Passwort', 'type' => 'password'));?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
The problem was, that i haven't define the 'fields'
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'username', //Default is 'username' in the userModel
'password' => 'passwort' //Default is 'password' in the userModel
)

Login Script in 2.4.2 is not working

I am new to cakephp. I have a problom while login. With wrong name and password redirects to login home page.
UsersController.php
public function login() {
$this->layout = 'admin-login';
if ($this->request->is('post')) {
if ($this->Auth->login($this->request->data)) {
return $this->redirect($this->Auth->redirectUrl())
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'loginAction' => array('controller' => 'users', 'action' => 'login')
)
);
public function beforeFilter() {
$this->Auth->allow("login");
//$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array(
'User.is_active' => 1
)
)
);
}
public function isAuthorized($user) {
return true;
}
login.ctp
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->submit(__('Submit');
echo $this->Form->end();
When i fill the wrong username & password & click on submit button it redirect to home page, Thanks.
You are using AuthComponent::login() wrong, you are only supposed to pass data to it in case you want to manually login a user, ie without automatic authentication.
If you want to use the components authentication functionality just call $this->Auth->login()
See also: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.
usercontroller
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','logout');
}
public function login()
{
$this->layout= 'login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect('/users');
else {
$this->Session->setFlash(__('Invalid email or password, please try again'));
}
}
else{
if($this->Auth->loggedIn())
$this->redirect('index');
}
}
}
AppController
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array(
'username' => 'user_name',
'password' => 'password'
)
)
),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You don\'t have access here.',
/*
'loginAction' => array('controller' => 'users', 'action' => 'forgot_password'),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'forgot_password'),
'authError' => 'You don\'t have access here.',
*/
),
);

CakePHP 2.x Auth Custom Username (Code in place but not working!)

Sorry - Hate to ask but I've spent hour's working this out and researching but havent had any luck.
CakePHP (running the latest version) seems to refuse to use the fields setting (So that I can use the email column in the database as the username). If I set it to 'email' which is the field I wish to use from the database it simply refuses to login stating incorrect details. Cant get any output from SQL in DebugKit for some reason. Although when it's set to username as per below it works fine just using a 'temp' column in the DB. I've tried putting it in the components var but had no luck with that either. What could I be doing wrong? Debug is on, cant see any errors in the log or browser.
The model does contain an email column.
Controller/AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'allow' => array('login','logout'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'authorize' => 'Controller'
)
);
function beforeFilter() {
Security::setHash('md5');
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'username',
),
),
);
}
}
Controller/UserController.php
class UsersController extends AppController {
public $uses = array('User');
public function beforeFilter() {
parent::beforeFilter();
}
public function isAuthorized($user){
return true;
}
public function login() {
$this->layout = 'login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password, try again','flash_error');
}
}
}
public function logout() {
$this->layout = 'login';
$this->Session->setFlash('Successfully logged out!','flash_success');
$this->redirect($this->Auth->logout());
}
}
View/Users/login.ctp
<?php
$this->set('title_for_layout', 'Login');
echo $this->Session->flash();
echo $this->Session->flash('auth','flash_info');
echo $this->Form->create('User', array(
'action' => 'login'
));
echo $this->Form->input('username',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Email:'
));
echo $this->Form->input('password',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Password:'
));
echo $this->Form->submit('Login', array(
'class' => 'submit',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to change the name of the field on your form from username to email. Just setting the label to "email" is not enough.
echo $this->Form->input('email',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Email:'
Try updating the components code in your appController to add the authenticate values to the Auth array like this:
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'allow' => array('login','logout'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'authorize' => 'Controller',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);

Cakephp 2.0 $this->Auth->login() not working

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>
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.
You also could check the Security hash method and compare with the password in the database :
Security::setHash('sha1');
(sha1 or md5)
to compare passwords :
Security::hash($password,"sha1", true);
function login() {
//if already logged-in, redirect
// if($this->Session->check('email')){
// $this->redirect(array('action' => ''));
// }
// if we get the post information, try to authenticate
if ($this->request->is('post')) {
$data = $this->request->data;
print_r($data); die;
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
only else codition is true

Resources