66 * included LICENSE file for details. *)
77
88open Js_utils
9+ open Lwt
910open Learnocaml_common
11+ open Learnocaml_api
12+
13+ module El = struct
14+ let id s = s, find_component s
15+ module Login_overlay = struct
16+ let login_overlay_id, login_overlay = id " login-overlay"
17+ let login_new_id, login_new = id " login-new"
18+
19+ let upgrade_email_id, upgrade_email = id " upgrade-email-input"
20+ let upgrade_password_id, upgrade_password = id " upgrade-password-input"
21+ let upgrade_confirmation_id, upgrade_confirmation = id " upgrade-confirmation-input"
22+ let upgrade_button_id, upgrade_button = id " upgrade-button"
23+ end
24+ end
25+
26+ let check_email_js email =
27+ let re = Regexp. regexp Learnocaml_data. email_regexp_js in
28+ Learnocaml_data. email_check_length email
29+ && match Regexp. string_match re email 0 with
30+ | Some _ -> true
31+ | None -> false
32+
33+ let init_token_dialog () =
34+ let open El.Login_overlay in
35+ Manip.SetCss. display login_overlay " block" ;
36+ let got_token = match Lwt. task () with
37+ | (_,got_tok) -> got_tok in
38+ let create_token () =
39+ let email = Manip. value upgrade_email and
40+ password = Manip. value upgrade_password and
41+ password_confirmation = Manip. value upgrade_confirmation in
42+ let email_criteria = not (check_email_js email) and
43+ passwd_crit1 = not (Learnocaml_data. passwd_check_length password) and
44+ passwd_crit2 = not (Learnocaml_data. passwd_check_strength password) and
45+ passwd_crit3 = not (password = password_confirmation) in
46+ Manip.SetCss. borderColor upgrade_email " " ;
47+ Manip.SetCss. borderColor upgrade_password " " ;
48+ Manip.SetCss. borderColor upgrade_confirmation " " ;
49+ if email_criteria || passwd_crit1 || passwd_crit2 || passwd_crit3 then
50+ begin
51+ if email_criteria then
52+ Manip.SetCss. borderColor upgrade_email " #f44" ;
53+ if passwd_crit1 || passwd_crit2 then
54+ Manip.SetCss. borderColor upgrade_password " #f44" ;
55+ if passwd_crit3 then
56+ Manip.SetCss. borderColor upgrade_confirmation " #f44" ;
57+ if email_criteria then begin
58+ cb_alert ~title: [% i" ERROR" ]
59+ [% i" The entered e-mail was invalid." ]
60+ (fun () -> Manip. focus upgrade_email)
61+ end
62+ else if passwd_crit1 then begin
63+ cb_alert ~title: [% i" ERROR" ]
64+ [% i" Password must be at least 8 characters long" ]
65+ (fun () -> Manip. focus upgrade_password)
66+ end
67+ else if passwd_crit2 then begin
68+ cb_alert ~title: [% i" ERROR" ]
69+ [% i" Password must contain at least one digit, \
70+ one lower and upper letter, \
71+ and one non-alphanumeric char." ]
72+ (fun () -> Manip. focus upgrade_password)
73+ end
74+ else if passwd_crit3 then begin
75+ cb_alert ~title: [% i" ERROR" ]
76+ [% i" The password and its confirmation are not the same" ]
77+ (fun () -> Manip. focus upgrade_confirmation)
78+ end ;
79+ Lwt. return_none
80+ end
81+ else
82+ let token = Learnocaml_data.Token. to_string (Learnocaml_local_storage. (retrieve sync_token)) in
83+ retrieve (Learnocaml_api. Upgrade
84+ (" email=" ^ email^ " &passwd=" ^ password^ " &token=" ^ token))
85+ (* body exemple ->
86+ email=&passwd=&confirmation=&csrf=Bfkxd/2TjpMAkq4bFGIs1hp9oxeBTZIKioMlQMUDlpk=&token=ZGB-GDD-SNB-41M*)
87+ >> = fun _ -> cb_alert ~title: [% i" VALIDATION REQUIRED" ]
88+ [% i" A confirmation e-mail has been sent to your address." ]
89+ Js_utils. reload;
90+ Lwt. return_none
91+ in
92+ let handler f t = fun _ ->
93+ Lwt. async (fun () ->
94+ f () > |= function
95+ | Some token -> Lwt. wakeup got_token token
96+ | None -> () );
97+ t
98+ in
99+ Manip.Ev. onclick upgrade_button (handler create_token false )
10100
11101let set_string_translations =
12102 List. iter
@@ -28,6 +118,7 @@ let () =
28118 " txt_upgrade_password_confirmation" , [% i" Confirm password" ];
29119 " txt_do_upgrade" , [% i" Upgrade" ];
30120 " txt_info" , [% i" An e-mail will be sent to your address to confirm it." ];
31- ]
121+ ];
122+ init_token_dialog ()
32123 with Not_found ->
33124 Learnocaml_common. alert ~title: [% i" NO TOKEN" ] [% i" You are not logged in" ]
0 commit comments