Attempt to resurrect qomet code - need some rewrite
[qomet.git] / public / javascripts / login.js
CommitLineData
8a2b3260
BA
1const messages = {
2 "login": "Go",
3 "register": "Send",
4};
e99c53fb 5
8a2b3260
BA
6const ajaxUrl = {
7 "login": "/sendtoken",
8 "register": "/register",
9};
e99c53fb 10
73609d3b
BA
11const ajaxMethod = {
12 "login": "PUT",
13 "register": "POST",
14};
15
8a2b3260
BA
16const infos = {
17 "login": "Connection token sent. Check your emails!",
18 "register": "Registration complete! Please check your emails.",
19};
e99c53fb 20
8a2b3260 21const animationDuration = 300; //in milliseconds
e99c53fb 22
8a2b3260
BA
23// Basic anti-bot measure: force at least N seconds between arrival on page, and register form validation:
24const enterTime = Date.now();
e99c53fb 25
8a2b3260
BA
26new Vue({
27 el: '#login',
28 data: {
29 messages: messages,
30 user: {
31 name: "",
32 email: "",
e99c53fb 33 },
8a2b3260
BA
34 stage: "login", //or "register"
35 },
36 mounted: function() {
37 // https://laracasts.com/discuss/channels/vue/vuejs-set-focus-on-textfield
38 this.$refs.userEmail.focus();
39 },
40 methods: {
41 toggleStage: function(stage) {
42 let $form = $("#form");
43 $form.fadeOut(animationDuration);
44 setTimeout( () => {
45 this.stage = stage;
46 $form.show(0);
47 }, animationDuration);
e99c53fb 48 },
8a2b3260
BA
49 submit: function() {
50 if (this.stage=="register")
51 {
52 if (Date.now() - enterTime < 5000)
53 return;
54 }
55 let error = Validator.checkObject({email: this.user.email}, "User");
56 if (!error && this.stage == "register")
57 error = Validator.checkObject({name: this.user.name}, "User");
58 let $dialog = $("#dialog");
59 show($dialog);
60 setTimeout(() => {hide($dialog);}, 3000);
61 if (error.length > 0)
62 return showMsg($dialog, "error", error);
63 showMsg($dialog, "process", "Processing... Please wait");
64 $.ajax(ajaxUrl[this.stage],
e99c53fb 65 {
73609d3b 66 method: ajaxMethod[this.stage],
8a2b3260 67 data:
e99c53fb 68 {
8a2b3260
BA
69 email: encodeURIComponent(this.user.email),
70 name: encodeURIComponent(this.user.name), //may be unused
71 },
72 dataType: "json",
73 success: res => {
74 if (!res.errmsg)
e99c53fb 75 {
8a2b3260
BA
76 this.user["name"] = "";
77 this.user["email"] = "";
78 showMsg($dialog, "info", infos[this.stage]);
79 }
80 else
81 showMsg($dialog, "error", res.errmsg);
82 },
83 }
84 );
85 },
86 }
87});