Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Simplifying login logic & making it persistent...

i.e. a user remains logged-in/out, even after navigating to other
pages.
  • Loading branch information...
commit 8a5a91401e547e46b8a45ae004ed53d6442bec6a 1 parent 69859bb
Adam Koprowski authored May 06, 2011
4  src/data.opa
@@ -46,7 +46,7 @@ init_data_store() =
46 46
         ; email      = Email.of_string("contact@mlstate.com")
47 47
         ; url        = "http://mlstate.com"
48 48
         ; photo      = some({png = @static_binary_content("img/mlstate.png")})
49  
-        ; passwd     = ""
  49
+        ; passwd     = "9d62e4f6d08f780e5d5e4f30ca3419d6"
50 50
         ; wallpaper  =
51 51
             { img = some({png = @static_binary_content("img/mlstate-bg.png")})
52 52
             ; tile = false
@@ -57,4 +57,4 @@ init_data_store() =
57 57
   | _ ->
58 58
     void
59 59
 
60  
-_ = init_data_store()
  60
+@server _ = init_data_store()
70  src/login.opa
@@ -8,22 +8,13 @@ package mlstate.twopenny
8 8
 import widgets.loginbox
9 9
 import components.login
10 10
 
11  
-type Login.credentials =
12  
-  { login : string
13  
-  ; passwd : string
14  
-  }
  11
+type Login.credentials = option((string, string))
15 12
 
16  
-type Login.private_state =
17  
-  { user : option(User.ref)
18  
-  }
  13
+type Login.state = option(User.ref)
19 14
 
20  
-type Login.public_state = Login.private_state
  15
+type Login.config = CLogin.config(Login.credentials, Login.state, Login.state)
21 16
 
22  
-type Login.config = CLogin.config(Login.credentials, Login.private_state,
23  
-                      Login.public_state)
24  
-
25  
-type Login.component = CLogin.t(Login.credentials, Login.private_state,
26  
-                         Login.public_state)
  17
+type Login.component = CLogin.t(Login.credentials, Login.state, Login.state)
27 18
 
28 19
 Login =
29 20
 
@@ -31,41 +22,32 @@ Login =
31 22
 
32 23
 {{
33 24
 
34  
-  @publish @server authenticate(cred : Login.credentials, state : Login.private_state)
35  
-    : option(Login.private_state) =
36  
-    do Log.debug("[LOGIN]", "authentication: [login:{cred.login}, passwd:{cred.passwd}]")
37  
-    user_ref = User.mk_ref(cred.login)
38  
-    match User.get(user_ref) with
39  
-    | {none} -> none
40  
-    | {some=user} ->
41  
-        passwd = User.mk_passwd(cred.passwd)
42  
-        if passwd == user.passwd then
43  
-          some({user = some(user_ref)})
44  
-        else
45  
-          none
  25
+  @private guest_state : Login.state =
  26
+    none
46 27
 
47  
-  @private loginbox(onchange : Login.credentials -> void, state : Login.public_state)
48  
-    : xhtml =
49  
-    login_action(login, passwd) = onchange(~{login passwd})
50  
-    user_opt = Option.map((user_ref -> <>{User.ref_to_string(user_ref)}</>), state.user)
51  
-    WLoginbox.html(WLoginbox.default_config, dom_id, login_action, user_opt)
  28
+  @publish @server authenticate(cred : Login.credentials, state : Login.state)
  29
+    : option(Login.state) =
  30
+    do Log.debug("[LOGIN]", "authentication: [{cred}]")
  31
+    match cred with
  32
+    | {none} -> none
  33
+    | {some=(login, passwd)} ->
  34
+        user_ref = User.mk_ref(login)
  35
+        match User.get(user_ref) with
  36
+        | {none} -> none
  37
+        | {some=user} ->
  38
+           passwd = User.mk_passwd(passwd)
  39
+           if passwd == user.passwd then
  40
+             some(some(user_ref))
  41
+           else
  42
+             none
52 43
 
53 44
   @private login_conf : Login.config =
54  
-    { ~authenticate
55  
-    ; get_credential(state : Login.private_state) : Login.public_state  = state
56  
-    ; ~loginbox
57  
-    ; on_change(_, _) = void
58  
-    ; dbpath = none
59  
-    ; prelude = none
60  
-    }
61  
-
62  
-  @private default_state : Login.private_state =
63  
-    { user = none }
  45
+    CLogin.default_config(dom_id, authenticate)
64 46
 
65  
-  init() : Login.component =
66  
-    CLogin.make(default_state, login_conf)
  47
+  login : Login.component =
  48
+    CLogin.make(guest_state, login_conf)
67 49
 
68  
-  html(component : Login.component) : xhtml =
69  
-    CLogin.html(component)
  50
+  html() : xhtml =
  51
+    CLogin.html(login)
70 52
 
71 53
 }}
2  src/main.opa
@@ -13,7 +13,7 @@ twopenny_page((title, content, style))(_req) =
13 13
   status =
14 14
     <div id=#status>
15 15
       <div id=#loginbox>
16  
-        {Login.html(Login.init())}
  16
+        {Login.html()}
17 17
       </div>
18 18
     </>
19 19
   page =

0 notes on commit 8a5a914

Please sign in to comment.
Something went wrong with that request. Please try again.