zxcvbn.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* global zxcvbn */
  2. import Ember from 'ember';
  3. export default Ember.Service.extend({
  4. includedJs: false,
  5. loadedJs: false,
  6. scorePassword: function(password, inputs) {
  7. // 0-4 score, the more the stronger password
  8. return zxcvbn(password, inputs).score;
  9. },
  10. load: function() {
  11. if (!this.get('includedJs')) {
  12. this._includeJs();
  13. }
  14. if (!this.get('loadedJs')) {
  15. return this._loadingPromise();
  16. } else {
  17. return this._loadedPromise();
  18. }
  19. },
  20. _includeJs: function() {
  21. this.loader.require('misago/js/zxcvbn.js');
  22. this.set('includedJs', true);
  23. },
  24. _loadingPromise: function() {
  25. var self = this;
  26. return new Ember.RSVP.Promise(function(resolve) {
  27. var wait = function() {
  28. if (typeof zxcvbn === "undefined") {
  29. Ember.run.later(function () {
  30. wait();
  31. }, 200);
  32. } else {
  33. self.set('loadedJs', true);
  34. Ember.run(null, resolve);
  35. }
  36. };
  37. wait();
  38. });
  39. },
  40. _loadedPromise: function() {
  41. // we have already loaded zxcvbn.js, resolve away!
  42. return new Ember.RSVP.Promise(function(resolve) {
  43. Ember.run(null, resolve);
  44. });
  45. }
  46. });