cakephp: login form not redirecting - cakephp

When I click the login button on the login form, nothing happens. It is not redirecting to /merry_parents/report_card. I have specified $this->Auth->loginRedirect in beforeFilter. Does anyone know on what i'm doing wrong? thanks in advance.
Following is my merry_parents controller, login view and report_card view
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
$this->Auth->loginRedirect=array('controller'=>'merry_parents','action'=>'report_card');
$this->Auth->allow('signup','login','logout');
$this->Auth->authorize='controller';
//$this->Auth->actionPath='controllers/';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
}
merry_parents_controller.php
class MerryParentsController extends AppController{
var $name='MerryParents';
var $components=array('Acl','Auth','Session');
function report_card(){
}
function register(){
}
function login(){
}
function logout(){
$this->redirect($this->Auth->logout());
}
function signup(){
print_r($this->data);
if (!empty($this->data)){
//$this->Auth->password($this->data['MerryParent']['password2'] used to get what the hashed password2 would look like.
$this->MerryParent->set($this->data);
//validates calls invalidFields methods which in turn populates validationErrors arrays. Validates is used to validate a record prior to updating. Needed only when you want to update certain fields in an existing record.
if ($this->MerryParent->validates(array('fieldList'=>array('username','email','password','password2')))){
if ($this->data['MerryParent']['password']==$this->Auth->password($this->data['MerryParent']['password2'])){
$this->MerryParent->id=$this->MerryParent->field('id',
array('MerryParent.username'=>$this->data['MerryParent']['username'],
'MerryParent.email'=>$this->data['MerryParent']['email'])
);
echo $this->MerryParent->id;
//die(debug($this->MerryParent->validationErrors));
if ($this->MerryParent->save($this->data,false))//record with $this->MerryParent->id is updated
{
$this->Auth->login($this->data); //automatically logs a user in after registration
$this->redirect('/merry_parents/report_card');
}
else
echo $this->Session->setFlash(__('Your admission could not be saved, please try again!',true));
}//end if ($this->data['MerryParent']['password']....
else
echo $this->Session->setFlash('Typed passwords did not match');
}//if ($this->MerryParent->validates
}//end if (!empty($this->data))
}
}
?>
login.ctp
<?php
echo $this->Form->create('MerryParent',array('action'=>'login'));
echo $this->Form->input('username',array('label'=>'Name'));
echo $this->Form->input('password', array('value'=>''));
echo $this->Form->end('Login');
?>
report_card.ctp
<?php
echo 'HALLO';
?>

Related

passing data to controller from view in cakephp

I'm new to cakephp. I went through Andrew Perkins's video tutorials. It is a great tutorial for absolute beginners.
What I want to do is get the username to a hidden field from login page and keep it in a view.and pass it to the controller and save it to the database when it is necessary.
right now I have a add.ctp, BabiesController.php as the controller and baby.php as the model.
since I haven't completed the login page I would like to hard code the username for instance.
here are my codes. hope you guys can help me.
this is the database.emails address has been used as primary key for babyprents table.parent column int the babies table is a foreign key to the email address in the babyparents table.
Model : baby.php
<?php
class Baby extends AppModel {
var $name = 'Baby';
}
Controller : BabiesController.php
<?php
class BabiesController extends AppController {
var $name = 'Baby';
function index()
{
$this->set('babies',$this->Baby->find('all'));
}
function add()
{
if(!empty($this->data))
{
$this->Session->setFlash($this->data);
if($this->Baby->save($this->data))
{
$this->Session->setFlash('Successful');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('unsuccessful');
}
}
}
}
View: add.ctp
<h2>Sign up</h2>
<?php
echo $this->form->create('Baby',array('action'=>'add'));
echo $this->form->input('firstName');
echo $this->form->input('middleName');
echo $this->form->input('lastName');
echo $this->form->input('birthday');
echo $this->form->input('age');
echo $this->form->input('doctor');
echo $this->form->end('Sign up');
?>
all the data has been saved except for email address.any guide will be appreciated. thank you all.
In your controller use saveAll($this->data):
function add()
{
if(!empty($this->data))
{
$this->Session->setFlash($this->data);
if($this->Baby->saveAll($this->data))
{
$this->Session->setFlash('Successful');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('unsuccessful');
}
}
}
In your add.ctp put this line
echo $this->form->input('Babyparent.email');

cakephp:$this->Auth->user() login not working on production server?

I'm having a problem with the Auth component on the production server. When I enter the username and password and click on login, nothing happens except the password text box clears itself. I'm getting 'I'm in loginFailure'. I have no idea on why it is not going into 'if ($this->Auth->user())' at all.
Earlier on my development server this Auth component was working perfectly fine. I was able to login a user and once the user logins in, the report card pdf file for his/her child will be displayed.
Can anyone point out on what mistake i'm making? Thank you.
merry_parents_controller.php
class MerryParentsController extends AppController{
var $name='MerryParents';
function beforeFilter(){
parent::beforeFilter();
$this->Auth->autoRedirect=false;
//$this->redirect($this->Auth->redirect());
}
function login(){
echo "i'm in login";
if ($this->Auth->user()){
debug($this->Auth->user());
$this->data=$this->Auth->user();
if (!empty($this->data)){
$student_info=$this->MerryParent->Student->getStudents($this->data['MerryParent']['id']);
$this->set('student_info',$student_info);
print_r($student_info);
$this->redirect(array('controller'=>'aptitudes','action'=>'viewpdf',$student_info['Student']['id']));
//$this->render('/aptitudes/viewpdf');
}
else{
echo 'Auth user has not been set';
}
}
else
echo "Failure";
}
}
login.ctp
<?php
//var_dump($this->data);
$this->Session->flash('auth');
echo $this->Form->create('MerryParent',array('action'=>'login'));
echo $this->Form->input('MerryParent.username',array('label'=>'Name'));
echo $this->Form->input('MerryParent.password', array('value'=>''));
echo $this->Form->end('Login');
?>
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session','Cookie','Email','Security','RequestHandler','Cookie');
var $helpers = array('Html','Form','Ajax','Javascript','Session');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
/*var_dump($this->data);
debug($this->Auth->user);*/
$this->Auth->allow('*');
$this->Auth->loginRedirect=array('controller'=>'aptitudes','action'=>'viewpdf');
$this->Auth->logoutRedirect=array('controller'=>'merry_parents','action'=>'register');
$this->Auth->authorize='controller';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
try this:
<?php
//var_dump($this->data);
$this->Session->flash('auth');
echo $this->Form->create('MerryParent',array('action'=>'login'));
echo $this->Form->input('username',array('label'=>'Name'));
echo $this->Form->input('password', array('value'=>''));
echo $this->Form->end('Login');
?>
try this login function:
function login(){
echo "i'm in login";
if (!empty($this->data)){
if($this->Auth->login($this->data)) {
debug($this->Auth->user());
$this->data=$this->Auth->user();
if (!empty($this->data)){
$student_info=$this->MerryParent->Student->getStudents($this->data['MerryParent']['id']);
$this->set('student_info',$student_info);
print_r($student_info);
$this->redirect(array('controller'=>'aptitudes','action'=>'viewpdf',$student_info['Student']['id']));
//$this->render('/aptitudes/viewpdf');
} else {
echo 'Auth user has not been set';
}
}
} else echo "Failure";
}
There were 2 problems. After I modified login function as per CodeParadox's answer, It still wasn't working till I modified core.php's Security.salt and Security.cipherSeed. I just copied the values for salt and cipherSeed from my development server's core.php and it worked! :)
thank you.
Remove extra space in controller last line or other use php file like as "config","components" and "helpers"
<?php ?><Remove space>
your Author.authorize Controller should be a capital letter case like below:
$this->Auth->authorize='Controller';

cakephp: signup link on register page not working

I'm trying to use the Auth component only for viewing the progress report of a student. For all other links, authentication is not required. For the discussion board i already have a separate forum plugin.
When the user clicks the progress report link on the navigation bar, the user is directed to /merry_parents/register. Here, new users will click on signup link and existing users will click on login link.
However, my signup link is not working. I'm not being directed to the signup page when I click on signup. What am I doing wrong? any help is much appreciated.
The following is my code:
register.ctp
<?php
echo $this->Html->link('Sign Up','/merry_parents/signup').' for new user |'.$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
?>
merry_parents_controller.php
<?php
class MerryParentsController extends AppController{
var $name='MerryParents';
var $components=array('Auth','Session');
function beforeFilter(){
//$this->Auth->authorize='actions';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'register');
//$this->Auth->loginRedirect=array('controller'=>'merry_parents','action'=>'report_card');
}
function register(){
}
function login(){
}
function logout(){
}
function signup(){
if (!empty($this->data)){
//$this->Auth->password($this->data['MerryParent']['password2'] used to get what the hashed password2 would look like.
if ($this->data['MerryParent']['password']==$this->Auth->password($this->data['MerryParent']['password2'])){
$merryparent_id=$this->MerryParent->field('id',
array('MerryParent.name'=>$this->data['MerryParent']['name'],
'MerryParent.email'=>$this->data['MerryParent']['email'])
);
echo $merryparent_id;
print_r($this->data);
if ($this->MerryParent->save($this->data))//record with $merryparent_id is updated
{
$this->Session->setFlash('You will be receiving an email shortly confirming your login and password.');
$this->Auth->login($this->data); //automatically logs a user in after registration
$this->redirect(array('controller'=>'pages','action'=>'home'));
}
else
echo $this->Session->setFlash(__('Your admission could not be saved, please try again!',true));
}//end if ($this->data['MerryParent']['password']....
else
echo $this->Session->setFlash('Typed passwords did not match');
}//end if (!empty($this->data))
}
}
?>
You have to use following code in your MerryParentsController controller.
function beforeFilter() {
$this->Auth->allow('signup');
}
This will allow your register method to get register.
For more information please read http://book.cakephp.org/view/1255/AuthComponent-Methods

cakephp: login link not taking me to login page instead it is taking me to loginRedirect page

My login link on register page is not taking me to login page. Instead it is taking me to report_card page which is the loginRedirect page.
In beforeFilter i've set autoRedirect to false coz' i'm setting cookies in login function and then i'm setting $this->redirect($this->Auth->redirect());
Can someone please help me? thanks in advance.
my code:
register.ctp
<?php
echo $this->Html->link('Sign Up','/merry_parents/signup',array()).' for new user |'.$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
?>
app_controller.php
class AppController extends Controller {
var $components=array('Auth','Session','Cookie');
function beforeFilter(){
if (isset($this->Auth)){
$this->Auth->userModel='MerryParent';
$this->Auth->loginAction=array('controller'=>'merry_parents','action'=>'login');
//var_dump($this->data);
$this->Auth->loginRedirect=array('controller'=>'merry_parents','action'=>'report_card');
$this->Auth->allow('signup','login','logout','display');
$this->Auth->authorize='controller';
}
else
$this->Session->setFlash('Auth has not been set');
}
function isAuthorized(){
return true;
}
merry_parents_controller.php
<?php
class MerryParentsController extends AppController{
var $name='MerryParents';
var $helpers=array('Html','Form');
function beforeFilter(){
$this->Auth->autoRedirect=false;
parent::beforeFilter();
}
function report_card(){
}
function register(){
}
function login(){
if ($this->Auth->user()){
if (!empty($this->data)){
$this->MerryParent->id=$this->MerryParent->field('id',array(
'MerryParent.username'=>$this->data['MerryParent']['username'],
'MerryParent.password'=>$this->data['MerryParent']['password']
)
);
echo 'id: '.$this->MerryParent->id;
$this->Cookie->write('MerryParent.id',$this->MerryParent->id,false,0);
$this->set('id',$this->Cookie->read('MerryParent.id'));
}
$this->redirect($this->Auth->redirect());
}
}
report_card.ctp
<?php
var_dump($this->data);
echo 'HALLO';
if (isset($id))
echo $id;
else
echo 'id has not been set';
?>
Actually the problem was when I clicked on login link for the first time, login link displays fine. But, the next time i click on login link again, login page doesn't display, instead report_card page (ie. the login redirect page) displays. The reason is, i didn't have a logout button anywhere on my webpage, so the user was logged on all the time. Thanks.
in register function of merry_parents_controller.php
function register(){
$this->set('id',$this->Session->read('Auth.MerryParent.id'));
}
in register.ctp
<?php
if (isset($id)){
echo $this->Html->link('Logout',
array('controller'=>'merry_parents','action'=>'logout'),array());
}
else{
echo $this->Html->link('Sign Up','/merry_parents/signup',array()).' for new user |'.
$this->Html->link('Login','/merry_parents/login',array()).' for existing user';
}
?>
now, login and logout works fine.

Validation doesn't working in cakephp2.x

I am new in cakephp. I wrote a validation ctpfile in cakephp 2.6.7 for viewing login and logout word but the validation doesn't work.
My code is:-
<?php
if (!$authUser) {
echo $this->element('logout-header');
} else {
echo $this->element('login-header');
}
?>
How can I write validation in ctp file for viewing login and logout word in my page header?
Why you wrote a validation ctp? put your validation rules in model
http://book.cakephp.org/2.0/en/models/data-validation.html
In your AppController's beforeRender() callback set the authUser view variable by retrieving the logged in user:-
public function beforeRender() {
parent::beforeRender();
$this->set('authUser', $this->Auth->user());
}
Then the view code in your question should work as expected.
I have a solution, which I regularly use when I worked in cakephp.
in AppController.php
class AppController extends Controller{
public function beforeFilter() {
parent::beforeFilter();
$userInfo = array();
if($this->Auth->user('_id')){
$userInfo['User'] = $this->Auth->user();
Configure::write($userInfo);
}
}
}
And after this in view .ctp file
<?php
$authUser = Configure::read('User');
if (!$authUser) {
echo $this->element('logout-header');
} else {
echo $this->element('login-header');
}
?>

Resources