@@ -799,6 +799,41 @@ let init_sync_session button_group =
799799 Lwt. return (Some session))
800800 (fun _ -> Lwt. return None )
801801
802+ (* * [migrate_from_legacy_token] runs once to move old browsers
803+ that still keep the old [sync-token] (v 1.x and earlier)
804+ over to the new session-based login used since Learn-OCaml 2.0. *)
805+ let migrate_from_legacy_token () =
806+ let token =
807+ try
808+ Some (Learnocaml_local_storage. (retrieve sync_token))
809+ with Not_found -> None
810+ in
811+ match token with
812+ | None -> Lwt. return ()
813+ | Some token ->
814+ Server_caller. request (Learnocaml_api. Login token) >> = function
815+ | Error e ->
816+ Learnocaml_common. alert
817+ ~title: [% i" Migration error" ]
818+ (Server_caller. string_of_error e);
819+ Lwt. return_unit
820+
821+ | Ok session ->
822+ Learnocaml_local_storage. (delete sync_token);
823+ Learnocaml_local_storage. (store sync_session session);
824+ Learnocaml_local_storage. (store is_teacher (Learnocaml_data.Token. is_teacher token));
825+
826+ Server_caller. request (Learnocaml_api. Fetch_save_s session) >> = (function
827+ | Ok save ->
828+ set_state_from_save_file ~session save;
829+ Learnocaml_common. alert
830+ ~title: [% i" Connection preserved" ]
831+ [% i" The application has been upgraded to a session-based \
832+ authentication. Your previous connection was restored" ];
833+ Lwt. return_unit
834+ | Error _ ->
835+ Lwt. return_unit)
836+
802837let set_string_translations () =
803838 let configured v s = Js.Optdef. case v (fun () -> s) Js. to_string in
804839 let translations = [
@@ -855,6 +890,7 @@ let () =
855890 Js. string (" Learn OCaml" ^ " v" ^ Learnocaml_api. version);
856891 Manip. setInnerText El. version (" v" ^ Learnocaml_api. version);
857892 Learnocaml_local_storage. init () ;
893+ migrate_from_legacy_token () >> = fun () ->
858894 let sync_button_group = button_group () in
859895 disable_button_group sync_button_group;
860896 let menu_hidden = ref true in
0 commit comments