cakephp: signup link on register page not working - cakephp

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

Related

CakePHP: login fails following a specific set of steps

I'm trying to set up a simple login system, but I'm having a particular problem that I can't solve. I have the following pages that perform self-explanatory actions. They are bookmarked for easy access.
cake/ (home page; must be logged in)
cake/login (must be logged in)
cake/logout (must be logged in)
cake/add (must be logged in)
All seems to work except when I preform the following sequence of actions:
1. log in
2. go to cake/logout to log out (login works immediately after this step)
3. go to cake/logout again immediately
4. attempt to log in but cake/login is just re-displayed and I'm not logged in
5. attempt to log in again and it is successful
I have noticed that $this->Session->flash('auth') is FALSE after step 3 but it is not false after 4. I tried destroying the session before or after logging out with no effect. Any ideas?
My code bits are below:
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
public function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash('User created!');
$this->redirect(array('action'=>'login'));
} else {
$this->Session->setFlash('Please correct the errors');
}
}
}
public function login() {
}
public function logout() {
$this->Session->destroy(); // makes no difference
$this->redirect($this->Auth->logout()); // redirected to login() by default
}
}
class AppController extends Controller {
public $components = array('Auth', 'Session');
}
I think that you are being redirected to the logout screen after your login.
When you go to a page you don't have access to (like the logout screen), you are redirected to login.
Once you enter name and password, you are taken back to your original request.
When that original request happens to be the logout page, logout occurs and you are sent back to the login.

cakephp: cupcake forum login function bug

I'm trying to get cupcake forum plugin's login function to work.
In the users_controller.php,
since the $user variable in the login function was not populated, it was giving errors. So I modified the login function as below:
public function login() {
if (!empty($this->data)) {
$this->User->set($this->data);
$this->User->action = 'login';
//--------------code that i added--------------
$username=$this->data['User']['username'];
$password=$this->data['User']['password'];
$user=$this->User->find('all',array(
'condition'=>array(
'User.username'=>$username,
'User.password'=>$password
)));
print_r($user);
//------------------------------------------------------------------------
if ($this->User->validates()) {
if ($user == $this->Auth->user()) {
$this->User->login($user);
$this->Session->delete('Forum');
$this->redirect($this->Auth->loginRedirect);
}
else
echo('i\'m not auth user');
}
else
echo('not validated');
}
$this->Toolbar->pageTitle(__d('forum', 'Login', true));
}
print_r($user) displays all the users from User model.
By right it should be displaying only the data of the user who has logged in. How can I achieve that? I'm clueless and this is driving me insane.
$user=$this->User->find('all',array(
'condition'=>array( // here
'User.username'=>$username,
'User.password'=>$password
)));
You have a typo - it should be conditions
As the key is invalid, Cake won't recognise it and just ignores it - so returns all your users.

cakephp: login form not redirecting

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';
?>

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.

CakePHP Facebook Plugin redirection problem

I have implemented Nick's Facebook Plugin.
Have Imported the Facebook Helper and Connect Component in the app_controller. Changed the Html accordingly.
app_controller.php
<?php
class AppController extends Controller {
var $components = array('Session', 'Facebook.Connect' => array('createUser' => false), 'Auth');
function beforeFilter() {
$this->Auth->allow('*');
$this->set('fbuser',$this->Connect->user());
}
function beforeFacebookSave() {
}
function beforeFacebookLogin($user) {
//Logic to happen before a facebook login
}
function afterFacebookLogin() {
//Logic to happen after successful facebook login.
}
}
?>
in the home.ctp
<?php
if($fbuser) {
echo $this->Facebook->logout();
debug($fbuser);
} else {
echo $this->Facebook->login();
}
?>
Once i click login and allow the permissions. it keeps refreshing indefinitely :(
My App settings online
Am on Windows Machine and access the code with this base http://localhost/spider/
i also set the canvas url as follows
I think its because of the configuration on the application settings online. Nick in the Video visits localhost.localdomain/websites/facebook_example to access the code. What is the need of the ".localdomain"
I had the same problem, and I found the answer here:
http://ardentdev.com/no-facebook-connect-cookies-for-localhost-development/
While doing some Facebook Connect development, I found that the expected cookies were not being set when developing on localhost. To fix the problem, I added localhost.local to my hosts file (pointing at 127.0.0.1) and changed the settings for my Facebook application to use localhost.local as the base domain.

Resources