create a login with angular and php sessions - angularjs

I'm looking for some inspiration (help), in order to do a good login system for my application based my needs, so the scenario goes like this:
I have a backend application which when logged, created a session and the only thing I can see it set on my browser is a PHPSESSID cookie.
So, I have my application done and define like this.
var app = angular.module('myApp', []);
As far as I know, I would have to do something like, check the login controller, if successful, create the line of code I copied and change the view, else, do not created.
So, I thought in creating a controller for this, however, should I create another app only for this? so my ng-app would have 2 applications?, or no matter is logged or not, instantiate the app and hide the home buttons as long as the login is not successful?
I'm kind of lost in here, I would appreciate any help that points me on the right direction.

There are many approach to achieve it. I am only talking the approach that I usually used. Let's say, in the pictures below, user able to view the blog without login. After user is login, the black navigation bar on the top will appear. How to achieve it by using AngularJS?
In the article.html
<div ny-app='myapp' ng-controller='mycontroller'>
<user-navbar ng-if="user" user-data='user'><user-navbar>
<article-content></article-content>
</div>
<script src="angularjs....."></script>
<script>
angular.module('myapp',[]).controller('mycontroller',['$scope','$http',
function($scope,$http){
$http.get('/getuserdata.php').success(function(user){
// you will get user data only when session is valid.
$scope.user = user;
});
}
]);
</script>
In getuserdata.php
<?php
if(!empty($_SESSION['user'])){
echo json_encode($_SESSION['user']);
}
echo null;
?>
The code above shows that when user is login and session is set in user key. $_SESSION['user'] will contain the information of your user and you just response it to frontend AngularJS by echo.
Then, $http will receive the user data that requested and assign to $scope.user and navigation bar will shows if $scope.user is not null. You don't have to due with PHPSESSID. PHP session will handle everything for you if you coding correctly.

Related

Get the url that a user is coming from AngularJS

If an user is coming from an specific page i need to do get some values out of a cookie and change what the users sees.
Now, the issue is that i cannot find a way to view what page the user is coming from.
EDIT: This is intended to capture when the users clicks back in a page and save the state of the previous page.
Any ideas?
Thanks in advance
Solved. Every time i load a page i'm saving the url, so when i get to this page i just have to read it to tell. Thanks!
You can use browser history in our javascript or you can write your last page in cookies and get the last link then update it
Using cookies will indeed fix this for you. So when a user goes to a new page - set a cookie like:
app.controller('myController',['$scope', '$location', $cookies], function($scope, $location, $cookies){
if($cookies.get('page') == '/index'){
//do stuff if user came from index
}
$scope.pageChanged = function(value){
$cookies.put('page', value);
$location.path('/index');
}
}
just make sure you use the pageChanged function to set your page every time user changes pages.
Using the $routeProvider you can use the resolve function to detect when a new route has been loaded.
Another way would be to listen for the event $routeChangeSuccessor $routeChangeError and get the information needed from the service $location or $route.
If you want a sample just ask me, I'll try to post one as soon as I have free time.

Load view in background based on data from initial login view

This is not my actual use case, but I am trying to understand the concept of how to load a view based on the information another view has using appgyver supersonic framework.
I have 3 pages, Login.html, MainPage.html, and Load.html. Each of them have their own controller. When the app opens it first goes to the login page. The login page sends the username and password to my server which then returns success or not. When there is success, the app goes to the main page. I would like for the load.html to understand a login has happened and start loading the view with the users appropriate content. This is so when the user goes from the main page to the load page all the information is already loaded. I am trying to understand the concept of how to accomplish this with supersonic.
You can use publish / subscribe methods between controllers.
Check out this doc for some good examples.
Basically, you want to publish a message with some data when the user is logged in. Your controller for after log in will be able to listen or subscribe to that message. When it receives the message it will be able to run any code you tell it in the callback.
One controller will publish:
// gather some user data with successful login like var id = result.user.id
supersonic.data.channel('loginSuccess').publish({ userid: id });
Another controller will listen:
supersonic.data.channel('loginSuccess').subscribe( function(data) {
$scope.id = data.userid;
// load some data based on user id
});
You can preload the views as well. This may help speed things up. If you don't, you may see a spinner in between view pushes. In structure.coffee in the config folder:
preloads: [
{
id: "viewIdYouSet"
location: "nameOfModule#view"
}
]
When the login finishes, you can find the preloaded view and push it to the stack:
supersonic.ui.views.find("viewIdYouSet").then( function(startedView) {
supersonic.ui.layers.push(startedView);
});
Hope this helps.

What's the idiomatic way to store a value for multiple controllers to use in Angular.js?

The backend of my app uses a rest api that requires the user to login via basic auth. This means every call they make requires them to send the username and password. But I want my app to hide this detail from the user. Yes, they will have a login page where they enter the username and password in, but from then on they should have no idea they're sending the username/password for every backend call.
My question is, how do I save the username/password on the client after the user submits on the login page so it can be used on every rest call thereafter?
This is almost my question, but the difference is
I'm not using a constant like he is
My value can't be initialized at module config time
I'm not sure if this answer is idiomatic
Side Note: I'm sure some people will leave comments saying, "You shouldn't do that!" Fair enough, but that doesn't answer my question. There are other use cases out there where you need to store a value on the client side that can only be initialized after an action. This is what I'd like to learn.
I would suggest storing the values in a service and injecting that service anywhere that you need the value. At config time, you can set the value to undefined and then you can fill it in whenever the value is available. This also allows you to use $watch to determine when the value is set.
I'd also take a look at the HTTP Auth Interceptor Module
for AngularJS.
Here's a rough example that illustrates this concept.
<div ng-app="exampleApp">
<div ng-controller="controller1">
{{ sharedData.someData }}
</div>
<div ng-controller="controller2">
<button ng-click="sharedData.someData='otherData'">Set Data</button>
</div>
</div>
angular.module('exampleApp', []).controller('controller1', function(sharedData, $scope){
$scope.sharedData = sharedData;
}).controller('controller2', function(sharedData, $scope){
$scope.sharedData = sharedData;
}).service('sharedData', function(){
this.someData = "test";
return this;
});

Cakephp One login function / multiple login views

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.

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