layouts.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 templateInstance = this;
  22. templateInstance.currentSetting = new ReactiveVar();
  23. templateInstance.isLoading = new ReactiveVar(false);
  24. Meteor.subscribe('setting', {
  25. onReady() {
  26. templateInstance.currentSetting.set(Settings.findOne());
  27. return this.stop();
  28. },
  29. });
  30. Meteor.call('isPasswordLoginDisabled', (_, result) => {
  31. if (result) {
  32. $('.at-pwd-form').hide();
  33. }
  34. });
  35. });
  36. Template.userFormsLayout.onRendered(() => {
  37. AccountsTemplates.state.form.keys = new Proxy(
  38. AccountsTemplates.state.form.keys,
  39. validator,
  40. );
  41. const i18nTag = navigator.language;
  42. if (i18nTag) {
  43. T9n.setLanguage(i18nTagToT9n(i18nTag));
  44. }
  45. EscapeActions.executeAll();
  46. });
  47. Template.userFormsLayout.helpers({
  48. currentSetting() {
  49. return Template.instance().currentSetting.get();
  50. },
  51. isLoading() {
  52. return Template.instance().isLoading.get();
  53. },
  54. afterBodyStart() {
  55. return currentSetting.customHTMLafterBodyStart;
  56. },
  57. beforeBodyEnd() {
  58. return currentSetting.customHTMLbeforeBodyEnd;
  59. },
  60. languages() {
  61. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  62. const tag = code;
  63. let name = lang.name;
  64. if (lang.name === 'br') {
  65. name = 'Brezhoneg';
  66. } else if (lang.name === 'ig') {
  67. name = 'Igbo';
  68. } else if (lang.name === 'oc') {
  69. name = 'Occitan';
  70. } else if (lang.name === '繁体中文(台湾)') {
  71. name = '繁體中文(台灣)';
  72. }
  73. return { tag, name };
  74. }).sort(function(a, b) {
  75. if (a.name === b.name) {
  76. return 0;
  77. } else {
  78. return a.name > b.name ? 1 : -1;
  79. }
  80. });
  81. },
  82. isCurrentLanguage() {
  83. const t9nTag = i18nTagToT9n(this.tag);
  84. const curLang = T9n.getLanguage() || 'en';
  85. return t9nTag === curLang;
  86. },
  87. });
  88. Template.userFormsLayout.events({
  89. 'change .js-userform-set-language'(event) {
  90. const i18nTag = $(event.currentTarget).val();
  91. T9n.setLanguage(i18nTagToT9n(i18nTag));
  92. event.preventDefault();
  93. },
  94. 'click #at-btn'(event, templateInstance) {
  95. if (FlowRouter.getRouteName() === 'atSignIn') {
  96. templateInstance.isLoading.set(true);
  97. authentication(event, templateInstance).then(() => {
  98. templateInstance.isLoading.set(false);
  99. });
  100. }
  101. },
  102. });
  103. Template.defaultLayout.events({
  104. 'click .js-close-modal': () => {
  105. Modal.close();
  106. },
  107. });
  108. async function authentication(event, templateInstance) {
  109. const match = $('#at-field-username_and_email').val();
  110. const password = $('#at-field-password').val();
  111. if (!match || !password) return undefined;
  112. const result = await getAuthenticationMethod(
  113. templateInstance.currentSetting.get(),
  114. match,
  115. );
  116. if (result === 'password') return undefined;
  117. // Stop submit #at-pwd-form
  118. event.preventDefault();
  119. event.stopImmediatePropagation();
  120. switch (result) {
  121. case 'ldap':
  122. return new Promise(resolve => {
  123. Meteor.loginWithLDAP(match, password, function() {
  124. resolve(FlowRouter.go('/'));
  125. });
  126. });
  127. case 'cas':
  128. return new Promise(resolve => {
  129. Meteor.loginWithCas(match, password, function() {
  130. resolve(FlowRouter.go('/'));
  131. });
  132. });
  133. default:
  134. return undefined;
  135. }
  136. }
  137. function getAuthenticationMethod(
  138. { displayAuthenticationMethod, defaultAuthenticationMethod },
  139. match,
  140. ) {
  141. if (displayAuthenticationMethod) {
  142. return $('.select-authentication').val();
  143. }
  144. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  145. }
  146. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  147. return new Promise(resolve => {
  148. try {
  149. Meteor.subscribe('user-authenticationMethod', match, {
  150. onReady() {
  151. const user = Users.findOne();
  152. const authenticationMethod = user
  153. ? user.authenticationMethod
  154. : defaultAuthenticationMethod;
  155. resolve(authenticationMethod);
  156. },
  157. });
  158. } catch (error) {
  159. resolve(defaultAuthenticationMethod);
  160. }
  161. });
  162. }