layouts.js 4.4 KB

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