views:

53

answers:

2

Hi Experts,

I am working on a website, where we want user to login using Javascript and not using postback. I have few pages which user can view without logging in, but on these pages if he wants to do something like "Add to favourite" or "Report abuse" or similar, he has to log in. I can display a div where he can log in. But I want the system to perform the task he initially tried to do. So say if the user wants to perform "Add to favourite", he should first log in and on success othere function "Add to favourite" should be called. So logic should know where to delegate once user is logged in.

As this loging stuff is required for many other purposes too, so I can hard code one function once log on is successful. I need something like delegation which Login Routine should know so that it calls it back.

Help will be appritiated.

Regards Parminder

+2  A: 

You could store the lastAttemptedAction in the session, and always check this upon a successful login. So the order of events would be:

  1. User accesses site anonymously
  2. User clicks "add to favorite"
  3. "Add to favorite" is added to Session.lastAttemptedAction
  4. User is asked to login
  5. User successfully logs in
  6. Session.lastAttemptedAction is performed/cleared

If nothing is stored in the lastAttemptedAction, then nothing will be ran. The user will silently login, and continue on his or her merry way.

Jonathan Sampson
+4  A: 

This assumes you have some basic experience with AJAX and callbacks...

// When the user submits the login form, call the login function with
// the original call as the third parameter

function login(username, password, callback) {
    // Perform AJAX call with username and password.
    // Your AJAX utility should call loginResult as its callback
    // Store the callback parameter on your object somewhere
}

function loginResult(result, callback) {
    if (/* Check if the result contains a valid user or sessionID, etc. */) {
         // Logged in, yay.  Do stuff to the UI to show this.
         if (callback) callback();
    } else {
         // Error logging in, oh no.  Display error.
    }
}
Renesis
Nice presentation of your code with comments.
DOK
this login function is being called on the button click on the login screen, how this function will know what is callback function.
Parminder
@parminder - a number of ways; you could store it on the form object that represents your login form.
Renesis