2
0

layouts.js 3.9 KB

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