Run Code On First Login - JavaScript onLogin Event
Last week I had a request that a popup window be opened each time a user logged in to a Domino website. They could browse Anonymously without the popup, but as soon as they log in it should appear. Only once per login though and not again until they next logged in.
Kind of like having an onLogin event in JavaScript. Which, we all know, there isn't. So I invented one using a little trickery with cookies. Here's how.
In the $$HTMLHead field I added this as the very first line:
@If( @UserName="Anonymous"; ""; @SetHTTPHeader("Set-Cookie"; "POST_LOGIN_CODE_RAN=1") );
In the Form's HTML Body Attributes I then added the following code:
@If( @UserName!="Anonymous" & !@Contains(HTTP_Cookie; "POST_LOGIN_CODE_RAN"); "onload=\"MyApp.onLogin()\""; "" )
In a "global" Script Library I added:
var MyApp={}; MyApp.onLogin = function(){ document.getElementById('message').innerText= "Welcome. You have just logged in."; }
You can see a demo of it here.
It works by using a kind of loophole, if you will, in the cookie-setting method. That is that the HTTP_Cookie field doesn't contain any cookies you set until the next time you open a page on the site after you set it.
So, when you login, the server sends a cookie to the browser to say you're no longer anonymous. However, at this point, the HTTP_Cookie field does not contain this new cookie as it was merely received by the browser.
This overlap between setting the cookie and the server knowing the page contains the cookie only lasts for one page view. It's this page where you have the chance to run some post login code. Next time you open a page the browser tells the server it does now have the cookie as the HTTP_Cookie field contains the value and so the code doesn't run.
Note: I had thought you could do the same thing by testing for DomAuthSessId in the cookie, but you can't, as it gets set during the names.nsf?login request which results in the 302 redirect to the page being logged in to. So it's not the page you're logging in to which sets this cookie and it does exist by the time the page loads.
I suppose you could do the reverse and have an onLogout event too. If I get chance I'll add it in to the demo before I put in the Sandbox area for download. For now I just want to check this is of use to you all and worth making available? Does it even make sense? I get a bit worried my overly-verbose explanations do no favours for the matter at hand and just confuse what is quite a simple solution.
an other way, would be to put the username (in the first case anonymous) as a value in the cookie and check is the @username != cookie.username.
You only access the login page once, so why not simply add the code to launch the pop up from the onUnload event of the login page.
How do you know the login was successful until the following page has loaded?
In the case I was working on there was no login form, as such. Just two "pretend" fields and a login button on every page while anonymous.
Do I take nobody gets this or sees a use in it?
Jake
I just completed a project: {Link}
On this web site anonymous visitors can view and search for apartments to rent. But if they want to see the details of their search they must establish an account then redo their search.
I'd like it if, after registering and logging in their previous search was displayed to them. Right now, for various reasons, I save a page with their search parameters and their results.
Your blog has given me the idea. Each time a visitor without a certain cookie comes to the web site I'd create a new document and then set a cookie with that UNID as the value. Then, once they register and login I could show them their last search results. Or for that matter I could offer them a view with all their searches before they registered and, of course, all the searches subsequent to registration.
Thanks for the tip and triggering an idea. (I voted for you.)
Peace,
Rob:-]
Thanks Rob. I feel a better now. It took almost the whole of last night to put the sample and blog entry together. It's always a bit upsetting when it doesn't go down as well as you'd like. The fact at least one person found it useful is good enough for me...
Jake
Jake, sometimes these tips aren't useful straight away but are the things that make your site such a goldmine. When you need to know something it usually comes up trumps.
I like it.
Most of my users are not very computer literate & need constant assurance about what's going on.
I think we assume too much about our users..We can never communicate with them too much..sometimes the simple stuff, as shown here, is the most effective.
Whack that example db up there eh Jake.
I'll be downloading it & prodding about it for one.
Rgds
N
Was talking this over with a co-worker this morning when we realized that the .Net code in our intranet does essentially the same thing this to display pop-ups. Never really thought about how it worked, so thanks for the explanation.
I've excavated several gems while digging through your blog archives. Instant feedback is gratifying I'm sure but building a body of work over time is also satisfying (and useful).
I've been following your Ajax exploits silently because I haven't had the right project to add it to. Rest assured, you are appreciated.
Peace,
Rob:-]
Hi Jake,
I find your site useful and entertaining even though I have not been programing Notes for over a year. Keep up the good work.
Thanks
Alan
Couldn this technique be used to set a user's last login date/time (profile or other document) to be displayed on a site perhaps via Ajax?
How would this technique hold up under session authentication in an intranet environment?
My users have been asking for a last login and this just might do it.
Thanks Jake!
Yes, Jordan, it could. You don't need to set the timestamp with Ajax though! You could do it with a WQO agent that only runs when !Anonymous and there's no cookie.
Jake
I've been searching for a way to allow users to have an account and login but not use the standard notes ID method. I was thinking that by using cookie values I would be able to keep people logged in until they close the browser. This looks like it might be a way to do it. Do you know of a better way to accomplish this?
Thanks for all the tips over the years.
Jake, I'd love to get this working but it's beyond me. I've had a look in your sandbox but can't find the demo you referred to above. Did you bother putting in the sandbox in the end?
Steven, look at article http://www.ibm.com/developerworks/lotus/library/ls-Registering_users/index.html. It's old but basics are still the same. It may be just what you need.
You don't have to deal with cookies. Read about session authentication in your administrator help as Domino will take care of cookies.