Files
tools-legacy/public/app.js
T

148 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-12-08 14:35:55 +01:00
var app = new Vue({
el: '#app',
data: {
step: 0,
phone: '',
errorMessage: '',
otp: '',
busy: {
login: false
},
user: {
phone: '',
username: '',
roles: []
},
staff: {
reference: ''
},
fingerDetails: {
'right-thumb': { },
'right-index-finger': { }
},
socket: null
},
created: function () {
var userStr = localStorage.getItem("user");
if (userStr) {
this.user = JSON.parse(userStr);
this.step = 1;
}
this.socket = io();
this.socket.on("connect", () => { })
2021-12-08 16:14:57 +01:00
this.socket.on("fprint-enroll-error", (data) => {
console.log({ event: 'enroll-error', data });
swal("Oops!", `Operation failed. ${data}.`, "error");
this.busy.login = false;
});
2021-12-08 14:35:55 +01:00
this.socket.on("fprint-enroll-done", (data) => {
console.log({ event: 'enroll-done', data});
if (data.ok) {
let { id, fingerprint, data: { finger }} = data;
this.step = 1.2;
this.fingerDetails[finger] = { id, fingerprint, finger }
}
this.busy.login = false;
})
this.socket.on("fprint-verify-done", (data) => {
console.log({ event: 'verify-done', data});
});
},
methods: {
authenticateUrl: function (api, authenticate = true) {
return `https://apis.a2loft.net/identity${api}` + (authenticate ? `?phone=${this.user.phone}&otp=${this.otp}` : '');
},
login: async function () {
this.busy.login = true;
var { data } = await axios.get(
this.authenticateUrl(`/validate/uc/${this.phone}/${this.otp}`, false)
);
this.errorMessage = '';
if (!data.ok)
this.errorMessage = "Invalid OTP specified.";
else {
localStorage.setItem("user", JSON.stringify(data.data));
this.user = data.data;
this.step = 1.0;
}
this.busy.login = false;
},
logout: function () {
localStorage.removeItem("user");
this.step = 0;
},
loadUserByReference: async function () {
this.busy.login = true;
try {
var { data } = await axios.get(
this.authenticateUrl(`/uc/${this.staff.reference}`)
);
if (!data.ok) {
swal("Oops!", `Operation failed. ${data.message}.` + this.staff.reference, "error");
} else {
this.staff = data.data;
// this.staff.verified = false;
this.step = 1.1;
}
} finally {
this.busy.login = false;
}
},
captureFingerPrint: async function (finger) {
this.busy.login = true;
this.step = 1.1;
// var { data: { success, message: { id, data, fingerprint } } } = await axios.post("http://localhost:9000/api/user", {
// name: this.staff.reference
// });
// this.fingerDetails = { id, fingerprint };
// var { data } = await axios.delete(`http://localhost:9000/api/user/${id}`);
// console.log(`${id} purged.`);
this.socket.emit("fprint-enroll", { finger });
},
saveFingerPrint: async function () {
this.busy.login = true;
try {
2021-12-08 16:14:57 +01:00
var fingerprints = Object.keys(this.fingerDetails).map(finger => {
2021-12-08 14:35:55 +01:00
return {
finger,
2021-12-08 17:34:01 +01:00
fingerprint: this.fingerDetails[finger].fingerprint
2021-12-08 14:35:55 +01:00
}
});
2021-12-08 17:36:11 +01:00
if (fingerprints.length < 2) {
swal("Oops!", 'Please capture the two fingers for this profile.', "error");
return;
}
2021-12-08 14:35:55 +01:00
var { data } = await axios.post(
this.authenticateUrl(`/uc/fingerprints/${this.staff.reference}`),
{ fingerprints }
);
if (data.ok) {
swal("Saved", `Fingerprint saved for ${this.staff.name} with reference ${this.staff.reference}.`, "success");
this.newEnrolment();
} else {
swal("Oops!", `Operation failed. ${data.message}.` + this.staff.reference, "error");
}
} finally {
this.busy.login = false;
}
},
newEnrolment: function () {
this.step = 1.0;
2021-12-13 13:59:09 +01:00
this.busy.login = false;
2021-12-08 14:35:55 +01:00
this.staff.reference = "";
2021-12-13 13:59:09 +01:00
this.fingerDetails = {
'right-thumb': { },
'right-index-finger': { }
};
2021-12-08 14:35:55 +01:00
}
}
})