diff options
Diffstat (limited to 'client/components/main/layouts.js')
-rw-r--r-- | client/components/main/layouts.js | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/client/components/main/layouts.js b/client/components/main/layouts.js index f12718a7..d5113a25 100644 --- a/client/components/main/layouts.js +++ b/client/components/main/layouts.js @@ -6,7 +6,36 @@ const i18nTagToT9n = (i18nTag) => { return i18nTag; }; +const validator = { + set(obj, prop, value) { + if (prop === 'state' && value !== 'signIn') { + $('.at-form-authentication').hide(); + } else if (prop === 'state' && value === 'signIn') { + $('.at-form-authentication').show(); + } + // The default behavior to store the value + obj[prop] = value; + // Indicate success + return true; + }, +}; + +Template.userFormsLayout.onCreated(function() { + const instance = this; + instance.currentSetting = new ReactiveVar(); + instance.isLoading = new ReactiveVar(false); + + Meteor.subscribe('setting', { + onReady() { + instance.currentSetting.set(Settings.findOne()); + return this.stop(); + }, + }); +}); + Template.userFormsLayout.onRendered(() => { + AccountsTemplates.state.form.keys = new Proxy(AccountsTemplates.state.form.keys, validator); + const i18nTag = navigator.language; if (i18nTag) { T9n.setLanguage(i18nTagToT9n(i18nTag)); @@ -15,6 +44,22 @@ Template.userFormsLayout.onRendered(() => { }); Template.userFormsLayout.helpers({ + currentSetting() { + return Template.instance().currentSetting.get(); + }, + + isLoading() { + return Template.instance().isLoading.get(); + }, + + afterBodyStart() { + return currentSetting.customHTMLafterBodyStart; + }, + + beforeBodyEnd() { + return currentSetting.customHTMLbeforeBodyEnd; + }, + languages() { return _.map(TAPi18n.getLanguages(), (lang, code) => { const tag = code; @@ -47,6 +92,15 @@ Template.userFormsLayout.events({ T9n.setLanguage(i18nTagToT9n(i18nTag)); evt.preventDefault(); }, + 'click #at-btn'(event, instance) { + if (FlowRouter.getRouteName() === 'atSignIn') { + instance.isLoading.set(true); + authentication(event, instance) + .then(() => { + instance.isLoading.set(false); + }); + } + }, }); Template.defaultLayout.events({ @@ -54,3 +108,64 @@ Template.defaultLayout.events({ Modal.close(); }, }); + +async function authentication(event, instance) { + const match = $('#at-field-username_and_email').val(); + const password = $('#at-field-password').val(); + + if (!match || !password) return undefined; + + const result = await getAuthenticationMethod(instance.currentSetting.get(), match); + + if (result === 'password') return undefined; + + // Stop submit #at-pwd-form + event.preventDefault(); + event.stopImmediatePropagation(); + + switch (result) { + case 'ldap': + return new Promise((resolve) => { + Meteor.loginWithLDAP(match, password, function() { + resolve(FlowRouter.go('/')); + }); + }); + + case 'cas': + return new Promise((resolve) => { + Meteor.loginWithCas(match, password, function() { + resolve(FlowRouter.go('/')); + }); + }); + + default: + return undefined; + } +} + +function getAuthenticationMethod({displayAuthenticationMethod, defaultAuthenticationMethod}, match) { + if (displayAuthenticationMethod) { + return $('.select-authentication').val(); + } + return getUserAuthenticationMethod(defaultAuthenticationMethod, match); +} + +function getUserAuthenticationMethod(defaultAuthenticationMethod, match) { + return new Promise((resolve) => { + try { + Meteor.subscribe('user-authenticationMethod', match, { + onReady() { + const user = Users.findOne(); + + const authenticationMethod = user + ? user.authenticationMethod + : defaultAuthenticationMethod; + + resolve(authenticationMethod); + }, + }); + } catch(error) { + resolve(defaultAuthenticationMethod); + } + }); +} |