Redirect to the last page requested after login in cakephp - cakephp

HI I have search from where client can either click on keywords
which is below in search bar or enter a keyword in search window, In
case first when user click on keywords the search_results opens with
keywords in URL but when user enter keyword then it just give result
according to it.
So when user not logged in then after some search_result we ask for
login & on click login button we redirect for login popup from where
user logged in that time we have to redirect on same page so for
this I am using two concept one for predefined keywords link which
come on URL just redirect last URL value in session & after logged
in redirect to that URL.
3.I need help in keyword enter by user & then logged in so needs to redirect with same keyword page to user.
function search_result($adKeyword = null){
if(!empty($adKeyword) && $adKeyword != ''){
$this->data['City']['keyword'] = $adKeyword;
}
//////////////////Maintain fetch data////////////////////
if(!empty($this->data)){
//pr($this->data);exit;
if($this->data['City']['keyword'] == 'Name or Area of expertise'){
$this->data['City']['keyword'] = '';
$this->set("title_for_layout","Search Result");
}
if ( empty($this->data['City']['city_name'])) {
$this->data['City']['city_name'] = $this->Session->read("Location");
}
if($this->data['City']['keyword'] != '')
$this->set("title_for_layout",$this->data['City']['keyword']." | Search Result");
//$request_params = Router::getParams();
//$this->Session->write('auth_redirect','/'.$request_params['url']['url']);
$this->Session->write('login_referrer',$this->params['url']['url']);
$this->Session->write('login_referrers',$this->data['City']['keyword']);
above two session variable I am using for redirect after authentication on login
if($this->Auth->user('role_id')== Configure::read('App.Role.Mentee')) {
if ($this->Session->check('login_referrer')) {
$loginReferrer = $this->Session->read('login_referrer');
$this->Session->delete('login_referrer');
//prd($loginReferrer);
$this->redirect(SITE_URL."$loginReferrer");
}
else if($this->Session->check('login_referrers'))
{
$loginReferrers = $this->Session->read('login_referrers');
$this->Session->delete('login_referrers');
//prd($loginReferrers);
$this->redirect(array('controller'=>'fronts','action'=>'search_result/','$adKeyword' => $loginReferrers));
}
else {
$this->redirect(array('controller'=>'fronts','action'=>'index'));
}
what happening its not going to else if statement Please help me

If they are browsing some pages and asked to login then they should redirect to that page after login
That's exactly what the Auth component does by default. As such to get the desired behavior, the login function should look similar to:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
Note the use of $this->Auth->redirect() which returns the url to redirect the user to, this example is taken directly from the documentation.
To change the default Auth redirect url modify the loginRedirect property of the auth component (from your beforeFilter, for example).

For manually login I have solved my issue,I have added this code to search_result action of my front_controller
$request_params = Router::getParams();
$this->Session->write('auth_redirect','/'.$request_params['url']['url']);
And in my user _controller I have just used this session variable:
if($this->Auth->user('role_id')== Configure::read('App.Role.Mentee')) {
$this->redirect($this->Session->read('auth_redirect'));
else
$this->redirect(array('controller'=>'fronts','action'=>'index'));
But it is not solving my problem completely, I want to delete session value after one time redirect.but for this it is not destroying the router session variable value

Related

redirecting not signed in user in react and firebase

I've seen this solution:
firebase.auth().onAuthStateChanged(user => {
user ? history.push("/dashboard") : history.push("/login");
renderApp();
});
from How to redirect to Login page when users not logged in the application
the issue with this is that when the user reloads the page (they will still be signed in, but it redirects them to the dashboard page and wipes everything from the url)
How can i solve this?
You have a couple options:
1. Not redirect them if they are logged in
You could just do nothing onAuthStateChanged if the user is logged in.
That could look something like this:
firebase.auth().onAuthStateChanged(user => {
user ? null : history.push("/login");
renderApp();
});
The benefit of this is that the user may enjoy not being forced to navigate on login so this solves that. But he downside is that if there is a reason that in certain cases that you want them to be redirected, nothing will happen.
2. Use a function to conditionally redirect if they are logged in
If the user is on a page with something in the url you may not want to redirect them. Maybe they are on a page for a certain shop item (or any page) and the url is .../shop?item=7987 and you don't want to lose where the user was. But there may be other pages such as a general info or login page where you do want the user to be redirected.
If this case you could call a function like this:
firebase.auth().onAuthStateChanged(user => {
user ? conditionallyNavigate() : history.push("/login");
renderApp();
});
And then inside the conditionallyNavigate() function you can decide what you want to do based on other factors. For example, you could change the behavior based on data stored in state, or you could check the route they are on or the URL parameters and determine if they should be redirected this way. For example:
const conditionallyNavigate = () => {
if ( onCertainPage() ) {
history.push("/dashboard")
}
}
This gives you the benefit of control over when they should be redirected or when you think they are on a page they would like to stay on.

Infinite loop when unauthorized user tries to access web page (authorization via adal-angular)

I have a web based dashboard that authenticates user with adal-angular. Authorized users can access the page without any issue, but when a user is unauthorized, they get into an infinite loop with login page.
This was my initial code:
AuthContext.handleWindowCallback();
if ((window === window.parent) && window === window.top && !AuthContext.isCallback(window.location.hash)) {
if (!AuthContext.getCachedToken(adalConfig.clientId) || !AuthContext.getCachedUser()) {
AuthContext.login();
}
}
else
{
AuthContext.acquireToken(adalConfig.endpoints.xyz, (message, token, msg) =>{
if (token) {
ReactDOM.render(
//rendering code
);
}
});
}
After moving 'AuthContext.handleWindowCallback();' inside the first if condition, the user doesn't get in to infinite loop anymore. But the error page is not displayed as expected. They just get a blank page, but the url has the message that user is not authenticated.
How can I get a working error page when the user is not authenticated? Is there a way to access the error message programmatically?
Try calling:
AuthContext.getLoginError()
After
AuthContext.handleWindowCallback()

How can I prompt user for more information during sign-in/registration process with Firebase?

I am using the firebaseUI toolset to allow users to login to my app.
I'm also using the firebase.auth().onAuthStateChanged method to redirect them to the main application once they are logged in.
What I would like to do is, during the sign-in process...if they are a new user, prompt them for a displayname to use (to override whatever name is given by the 3rd party provider).
Right now, once they login, they are redirected to the app..and it's not obvious how I can have a smooth workflow that would provide a modal /screen to allow them to create a display name before continuing onto the app.
Is there a recommended best practice of how to capture/prompt for additional information during the first sign-in process, which I would then store in my firebase /users collection?
Right now it will just auto redirect to the main app.
componentDidMount() {
//console.log(this.props.isLoading)
firebase.auth().onAuthStateChanged(user => {
if(user) {
this.props.setUser(user);
this.props.history.push('/');
}
callbacks : {
signInSuccessWithAuthResult: function(authResult, redirectUrl) {
var user = authResult.user;
var credential = authResult.credential;
var isNewUser = authResult.additionalUserInfo.isNewUser;
var providerId = authResult.additionalUserInfo.providerId;
var operationType = authResult.operationType;
if(isNewUser)
{
//prompt user to enter a displayName to use with the application
}
// Do something with the returned AuthResult.
// Return type determines whether we continue the redirect automatically
// or whether we leave that to developer to handle.
return true;
},
},

Symfony does not redirect to login route after session is expired

I am using Symfony 3.1 and angularjs. I would like to redirect the users to a login page when the session expires. However, when I delete a session and click on one of the ng-click button in my view, symfony actually redirects to the loginAction in LogInController. However, it does not render the login page.
public function loginAction(Request $request)
{
error_log('Here in login');
// get access to authentication utilities
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
// render login.html.twig
return $this->render(
'security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
This is the network tab after clicking the ng-click button after deleting the session.

How to redirect user after login to the same page in which he was

have a cart products page, if a person clicks on a product add to cart button they will be redirected to the login page.
After a successful login, I need to send the user back to same products page.
You can do something like this,
function module_user_login(&$edit, $account) {
$alias = drupal_get_path_alias($_GET['q']);
if (in_array('Member', $account->roles)) {
drupal_goto("http://" . $_SERVER['HTTP_HOST'] . '/' . $alias);
}
}
Note that the code above checks that the user has the Role ‘Member’.
Or make use of this module

Resources