meteor-template/client/Accounts/Login/reg.js
2025-12-06 15:44:43 -06:00

137 lines
No EOL
4.1 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
import { SysConfig } from '../../../imports/api/systemConfig';
Template.reg.onCreated(function() {
this.subscribe("SystemConfig");
});
Template.reg.onRendered(function() {
Session.set("canReg", false);
Session.set("missingReq", false);
Session.set("missingName", false);
Session.set("missingEmail", false);
Session.set("missingPassword", false);
Session.set("passMatch", true);
});
Template.reg.helpers({
canReg: function() {
return Session.get("canReg");
},
misName: function() {
return Session.get("missingName");
},
misEmail: function() {
return Session.get("missingEmail");
},
misPass: function() {
return Session.get("missingPassword");
},
misReq: function() {
return Session.get("missingReq");
},
passMatch: function() {
return Session.get("passMatch");
},
allowReg: async() => {
const conf = await SysConfig.findOneAsync();
try {
if (typeof conf != 'undefined') {
return conf.allowReg;
} else {
return true
}
} catch(error) {
console.log(" ERROR getting registration allowed info: " + error);
}
}
});
Template.reg.events({
'click #registerMe' (event) {
event.preventDefault();
if (Session.get("canreg") == false) {
// console.log("reg disabled.");
} else {
// console.log("Clicked");
let missingName = false;
let missingEmail = false;
let missingPassword = false;
let email = $("#email").val();
let password = $("#password").val();
let name = $("#name").val();
if (name == "" || name == null) {
missingName = true;
Session.set("missingName", true);
}
if (email == "" || email == null) {
missingEmail = true;
Session.set("missingEmail", true);
}
if (password == "" || password == null) {
missingPassword = true;
Session.set("missingPassword", true);
}
let userId;
if (missingName == true || missingEmail == true || missingPassword == true) {
Session.set("missingReq", true);
} else {
Session.set("missingReq", false);
Accounts.createUser({
email: email,
password: password,
profile: {
fullname: name,
}
});
let userId = Meteor.userId();
// console.log("User ID: " + userId);
const addRole = async() => {
let result = await Meteor.callAsync("addToRole", "user");
if (!result) {
console.log(" ERROR: ROLES - Error adding user to role: ");
} else {
// console.log("User should be added to role - teacher.");
FlowRouter.go('/dashboard');
}
}
addRole();
}
}
},
'keyup #passwordConfirm' (event) {
let pwd = $("#password").val();
let pwdconf = $("#passwordConfirm").val();
if (pwd == pwdconf) {
// console.log("passwords match");
Session.set("canreg", true);
} else {
// console.log("passwords don't match");
Session.set("canreg", false);
}
},
'change #email' (event) {
let email = $("#email").val();
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
let isEmail = regex.test(email);
if (isEmail == false) {
Session.set("missingEmail", true);
} else {
Session.set("missingEmail", false);
}
},
'click #login' (event) {
event.preventDefault();
FlowRouter.go('/login');
},
});