I have a strange problem in my app. I have a form with some data to edit, when I am trying to save the data, strange things happens.
If I put die('test'); at the end of beforeFilter(), I see "test" on my screen, but if I put it on the beginning of my action, I am redirected to the login page.
It's happening only if I pass some specyfic data
$this->data['Movie']['title'])
if I pass something else
$this->data['Movie']['genere']
everything works fine.
How can I debug it? What is happening between beforFilter and action?
If you Auth component is active then check that in your beforeFilter() function that you allowed the action for that user.
For example:
function beforeFilter() {
$this->Auth->allow( array('add', 'edit', ) ); // for example
}
Within your allow() method include the name of the desired method.
Related
I have a login page(Functional Component), When a user tries to login without entering the required fields, I have to show the error message(ex:"Email is required"). When an invalid field exists, I shouldn't make the API call.
But, without entering fields, when I click on login button, API call is done. Again clicking on login button stops the API call.
I have made this demo - https://stackblitz.com/edit/react-pd6vvk?file=login.js which explains the issue.
Steps to follow:
1.Click on login without filling any text values. You can see "API request made" statement when fields are invalid.
2.Again click on login button, "API Request stopped" statement is displayed now.
I am new to React and I don't know, the reason and the solution to fix this issue.
Can somebody please help me out?
Thank you,
Abhilash
Because, setValidFields has async behaviour :
validateLoginForm(); //<--- inside this setValidFieldsis async
console.log(validFields);
if (isFormValid()) { //<---- so,this will still have true value ( means not updated )
// inside validateLoginForm()
// this state change won,t be reflected immediately
setValidFields(validFields => ({
...validFields,
[key]: error.length == 0
}));
I have made few changes as per your code structure, as long as I know you will need useEffect also as shown in working demo.
WORKING DEMO
So a change was made to Controller::redirect and now it fires AFTER your controller action is called. So if you're wanting to redirect someone (that's not logged in for example) before logic is fired that is no longer possible. Does anyone know how to simulate the old 2.X behavior where a call to Controller::redirect would immediately stop everything and redirected?
The key is to return the $this->redirect() call, that will return a response object immediately from the controller that will make the Dispatcher understand that you want to terminante the request immediately:
return $this->redirect($url);
The redirect can be done from the controller action itself or any of the callbacks in the controller (beforeFilter, beforeRender, afterFilter ...). Returning the redirect value is also allowed from any of the component callbacks.
There are three possible ways to redirect user if he not logged in. I did it one of my app. First is using beforeFilter() of a controller. You could use like
public function beforeFilter(){
parent::beforeFilter();
if(empty($this->logged_in)){
//your redirect code or your logic
}
}
It executes at very first time when your controller action is being called.
Second you can create a class's constructor function. It executes also at very first.
public function __construct( $request = null, $response = null ) {
parent::__construct( $request = null, $response = null );
//Your redirect code if user not logged in
}
You can write your code in AppController beforeFilter(); But if you only want to prevent non logged in users to access some function, you should use Auth component allow function. Syntax is
$this->Auth->allow(array('action_name_1','action_name2'));
Paste this allow function in beforeFilter function. By using above tricks you can prevent user to access your logic if he / she not logged in.
I just had the same issue. And I ended up with good old header() function along with die()
header('Location: http://example.com/whatever');
die;
I test a controller action. I pass data to it via the POST method. After saving a data into DB I do redirect to a main page where I display a message about of the action result.
I want to test whether a text of the message equal to expected, but I get a null instead of the $this->contents value.
Yet, all data saved and messages displayed successfully via the browser.
Below is my call of the testAction
$res = $this->testAction('/ask', array_merge(array('return' => 'contents'), array('data' => $fields_data, 'method' => 'post')));
testAction() doesn't follow redirects, simple as that.
You'll have to change your tests accordingly, and maybe test for whether Controller::redirect() is being invoked, that the response has the expected headers set, that the expected flash message has been set, etc.
And the other way around test the action where you are planning to redirect to separately by defining possible flash messages before invoking the action.
I want to do some stuff in the users/login action before login user.
For example i have captcha code on each 3rd submit of the login form and i want to check if the captcha code is valid. If it is not valid i want user to fill it up correctly first, before is able to submit. And if captcha is ok i want to use Auth->login()
Unfortunately when submit the form Auth->data['User']['username'] and Auth->data['User']['password'] are filled and the login is done behind(automagically)...
How can i disable automagically user login of the Auth component and do that only from Auth->login($data)?
you can write - like I did (with similar captcha stuff) - your own AuthExt component (or name it whatever you like) which extends the Auth Component
then override the login() method so that it suits your need
One option, depending on your project, is to upgrade to CakePHP 2.0 as there the auto-magic behavior has been removed, see Identifying users and logging them in.
one hack is to set the Auth->loginAction to a non-existent action (non-existent so that no one can access that), so Auth won't auto login in login(), you can do your own logic in there (password hashing is still automatically applied).
use this :
function beforeFilter(){
}
in this function u can do all things that want before anything happens in your controller.
This is the first time I've had this happen to me. I submit the form in one action but instead of clearing $this->data when redirected it actually keeps it. This is the part of the code that fails:
if ($this->Order->save($this->data)) {
$this->redirect("/cart/step_02");
}
The redirect certanly causes a new request from the browser,
the only explanation I can think of:
It is a GET request, you re-submit your data with the redirect.
It is loaded again in CartController::step_02().
It is loaded again in CartController(or any superclasse's) beforeXxx() callbacks.
It is unlikely a cake bug, but might be loaded from the persistent class cache,
or some hack causing redirect to fake a requestAction instead.