Cakephp One login function / multiple login views - cakephp

I have one website with a login system. However, I would like the login view to be different depending on what link has the user used to get to the login screen.
Something like:
function login ($from_page = null) {
if (isset($page)) $this->render('login_alternate_view');
else $this->render('login'); //default login view
}
And then each of the login views (login.ctp, login_alternate_view.ctp) would have the login form plus other stuff specific to each one.
Is this possible in some way? I've already tried something like the example above but it doesn't work...

So I fixed it using GET variables:
/users/login?some_var=some_value
And then in the login function I catch that variable's value with:
$this->params['url']['some_var'];
This way I can "customize" my login function depending on the link the user uses

First show the real error message you're talking in the comments about and not "something".
I guess that you want the current page url the user is on when he logs in? How to you generate the modal? Request the whole form via ajax or is it embedded in the page you're on? If it's embedded I would put the current page url the user is on in a hidden field "from" in the login form and check that.

Related

Change the session remote user in AngularJS file and redirect to main page in ColdFusion file

I have the main.cfc file where I am setting the view based on the user that is logged in. I have admin view and I have user view.
Admin view is decided based on the ids i add to the list.
I want to give option for admin to change to see view like a specific user.
I have ng-click="switchView"
This calls a function in the AngularJS
$scope.switchView = function(){
// I want to cfset session.remoteuser = userid
// and redirect to main page to change the view
}
Is this possible? How can i achieve it?

Redirect AngularJS up one directory

Im using Angular.js and I have several routes:
http://localhost/tool/new
http://localhost/tool/report/#/59FD59D56F20DDFA722F5B31796CAE3396C
tool/new is the form where a user would create a new report using the tool. The program then runs in the background and the user can access the report by going to tool/report/#/their-id where their-id is a big SHA1 hash.
That hash is a route param, if that hash is invalid I need to redirect the user back to tool/new. The problem Im having is that the program file structure is:
tool/new/index.html // The create new report form
tool/report/index.html // The report area
I've been trying to use $location to redirect like this:
$location.path('/new');
But it just redirects to tool/report/#/new and that doesn't help.
Does anyone know how to tell Angular to move up one directory and redirect?
Based on the answer from Branimir, I get the URL and trim it at the current (report) directory. I do this because the tool name and domain can change depending on the server its located on. This worked for me.
var dialog = $dialogs.notify('We were unable to find your tool run.','<p>It is possible that the URL you have entered is incorrect or that your tool run has been deleted due to inactivity.</p><p>Please confirm your URL and try again; or create a new tool run.</p>');
dialog.result.then(function(btn){
var url = $location.absUrl();
var base = url.substring(0,url.indexOf('/report/'));
window.location = base + '/#/new';
},function(btn){});
Note Im using $dialogs from here https://github.com/m-e-conroy/angular-dialog-service/blob/master/dialogs.min.js to tell the user the id is bad.
If you want to reload page use:
$window.location.href = 'http://localhost/tool/new';
$location service doesn't reload a page, it changes a URL in the same AngularJS application.

Backbone.js: How to utilize router.navigate to manipulate browser history?

I am writing something like a registration process containing several steps, and I want to make it a single-page like system so after some studying Backbone.js is my choice.
Every time the user completes the current step they will click on a NEXT button I create and I use the router.navigate method to update the url, as well as loading the content of the next page and doing some fancy transition with javascript.
Result is, URL is updated which the page is not refreshed, giving a smooth user experience. However, when the user clicks on the back button of the browser, the URL gets updated to that of a previous step, but the content stays the same. My question is through what way I can capture such an event and currently load the content of the previous step and present that to the user? Or even better, can I rely on browser cache to load that previously loaded page?
EDIT: in particular, I'm trying something like mentioned in this article.
You should not use route.navigate but let the router decide which form to display based on the current route.
exemple :
a link in your current form of the registration process :
<a href="#form/2" ...
in the router definition :
routes:{
"form/:formNumber" : "gotoForm"
},
gotoForm:function(formNumber){
// the code to display the correct form for the current url based on formNumber
}
and then use Backbone.history.start() to bootstrap routing

CakePHP 2.1 loggedIn user in views

I am using cakephp 2.1. So I am looking for getting loggedIn user in views. How to get the logged user in views.
You can take a look here:
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
or here:
http://api20.cakephp.org/class/auth-component#method-AuthComponentuser
Try this in $this->Auth->loggedIn() in your view. If it doesn't work then you have to use
$this->Auth->user('id').
For example;
if($this->Auth->loggedIn()) {
// then do something
}
or:
if($this->Auth->user('id')) {
// then do something
}
If you want to show the current logged in user details to all views. it will be better to put the logic in the layout file rather than putting the code in all view files.
to get the current logged in user details you can use $this->Auth->user
Lets say if you want to display the current loged in user name you can use echo $this->Auth->user('user_name');
If you are using the Auth Component the user data is also stored in the Session's "Auth.User" key.
So in a view it can be accessed with the SessionHelper::read() method:
$user = $this->Session->read("Auth.User");
Auth.User contains the user record from the database.
Don't forget to include the Session Helper in the $helpers array in your controller.

CakePHP Logs Me Out Prematurely

I have a CakePHP app that seems to be terminating my session on one specific action. I have a page which, when a link is clicked, launches a Fancybox overlay of the iframe type. In that overlay, the user fills out and submits a form. The form is submitted properly, does its work (including sending an email), loads the success view and lets me close the overlay, but as soon as I try to get to any other page, I'm sent to the login screen to reauthenticate.
The value of my Security.level config setting is medium and my Session.timeout is 120, so that shouldn't be the problem. Anyone have any idea what could be creating this?
Thanks.
is it possible that your ajax calls and redirects are not going to the same place, eg www.site.com and site.com? I have had that before and also kept getting logged out.
So this wasn't fun to track down, but it was me being an idiot. Buried in the code was some early-stage code to refresh user data in the authenticated session that wasn't doing what it should have been doing. It was attempting to update the entire Auth.User object directly (e.g. $this->Session->write( 'Auth', $user )) instead of calling the login method.
Once I changed the Session::write() code to $this->Auth->login( $user ), everything lined up nicely. A nice bit of reference material on this subject at http://milesj.me/blog/read/31/Refreshing-The-Auths-Session.

Resources