is it possible to redirect the user to same page after login using client side technology

By : zaq
Source: Stackoverflow.com
Question!

I have searched quite a lot regarding my problem and I couldn't find any relevant tutorial. Moreover, I am not even sure if it is possible using client side technology.

Problem statement: For e.g I have many pages in my web app and if a user switch from index page to page 1 and then page 2. Now the user decides to login to my web site. I want to redirect the user to page 2 once the login is successful.

Current outcome: Once the login is successful user always seems to get redirected to the index page.

Desired outcome: Once the login is successful the user should stay on page 2.

Is it possible using client side technology? In PHP we could use sessions and all. But I am confined on using client side technology to achieve that.

Here is my login function

function login(params) {

    if(checkEmpty("loginEmail") && checkEmpty("password")) {

        var emailField = $("#loginEmail").val(),
            passwordField = $("#password").val(),
            data = "login=" + emailField + "&password=" + passwordField;

        for (var key in params) {
            data += "&" + key + "=" + params[key];
        }

        // Hide errors as default
        $("#loginErrorWrapper").hide();

        // Try to launch the "normal" submit operation to make browser save email-field's value to cache
        $('#loginSubmitHidden').click();

        // Send data to server and refresh the page if everything is ok
        $.when(loginPost(data)).done(function(map) {

            if(!hasErrors(map)) {

                var lang = map.language;
                if (lang != "") {
                    changeLanguage(lang)
                }
                else {
                    lang = 'en';
                }

                redirect("/" + lang + "/");
            } else {

                if (map.errorCode == "155") {

                    $.fancybox({
                        href : '#termsAcceptancePopup',
                        title : '',
                        beforeLoad : function() {
                            $('#acceptTermsButton').attr('onclick','javascript:login({policyChecked:true});$.fancybox.close();');
                        },
                        helpers : {
                            overlay : { closeClick: false }
                        }
                    });

                } else {

                    var errorString = getErrorMessage(map);
                    $("#loginErrorWrapper").show();
                    $("#loginErrorWrapper").html(errorString);

                }

            }

        });
    }
} 

Ajax request

function loginPost(data) {

    return $.ajax({

        url: "/some-api/login",
        type: "POST",
        dataType: "json",
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        async: true,
        data:data
    });
} 

P.S -> I am not using PHP at all. I am working on a Java based web app.

By : zaq


Answers

So I have tried all the methods suggested in the comment section and all of them worked.

1) Using location.reload()

Once the user is logged in it just refresh the page.

2) Saving the last URL in a cookie

Calling the below function before calling redirect.

createCookie(value1, value2, value3);

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() (days*24*60*60*1000));
        var expires = "; expires=" date.toGMTString();
    }
    else var expires = "";
    document.cookie = name "=" value expires "; path=/";
}

3) Removing redirect("/" lang "/"); from my function since I am using ajax for login. However this method is not useful because once the user is logged in he/she will never know whether everything went fine or not unless he/she refresh the page manually or go to another page.

I am not certain which method is better (performance and loading time) - method 1 or method 2.

By : zaq


This video can help you solving your question :)
By: admin