Wiki source code of Development-Examples-Login

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

Show last authors
1 == Simple Login ==
2
3 Here is a little example on how to redirect a request to a "login" page if necessary:
4
5 {{code}}
6
7 public void appendToResponse(WOResponse aResponse, WOContext aContext) {
8 String aPageName = LoginPage.pageWithContext( aContext );
9
10 if ( ( aPageName != null ) && ( this.name().equals( aPageName ) == false ) ) {
11 WOComponent aComponent = this.pageWithName( aPageName );
12
13 this.currentSession().setPage( aPageName );
14 aContext._setPageComponent( aComponent );
15 aContext._setCurrentComponent( aComponent );
16 aComponent.appendToResponse( aResponse, aContext);
17
18 return;
19 }
20
21 this.currentSession().setPage( this.name() );
22 super.appendToResponse( aResponse, aContext);
23 }
24
25
26 {{/code}}
27
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.
29
30 The LoginPage class (not shown) will decide if a login is necessary or not.
31
32 == Avoiding Session Timeouts at Login ==
33
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.
35
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.
37
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}}