Tuesday, 4 April 2017

Change the default content of a site if the site is redirected

So I have a eventhandler for a website called "login" :

 function _GetLoginEventHandler(req, res) {
    var answer = _getHTMLSkeleton();

    var dynamicAnswer;
    if (isLogedIn == false) {
         dynamicAnswer = "<h1>Login</h1>" + //creates a form for not logged users
            "<form action='/login' method='post'>" +
            "User name: <br>" +
            '<input type="text" name="username" value="MickeyMouse">' +
            "<br>" +
            "Password: <br>" +
            '<input type="password" name="password" value="geheim">' +
            "<br><br>" +
            '<input type="submit" value="Submit">' +
            "</form> ";
         answer = answer.replace('{title}', "Login");
         answer = answer.replace("{body}", dynamicAnswer);
         res.write(answer);
         res.end();
    }
    else {
        res.writeHead(302, {  //if the user is loged it, it redirects to his "profile"
            'Location': '/user'
        });
        res.end();
    }     
};

If the user is already logged in, it redirect him to a page called "user" which is in my case his profile:

 function _GetUserEventHandler(req, res) {
    var answer = _getHTMLSkeleton();

    var dynamicAnswer;
    if (isLogedIn == true) {
        dynamicAnswer = "<h1>Logg Off</h1>" +
            "<p>Your user id is: " + uID + "</p>" +
            "<form action='/user' method='post'>" +
            '<input type="submit" value="Submit">' + "</form>";
    }
    else
        dynamicAnswer = "<h4>You are not allowed to see this content, you are not loged in </h4>";

    answer = answer.replace('{title}', "User");
    answer = answer.replace("{body}", dynamicAnswer);
    res.write(answer);
    res.end();
}

I want to check if the home site is redirected from (in this case) the login page so I can add to "answer" for example "You are succesfully logged in". PS: The _getHTMLSkeleton returns the structur of a HTML page with placeholders



via igodie

No comments:

Post a Comment