Advanced XPages : A Nicer Domino Login Part 2
Tags : Lotus Domino XPages
Bookmark :
In part one of this mini series I described how to add the dojo modules to our custom control and created the layout for the dialog box where the user enters their username and password. In this part I'll create and describe the client side javascript that will perform the actual login.
In domino designer go to the Code -> Script Libraries and click the button to create a new javascript library. I called mine 'login' and here is the javascript code I entered.
The script libary will contain a single function called dominoLogin which accepts as it's arguments a keypaired set of fieldnames and values from the dojo dialog box that we created in the last part.
We then do an AJAX post to the url of 'names.nsf?login' and the username, password and redirect fields are passed in as the contents.
When the ajax request is returned to the browser the 'load' function is run using the returned value. If you remember I was redirecting to a gif file. If the login was successful then the gif file is returned so I can check for that by looking at the first three characters of the file. If they are equal to 'GIF' then I know I'm logged in. I can set a 'Please wait' message and then reload the page. the dojo.byID finds a div on my page to update either the style or the inner HTML.
What if I didn't get the gif file returned? Well it can mean one of two things, I've either successfully logged into the server but I am not authorized to access the application because of the database ACL or I have not logged in because I have supplied an invalid username or password.
To see if I have logged into the server I check for the DomAuthSessID or LtpaToken cookies. One of these two cookies will exist if I'm logged in successfully but I already know that if I've gotten this far in the process then I didn;t have the correct rights to open the database. So if the cookie exists, then use the dojo.ByID to display an error message and also clear the cookies ( effectively logging you back out of the server )
If the cookies didn't exist and we got this far in the processing then it was just a plain failed login so again display an error message
If the overall AJAX request failed then I'm just outputting the error to the console so if you have the javascript debugger turned on you'd see the error.
Now that the javascript library is done return to the 'layout_banner_Login' custom control and in the resources section and click the 'Add script Library' option and select the script library we just created.
We now have the custom control and the script library created. In the next and final part I'll adjust the original layout_Banner custom control to load the new login method.
Bookmark :
In part one of this mini series I described how to add the dojo modules to our custom control and created the layout for the dialog box where the user enters their username and password. In this part I'll create and describe the client side javascript that will perform the actual login.
In domino designer go to the Code -> Script Libraries and click the button to create a new javascript library. I called mine 'login' and here is the javascript code I entered.
function dominoLogin(data){
dojo.xhrPost({
url: '/names.nsf?login',
content: data,
load: function (data) {
if( String(data).substring(0,3) == "GIF")
{
dojo.byId("loginMsg").style.color = "green"
dojo.byId("loginMsg").style.backgroundColor = "transparent"
dojo.byId("loginMsg").innerHTML = "Please Wait..."
location.reload();
} else {
if ( dojo.cookie('DomAuthSessId') != null || dojo.cookie('LtpaToken') != null )
{
dojo.byId("loginMsg").style.color = "red"
dojo.byId("loginMsg").style.backgroundColor = "transparent"
dojo.byId("loginMsg").innerHTML = "Not Authorized."
dojo.cookie('DomAuthSessId', null, { path: '/', domain: 'qtzar.com' });
dojo.cookie('LtpaToken', null, { path: '/', domain: 'qtzar.com' });
} else {
dojo.byId("loginMsg").style.color = "red"
dojo.byId("loginMsg").style.backgroundColor = "transparent"
dojo.byId("loginMsg").innerHTML = "Invalid User/Password."
}
}
},
error: function (error) {
console.error ('Error: ', error);
}
});
}
The script libary will contain a single function called dominoLogin which accepts as it's arguments a keypaired set of fieldnames and values from the dojo dialog box that we created in the last part.
We then do an AJAX post to the url of 'names.nsf?login' and the username, password and redirect fields are passed in as the contents.
When the ajax request is returned to the browser the 'load' function is run using the returned value. If you remember I was redirecting to a gif file. If the login was successful then the gif file is returned so I can check for that by looking at the first three characters of the file. If they are equal to 'GIF' then I know I'm logged in. I can set a 'Please wait' message and then reload the page. the dojo.byID finds a div on my page to update either the style or the inner HTML.
What if I didn't get the gif file returned? Well it can mean one of two things, I've either successfully logged into the server but I am not authorized to access the application because of the database ACL or I have not logged in because I have supplied an invalid username or password.
To see if I have logged into the server I check for the DomAuthSessID or LtpaToken cookies. One of these two cookies will exist if I'm logged in successfully but I already know that if I've gotten this far in the process then I didn;t have the correct rights to open the database. So if the cookie exists, then use the dojo.ByID to display an error message and also clear the cookies ( effectively logging you back out of the server )
If the cookies didn't exist and we got this far in the processing then it was just a plain failed login so again display an error message
If the overall AJAX request failed then I'm just outputting the error to the console so if you have the javascript debugger turned on you'd see the error.
Now that the javascript library is done return to the 'layout_banner_Login' custom control and in the resources section and click the 'Add script Library' option and select the script library we just created.
We now have the custom control and the script library created. In the next and final part I'll adjust the original layout_Banner custom control to load the new login method.
Comments
Posted by null At 12:39:37 PM On 04/20/2009 | - Website - |
Second, I have a question. I would like to use a similar dijit dialog to get user input, but I seem to have trouble getting reliable information about how to go about that. If there is a good complete dojo tutorial somewhere I have yet to find it. So... what is your secret - which internet source(s) do you use? For ex. how did you know to use "arguments" to pass the name/value pairs of your fields to the function?
Maybe you could consider doing a special post with a few of the internet sites you use most often for reference? For ex. I tried to find some additional info about the IBM OneUI theme you used in your tutorial - zip!
Thanks again. I look forward to your next post.
Posted by Helene At 07:41:12 PM On 04/21/2009 | - Website - |