Wiki source code of Development-Examples-Login

Last modified by Pascal Robert on 2010/09/13 00:32

Hide last authors
Pascal Robert 3.1 1 == Simple Login ==
smmccraw 1.1 2
3 Here is a little example on how to redirect a request to a "login" page if necessary:
4
Pascal Robert 3.1 5 {{code}}
smmccraw 1.1 6
Pascal Robert 3.1 7 public void appendToResponse(WOResponse aResponse, WOContext aContext) {
8 String aPageName = LoginPage.pageWithContext( aContext );
smmccraw 1.1 9
Pascal Robert 3.1 10 if ( ( aPageName != null ) && ( this.name().equals( aPageName ) == false ) ) {
11 WOComponent aComponent = this.pageWithName( aPageName );
smmccraw 1.1 12
Pascal Robert 3.1 13 this.currentSession().setPage( aPageName );
14 aContext._setPageComponent( aComponent );
15 aContext._setCurrentComponent( aComponent );
16 aComponent.appendToResponse( aResponse, aContext);
smmccraw 1.1 17
Pascal Robert 3.1 18 return;
19 }
smmccraw 1.1 20
Pascal Robert 3.1 21 this.currentSession().setPage( this.name() );
22 super.appendToResponse( aResponse, aContext);
23 }
smmccraw 1.1 24
25
Pascal Robert 3.1 26 {{/code}}
smmccraw 1.1 27
Pascal Robert 5.1 28 WOContext._setPageComponent() and WOContext._setCurrentComponent() are undocumented methods, but are required in order to update the WOContext with the current page and component information.
smmccraw 1.1 29
Pascal Robert 3.1 30 The LoginPage class (not shown) will decide if a login is necessary or not.
smmccraw 1.1 31
Pascal Robert 3.1 32 == Avoiding Session Timeouts at Login ==
smmccraw 1.1 33
Pascal Robert 3.1 34 A good login should not timeout when no user is logged in. This can be a problem if your application uses a session in the page generation.
smmccraw 1.1 35
Pascal Robert 3.1 36 Fortunately Apple has provided us with a way of doing this correctly: "DirectAction";. You should implement your login page as a DirectAction and be very careful not to create a "lazy" session otherwise you will have worked for nothing. WO create a session when you call certain methods so you have to be careful. Use this.context().hasSession() to check that everything is OK. After successful login you create a session by calling this.session() in your component and store the user in it; you are in business.
smmccraw 1.1 37
Pascal Robert 3.1 38 On logout you destroy the session and return the direct action page that way the user has a new login screen instead of an empty screen. The easiest way of generating a login page on logout is to return a redirect page (302) to the main URL of the application it is transparent to the user and does exactly what you need.
39
40 You will find the code sample there after. I place it in the Application and when I want to logout I just call handleLogout with the context.
41
42 {{code}}
43
44 private WOResponse responseForPageWithName(String aPageName, WOContext aContext) {
45 if ( aPageName != null ) {
46 WOResponse aResponse = new WOResponse();
47 String adaptorPrefix = aContext.request().adaptorPrefix();
48 String applicationName = aContext.request().applicationName();
49 String anURL = adaptorPrefix + "/" + applicationName + ".woa";
50 aResponse.setHeader(anURL, "Location");
51 aResponse.setHeader("text/html", "content-type");
52 aResponse.setHeader("0","content-length");
53 aResponse.setStatus(302);
54 return aResponse;
55 }
56 return null;
57 }
58
59 public WOResponse handleLogout(WOContext aContext) {
60 WOResponse aResponse = this.responseForPageWithName("Login", aContext);
61 if ( ! aContext.session().isTerminating() ) {
62 ((Session)aContext.session()).setUser(null);
63 aContext.session().terminate();
64 }
65 return aResponse;
66 }
67
68 {{/code}}