layouts.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. BlazeLayout.setRoot('body');
  2. const i18nTagToT9n = (i18nTag) => {
  3. // t9n/i18n tags are same now, see: https://github.com/softwarerero/meteor-accounts-t9n/pull/129
  4. // but we keep this conversion function here, to be aware that that they are different system.
  5. return i18nTag;
  6. };
  7. const validator = {
  8. set(obj, prop, value) {
  9. if (prop === 'state' && value !== 'signIn') {
  10. $('.at-form-authentication').hide();
  11. } else if (prop === 'state' && value === 'signIn') {
  12. $('.at-form-authentication').show();
  13. }
  14. // The default behavior to store the value
  15. obj[prop] = value;
  16. // Indicate success
  17. return true;
  18. },
  19. };
  20. Template.userFormsLayout.onCreated(function() {
  21. const instance = this;
  22. instance.currentSetting = new ReactiveVar();
  23. instance.isLoading = new ReactiveVar(false);
  24. Meteor.subscribe('setting', {
  25. onReady() {
  26. instance.currentSetting.set(Settings.findOne());
  27. return this.stop();
  28. },
  29. });
  30. });
  31. Template.userFormsLayout.onRendered(() => {
  32. AccountsTemplates.state.form.keys = new Proxy(AccountsTemplates.state.form.keys, validator);
  33. const i18nTag = navigator.language;
  34. if (i18nTag) {
  35. T9n.setLanguage(i18nTagToT9n(i18nTag));
  36. }
  37. EscapeActions.executeAll();
  38. });
  39. Template.userFormsLayout.helpers({
  40. currentSetting() {
  41. return Template.instance().currentSetting.get();
  42. },
  43. isLoading() {
  44. return Template.instance().isLoading.get();
  45. },
  46. afterBodyStart() {
  47. return currentSetting.customHTMLafterBodyStart;
  48. },
  49. beforeBodyEnd() {
  50. return currentSetting.customHTMLbeforeBodyEnd;
  51. },
  52. languages() {
  53. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  54. const tag = code;
  55. let name = lang.name;
  56. if (lang.name === 'br') {
  57. name = 'Brezhoneg';
  58. } else if (lang.name === 'ig') {
  59. name = 'Igbo';
  60. }
  61. return { tag, name };
  62. }).sort(function(a, b) {
  63. if (a.name === b.name) {
  64. return 0;
  65. } else {
  66. return a.name > b.name ? 1 : -1;
  67. }
  68. });
  69. },
  70. isCurrentLanguage() {
  71. const t9nTag = i18nTagToT9n(this.tag);
  72. const curLang = T9n.getLanguage() || 'en';
  73. return t9nTag === curLang;
  74. },
  75. });
  76. Template.userFormsLayout.events({
  77. 'change .js-userform-set-language'(evt) {
  78. const i18nTag = $(evt.currentTarget).val();
  79. T9n.setLanguage(i18nTagToT9n(i18nTag));
  80. evt.preventDefault();
  81. },
  82. 'click #at-btn'(event, instance) {
  83. if (FlowRouter.getRouteName() === 'atSignIn') {
  84. instance.isLoading.set(true);
  85. authentication(event, instance)
  86. .then(() => {
  87. instance.isLoading.set(false);
  88. });
  89. }
  90. },
  91. });
  92. Template.defaultLayout.events({
  93. 'click .js-close-modal': () => {
  94. Modal.close();
  95. },
  96. });
  97. async function authentication(event, instance) {
  98. const match = $('#at-field-username_and_email').val();
  99. const password = $('#at-field-password').val();
  100. if (!match || !password) return undefined;
  101. const result = await getAuthenticationMethod(instance.currentSetting.get(), match);
  102. if (result === 'password') return undefined;
  103. // Stop submit #at-pwd-form
  104. event.preventDefault();
  105. event.stopImmediatePropagation();
  106. switch (result) {
  107. case 'ldap':
  108. return new Promise((resolve) => {
  109. Meteor.loginWithLDAP(match, password, function() {
  110. resolve(FlowRouter.go('/'));
  111. });
  112. });
  113. case 'cas':
  114. return new Promise((resolve) => {
  115. Meteor.loginWithCas(match, password, function() {
  116. resolve(FlowRouter.go('/'));
  117. });
  118. });
  119. default:
  120. return undefined;
  121. }
  122. }
  123. function getAuthenticationMethod({displayAuthenticationMethod, defaultAuthenticationMethod}, match) {
  124. if (displayAuthenticationMethod) {
  125. return $('.select-authentication').val();
  126. }
  127. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  128. }
  129. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  130. return new Promise((resolve) => {
  131. try {
  132. Meteor.subscribe('user-authenticationMethod', match, {
  133. onReady() {
  134. const user = Users.findOne();
  135. const authenticationMethod = user
  136. ? user.authenticationMethod
  137. : defaultAuthenticationMethod;
  138. resolve(authenticationMethod);
  139. },
  140. });
  141. } catch(error) {
  142. resolve(defaultAuthenticationMethod);
  143. }
  144. });
  145. }