Permalink
Browse files

Simplifying login logic & making it persistent...

i.e. a user remains logged-in/out, even after navigating to other
pages.
  • Loading branch information...
1 parent 69859bb commit 8a5a91401e547e46b8a45ae004ed53d6442bec6a @akoprow akoprow committed May 6, 2011
Showing with 29 additions and 47 deletions.
  1. +2 −2 src/data.opa
  2. +26 −44 src/login.opa
  3. +1 −1 src/main.opa
View
4 src/data.opa
@@ -46,7 +46,7 @@ init_data_store() =
; email = Email.of_string("contact@mlstate.com")
; url = "http://mlstate.com"
; photo = some({png = @static_binary_content("img/mlstate.png")})
- ; passwd = ""
+ ; passwd = "9d62e4f6d08f780e5d5e4f30ca3419d6"
; wallpaper =
{ img = some({png = @static_binary_content("img/mlstate-bg.png")})
; tile = false
@@ -57,4 +57,4 @@ init_data_store() =
| _ ->
void
-_ = init_data_store()
+@server _ = init_data_store()
View
70 src/login.opa
@@ -8,64 +8,46 @@ package mlstate.twopenny
import widgets.loginbox
import components.login
-type Login.credentials =
- { login : string
- ; passwd : string
- }
+type Login.credentials = option((string, string))
-type Login.private_state =
- { user : option(User.ref)
- }
+type Login.state = option(User.ref)
-type Login.public_state = Login.private_state
+type Login.config = CLogin.config(Login.credentials, Login.state, Login.state)
-type Login.config = CLogin.config(Login.credentials, Login.private_state,
- Login.public_state)
-
-type Login.component = CLogin.t(Login.credentials, Login.private_state,
- Login.public_state)
+type Login.component = CLogin.t(Login.credentials, Login.state, Login.state)
Login =
dom_id = "login"
{{
- @publish @server authenticate(cred : Login.credentials, state : Login.private_state)
- : option(Login.private_state) =
- do Log.debug("[LOGIN]", "authentication: [login:{cred.login}, passwd:{cred.passwd}]")
- user_ref = User.mk_ref(cred.login)
- match User.get(user_ref) with
- | {none} -> none
- | {some=user} ->
- passwd = User.mk_passwd(cred.passwd)
- if passwd == user.passwd then
- some({user = some(user_ref)})
- else
- none
+ @private guest_state : Login.state =
+ none
- @private loginbox(onchange : Login.credentials -> void, state : Login.public_state)
- : xhtml =
- login_action(login, passwd) = onchange(~{login passwd})
- user_opt = Option.map((user_ref -> <>{User.ref_to_string(user_ref)}</>), state.user)
- WLoginbox.html(WLoginbox.default_config, dom_id, login_action, user_opt)
+ @publish @server authenticate(cred : Login.credentials, state : Login.state)
+ : option(Login.state) =
+ do Log.debug("[LOGIN]", "authentication: [{cred}]")
+ match cred with
+ | {none} -> none
+ | {some=(login, passwd)} ->
+ user_ref = User.mk_ref(login)
+ match User.get(user_ref) with
+ | {none} -> none
+ | {some=user} ->
+ passwd = User.mk_passwd(passwd)
+ if passwd == user.passwd then
+ some(some(user_ref))
+ else
+ none
@private login_conf : Login.config =
- { ~authenticate
- ; get_credential(state : Login.private_state) : Login.public_state = state
- ; ~loginbox
- ; on_change(_, _) = void
- ; dbpath = none
- ; prelude = none
- }
-
- @private default_state : Login.private_state =
- { user = none }
+ CLogin.default_config(dom_id, authenticate)
- init() : Login.component =
- CLogin.make(default_state, login_conf)
+ login : Login.component =
+ CLogin.make(guest_state, login_conf)
- html(component : Login.component) : xhtml =
- CLogin.html(component)
+ html() : xhtml =
+ CLogin.html(login)
}}
View
2 src/main.opa
@@ -13,7 +13,7 @@ twopenny_page((title, content, style))(_req) =
status =
<div id=#status>
<div id=#loginbox>
- {Login.html(Login.init())}
+ {Login.html()}
</div>
</>
page =

0 comments on commit 8a5a914

Please sign in to comment.