1

I'm trying to maintain session after page refresh in angularjs using java. I have seen many examples but, i didn't find proper solution which exactly I'm looking for. Please find below login snippet code, when i click on login button it is calling loginUser()function in LoginController When i do page refresh it is going to LoginController but it is not going inside loginUser()function.

According to my knowledge until we call function, it doesn't goes inside of it. When I do refresh how can i call back loginUser() function.

please help me out from these. Appreciated..Many thanks.

LoginController.js

function LoginController($scope, $http, $location, $rootScope,
    userService, SessionIdService) {
$scope.user = {};
$scope.user.username = '';
$scope.user.password = '';


$rootScope.loginUser = function(username, password) {

    $scope.resetError();

    $http.post('/user/main/login/' + username, password).success(
            function(login) {

                if (login.sessionId === null) {
                    $scope.setError(login.status);
                    return;
                } else {


                    $rootScope.userlogin = login.uname;
                    userService.setUserName(login.uname);
                    SessionIdService.setSessionId(login.sessionId);
                    $location.path("/home");
                }

            }).error(function() {
        $scope.setError('Invalid user/password combination');
    });

};

$scope.resetError = function() {
    $scope.error = false;
    $scope.errorMessage = '';
};

$scope.setError = function(message) {
    $scope.error = true;
    $scope.errorMessage = message;
    $rootScope.sees = '';
    $rootScope.userlogin = '';
};

};

app.js

app.run(function($rootScope, $location, SessionIdService) {


$rootScope.$on("$routeChangeStart", function(event, next, current) {

    console.log("Routechanged... ");

    if (SessionIdService.getSessionId == "true") {


        if (next.templateUrl == "scripts/views/homescreen.html") {
            $location.path("/home");

        } else {

            $location.path("/screen");
        }
    }
});

});

login.html

<input name="textfield" type="text" ng-model="user.username"/>   
<input name="textfield" type="password" ng-model="user.password"/>
<button type="button" ng-lick="loginUser(user.username,user.password)">Login</button>
2
  • Are you talking about refreshing the page after login is done or before. If it is before, user has to enter username\password again and click button again after refresh. Commented Apr 17, 2014 at 12:01
  • After login, on user home page when i refresh I'm unable to maintain user data. Commented Apr 18, 2014 at 4:09

1 Answer 1

0

It is not clear to me why you want to call loginUser after page refresh. Isn't the user already logged in? I think what you want is to call the success function inside the loginIser. In that case, you need to embed that data as a global JS variable inside your Java template, and pass that to your controller somehow.

You probably want these to be run after refresh: $rootScope.userlogin = login.uname; userService.setUserName(login.uname); SessionIdService.setSessionId(login.sessionId); $location.path("/home"); So, in your Java template, do something like:

<script>window.UNAME = {% this comes from your database %};window.SESSIONID={% similar %}</script>

Then, call that function somehow with window.UNAME as input. (or in your controller, check for the existence of window.UNAME and call it immediately. Something like:

window.UNAME && function (name, sessionId) {
                    $rootScope.userlogin = uname;
                    userService.setUserName(uname);
                    SessionIdService.setSessionId(sessionId);
                    $location.path("/home");
) {

}(window.UNAME, window.SESSION_ID)

Some other recommendations (unrelated to your main problem probably):

First of, change $rootScope.loginUser = function(username, password) { to

$rootScope.loginUser = function() { var username = $scope.user.username; var password = $scope.user.password

since you already have access to username and password there. So, change ng-click="loginUser()".

Second, SessionIdService.getSessionId == "true" seems off, check should probably be just SessionIdService.getSessionId

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. Here, when user suppose to click on refresh,it is calling controller but i'm unable to go inside function. I want to recall that function. I followed below example,in that part2 is not working for me as expected. blog.e-bluesoft.com/…
Issue solved, I'm able to recall the function and maintain session. Thanks for all your replies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.