forked from auth0/lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1321 lines (1057 loc) · 32.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Insert css when first loaded
*/
require('./lib/insert-css');
/**
* Module dependencies.
*/
var bonzo = require('bonzo');
var _ = require('underscore');
var Auth0 = require('auth0-js');
var $ = require('./lib/bonzo-augmented');
var EventEmitter = require('events').EventEmitter;
var strategies = require('./lib/strategies');
var template = require('./lib/html/main.ejs');
var HeaderView = require('./lib/header');
var SigninPanel = require('./lib/mode-signin');
var SignupPanel = require('./lib/mode-signup');
var ResetPanel = require('./lib/mode-reset');
var LoggedinPanel = require('./lib/mode-loggedin');
var KerberosPanel = require('./lib/mode-kerberos');
var LoadingPanel = require('./lib/mode-loading');
var OptionsManager = require('./lib/options-manager');
//browser incompatibilities fixes
var placeholderSupported = require('./lib/supports-placeholder');
var has_animations = require('./lib/supports-animation');
var ocreate = require('./lib/object-create');
var stop = require('./lib/stop-event');
var utils = require('./lib/utils');
var bind = require('./lib/bind');
/**
* Expose `Auth0Lock` constructor
*/
module.exports = Auth0Lock;
/**
* Create `Auth0Lock` instance
* resolving `options`.
*
* @param {String} clientID
* @param {String} domain
* @param {Object} options
* - cdn
* - assetsUrl
* @return {Auth0Lock}
* @constructor
*/
function Auth0Lock (clientID, domain, options) {
if (!(this instanceof Auth0Lock)) {
return new Auth0Lock(options);
}
// validate required options
if ('string' !== typeof clientID) throw new Error('`ClientID` required as first parameter.');
if ('string' !== typeof domain) throw new Error('`domain` required as second parameter.');
// Initiate `EventEmitter`
EventEmitter.call(this);
// Instance properties and options
this.$options = _.extend({}, options);
// Save clientID and domain in $options
this.$options.clientID = clientID;
this.$options.domain = domain;
// Holds copy for all suppported strategies
this.$strategies = strategies;
// Holds auth0-js' instance
this.$auth0 = new Auth0({
clientID: this.$options.clientID,
domain: this.$options.domain
});
// use domain as assetsUrl if no assetsUrl provided
// and domain is not *.auth0.com. Fallback to S3 url
this.$options.assetsUrl = this.$options.assetsUrl || this.isAuth0Domain() ? 'https://s3.amazonaws.com/assets.auth0.com/' : 'https://' + this.$options.domain + '/';
this.$options.cdn = this.$options.cdn || this.isAuth0Domain() ? 'https://d19p4zemcycm7a.cloudfront.net/w2/' : 'https://' + this.$options.domain + '/w2/';
// Holds SSO Data for return user experience
this.$ssoData = null;
// Holds widget's DOM `$container` ref
this.$container = null;
// holds client's connections configuration
// retrieved from S3 or CDN/assetsUrl provided
this.$client = {};
this.getClientConfiguration(bind(this.setClientConfiguration, this));
}
/**
* Expose current `Auth0Lock`'s version
*/
Auth0Lock.version = require('package.version');
/**
* Inherit from `EventEmitter`
*/
Auth0Lock.prototype = ocreate(EventEmitter.prototype);
/**
* Get client configuration.
* XXX: Why not use jsonp? that woudld allow the
* global namespace definition to be optional...
*
* @param {Function} done
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype.getClientConfiguration = function (done) {
var self = this;
// Save callback to be called once
// client configuration gets loaded
if ('function' === typeof done) {
this.once('client loaded', function (client) {
done(client);
});
}
// If not loading, check for already stored
// in a previous widget instantiation
global.window.Auth0 = global.window.Auth0 || { clients: {}, script_tags: {} };
if (!global.window.Auth0.clients) {
global.window.Auth0.clients = {};
global.window.Auth0.script_tags = {};
}
var clients = global.window.Auth0.clients;
var client = clients[this.$options.clientID];
if (client) return this.emit('client loaded', client);
// check if loading state
// and then await for response
// no need to monkey-patch again
if (this.loadState) return;
this.loadState = true;
// Monkey patch Auth.setClient to load client
var setClient = global.window.Auth0.setClient || function setClient() {};
global.window.Auth0.setClient = function (client) {
setClient.apply(window.Auth0, arguments);
// If not this client, return
if (self.$options.clientID !== client.id) return;
// store the client
clients[self.$options.clientID] = client;
// notify initialized and pass the client with it
self.emit('client loaded', client);
};
var script = global.window.Auth0.script_tags[this.$options.clientID];
if (!script) {
// Load client from assets url
var script = document.createElement('script');
script.src = this.$options.assetsUrl + 'client/' + this.$options.clientID + '.js' + '?t' + (+new Date());
// Save script reference for other intances using the same clientID
global.window.Auth0.script_tags[this.$options.clientID] = script;
// Insert script in DOM head
var firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
}
// Handle load and error for client config
script.addEventListener('load', bind(this.onclientloadsuccess, this));
script.addEventListener('error', bind(this.onclientloaderror, this));
this.timeout = setTimeout(bind(this.onclientloaderror, this), 3000);
};
/**
* Handle success for script load of client's configuration
*
* @private
*/
Auth0Lock.prototype.onclientloadsuccess = function() {
// clear error timeout
clearTimeout(this.timeout);
// We should use debug and log stuff without console.log
// and only for debugging
if (console && console.log) {
console.log('Client configuration loaded');
}
}
/**
* Handle error for script load of client's configuration
*
* @private
*/
Auth0Lock.prototype.onclientloaderror = function(err) {
// clear error timeout
clearTimeout(this.timeout);
// If no options, there is no UI to actually show error
if (this.options) {
// Exhibit lock's working canvas
this.exhibit();
// XXX: Should we create an "error-mode" for such cases?
// XXX: or are we ok with this display?
this._loadingPanel(this.options);
// Turn off the loading spinner
this.query('.a0-spinner').addClass('a0-hide');
// display error
this._showError(this.options.i18n.t('networkError'));
};
// reset loadstate
this.loadState = false;
// reset script loading state
global.window.Auth0.script_tags[this.$options.clientID] = null;
if (console && console.log) {
console.log(new Error('Failed to load client configuration for ' + this.$options.clientID));
};
}
/**
* Set's the client configuration object
*
* @param {Object} client
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype.setClientConfiguration = function (client) {
this.$client = _.clone(client);
this.emit('client initialized');
return this;
};
/**
* Query for elements by `selector` within optional `context`.
* Last defaults to widget's instance `$container`.
*
* @param {String} selector
* @param {NodeElement} context
* @return {BonzoAugmented}
* @public
*/
Auth0Lock.prototype.query = function(selector, context) {
if ('string' === typeof selector) {
return $(selector, context || this.$container);
}
return $('#a0-lock', selector || this.$container);
};
/**
* Render template function with default incance
* `_locals` resolved.
*
* @param {Function} tmpl
* @param {Object} locals
* @return {String}
* @public
*/
Auth0Lock.prototype.render = function(tmpl, locals) {
var _locals = _.extend({}, this.options, locals);
return tmpl(_locals);
};
/**
* Render widget container to DOM
* XXX: consider renaming!
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype.insert = function() {
if (this.$container) return this;
var options = this.options;
var cid = options.container;
var locals = {
options: options,
alt_spinner: !has_animations() ?
(this.$options.cdn + 'img/ajax-loader.gif') :
null
};
// widget container
if (cid) {
this.$container = document.getElementById(cid);
if (!this.$container) throw new Error('Not found element with \'id\' ' + cid);
this.$container.innerHTML = this.render(template, locals);
} else {
this.$container = document.createElement('div');
bonzo(this.$container).addClass('a0-lock-container');
this.$container.innerHTML = this.render(template, locals);
document.body.appendChild(this.$container);
}
return this;
};
/**
* Exhibit Lock's working space
* before loading any other panel
*
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype.exhibit = function() {
var options = this.options;
// Create and set the header
this.header = new HeaderView(this, this.query('.a0-header').get(0), options);
// activate panel
// XXX: (?) this I don't get... why remove and add?
this.query('div.a0-panel').removeClass('a0-active');
this.query('div.a0-overlay').addClass('a0-active');
this.query('.a0-panel.a0-onestep').addClass('a0-active');
if (!options.container) {
bonzo(document.body).addClass('a0-lock-open');
} else {
this.query('.a0-active').removeClass('a0-overlay');
}
this.query('.a0-popup .a0-invalid').removeClass('a0-invalid');
this.query('.a0-overlay')
.toggleClass('a0-no-placeholder-support', !placeholderSupported);
// buttons actions
this.query('.a0-onestep a.a0-close').a0_on('click', bind(this.oncloseclick, this));
// close popup with ESC key
if (options.closable) {
this.query('').a0_on('keyup', bind(this.onescpressed, this));
}
// after pre-setting classes and dom handlers
// emit as shown
this.emit('shown');
}
/**
* Show the widget resolving `options`
* with default mode as 'signin'
*
* @param {Object} options
* @param {Function} callback
* @return {Auth0Lock}
* @public
*/
Auth0Lock.prototype.show = function(options, callback) {
var params = getShowParams(options, callback);
var opts = _.extend({ mode: 'signin' }, params.options);
return this.display(opts, params.callback);
};
/**
* Show widget on `signin` mode with
* signup and reset actions disabled
* by default so no action buttons
* are present on widget.
*
* @param {Object} options
* @param {Function} callback
* @return {Auth0Lock}
* @public
*/
Auth0Lock.prototype.showSignin = function(options, callback) {
var params = getShowParams(options, callback);
var optional = { disableSignupAction: true, disableResetAction: true };
var required = { mode: 'signin' };
// merge and force `signin` mode
var opts = _.extend(optional, params.options, required);
return this.show.call(this, opts, params.callback);
};
/**
* Show widget on `reset` mode with
* signup and reset actions disabled
* by default so no action buttons
* are present on widget.
*
* @param {Object} options
* @param {Function} callback
* @return {Auth0Lock}
* @public
*/
Auth0Lock.prototype.showSignup = function(options, callback) {
var params = getShowParams(options, callback);
var optional = { disableSignupAction: true, disableResetAction: true };
var required = { mode: 'signup' };
// merge and force `signin` mode
var opts = _.extend(optional, params.options, required);
return this.show.call(this, opts, params.callback);
};
/**
* Show widget on `reset` mode with
* signup and reset actions disabled
* by default so no action buttons
* are present on widget.
*
* @param {Object} options
* @param {Function} callback
* @return {Auth0Lock}
* @public
*/
Auth0Lock.prototype.showReset = function(options, callback) {
var params = getShowParams(options, callback);
var optional = { disableSignupAction: true, disableResetAction: true };
var required = { mode: 'reset' };
// merge and force `signin` mode
var opts = _.extend(optional, params.options, required);
return this.show.call(this, opts, params.callback);
};
/**
* Hide the widget and call `callback` when done.
*
* @param {Function} callback
* @return {Auth0Lock}
* @public
*/
Auth0Lock.prototype.hide = function (callback) {
// immediatelly hide widget
bonzo(document.body).removeClass('a0-lock-open');
// Remove widget and/or it's container
if (this.$container && this.options.container) {
// remove `#a0-lock`
this.query().remove();
} else if(this.$container) {
// remove `.a0-lock-container`
this.query().parent('.a0-lock-container').remove();
}
this.$container = null;
if ('function' === typeof callback) callback();
this.emit('hidden');
return this;
};
/**
* Proxy `auth0.js` instance `.logout()` method
*
* @param {Object} query
* @return {Auth0Lock}
* @public
*/
Auth0Lock.prototype.logout = function (query) {
this.$auth0.logout(query);
return this;
};
/**
* Display the widget in "signin" or "signup"
* or "reset" mode, resolved from display `options`.
* Optionaly set "popupCallback" to `callback` if present
*
* @param {Object} options
* @param {Function} callback
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype.display = function(options, callback) {
// pre-format options
var opts = _.extend({ popupCallback: callback }, options);
// Instantiate OptionsManager as `this.options`
this.options = new OptionsManager(this, opts);
// Start by render widget's container
this.insert();
this.options.ready(bind(onoptionsready, this));
// Initialize widget's view
// when options get loaded
function onoptionsready() {
this.initialize(bind(oninitialized, this));
}
// and right after that render mode
function oninitialized() {
// focus once ready
this.once(this.options.mode + ' ready', bind(this.focusInput, this));
// resolve view
if ('signin' === this.options.mode) {
// if user in AD ip range
if (this.$ssoData && this.$ssoData.connection) {
return this._kerberosPanel(this.options, callback);
}
// if user logged in show logged in experience
if (this.options._shouldShowLastLogin()) {
return this._loggedinPanel(this.options, callback);
}
// otherwise, just show signin
this._signinPanel(this.options, callback);
}
if ('signup' === this.options.mode) {
this._signupPanel(this.options, callback);
}
if ('reset' === this.options.mode) {
this._resetPanel(this.options, callback);
}
}
return this;
};
/**
* Initialize widget for the `display` method
* and calls `done` when ready to continue mode
* setup...
*
* @param {Function} done
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype.initialize = function(done) {
var self = this;
var options = this.options;
// Wait for Auth0.setClient() to be sure
// we have the client's configuration
// before setting up
if (_.isEmpty(this.$client)) {
var args = arguments;
return this.getClientConfiguration(function () {
self.initialize.apply(self, args);
});
}
if (options._isFreeSubscription()) {
// hide footer for non free/dev subscriptions
this.query('.a0-footer').toggleClass('a0-hide', true);
this.query('.a0-free-subscription').removeClass('a0-free-subscription');
}
// Exhibit lock's working canvas
this.exhibit();
function finish(err, ssoData) {
// XXX: auth0.getSSOData() never returns err
// see source at: https://github.com/auth0/auth0.js/blob/master/lib/index.js
self.$ssoData = ssoData;
done();
self.emit('ready');
}
// do not get SSO data on signup or reset modes
var notSigninMode = ~['reset', 'signup'].indexOf(options.mode);
if (notSigninMode) {
return finish(null, {}), this;
}
var disabledReturnUserExperience = false === options.rememberLastLogin &&
(!options._isThereAnyADConnection() || false === options.integratedWindowsLogin);
if (disabledReturnUserExperience) {
return finish(null, {}), this;
}
this._loadingPanel(options);
// get SSO data and then render
this.$auth0.getSSOData(options._isThereAnyADConnection(), finish);
return this;
};
/**
* Create and set a new SigninPanel with
* `options`, and also set widget's title
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype._signinPanel = function (options) {
var panel = SigninPanel(this, { options: options || {} });
// XXX: future Panel API placeholder
// panel.on('submit', this.setLoadingMode);
// panel.on('error', function(errors) {
// // errors are already saved in `signin` instance
// self.unsetLoadinMode();
// self.query('.a0-panel').html(signin.create());
// });
// panel.on('success', function() {
// self.hide(); // will unset loading mode
// // and destroy and detach
// // widget container from DOM
// });
this._setTitle(this.options.i18n.t('signin:title'));
this.setPanel(panel);
return this;
};
/**
* Create and set a new SignupPanel with
* `options`, and also set widget's title
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype._signupPanel = function (options) {
var panel = SignupPanel(this, { options: options || {} });
this._setTitle(this.options.i18n.t('signup:title'));
this.setPanel(panel);
return this;
};
/**
* Create and set a new ResetPanel with
* `options`, and also set widget's title
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype._resetPanel = function (options) {
var panel = ResetPanel(this, { options: options || {} });
this._setTitle(this.options.i18n.t('reset:title'));
this.setPanel(panel);
return this;
};
/**
* Create and set a new LoadingPanel with
* `options`, and also set widget's title
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype._loadingPanel = function (options) {
var panel = LoadingPanel(this, { options: options });
if (options.title) {
this._setTitle(this.options.i18n.t(options.title + ':title'));
} else {
this._setTitle(this.options.i18n.t((options.mode || 'signin') + ':title'));
}
this.setPanel(panel);
if (options.message) {
panel.query('').addClass('a0-with-message');
panel.query('.a0-spin-message span').html(options.message.replace('-', ' '));
}
return this;
};
/**
* Create and set a new LoggedinPanel with
* `options`, and also set widget's title
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype._loggedinPanel = function (options) {
var panel = LoggedinPanel(this, { options: options || {} });
this._setTitle(this.options.i18n.t('signin:title'));
this.setPanel(panel);
return this;
};
/**
* Create and set a new KerberosPanel with
* `options`, and also set widget's title
*
* @param {Object} options
* @return {Auth0Lock}
* @private
*/
Auth0Lock.prototype._kerberosPanel = function (options) {
var panel = KerberosPanel(this, { options: options || {} });
this._setTitle(this.options.i18n.t('signin:title'));
this.setPanel(panel);
return this;
};
/**
* Set `panel` to .a0-mode-container element and
* emit it's `name` as ready
*
* @param {SigninPanel|SignupPanel|...} panel
* @param {String} name
* @private
*/
Auth0Lock.prototype.setPanel = function(panel, name) {
var el = 'function' === typeof panel.render ? panel.render() : panel;
var pname = 'function' === typeof panel.render ? panel.name : (name || 'signin');
this.query('.a0-mode-container').html(el);
this.emit('%s ready'.replace('%s', pname));
};
/**
* Resolve whether instance `$options.domain` is an
* Auth0's domain or not
*
* @return {Boolean}
* @private
*/
Auth0Lock.prototype.isAuth0Domain = function () {
var domainUrl = utils.parseUrl('https://' + this.$options.domain);
return utils.endsWith(domainUrl.hostname, '.auth0.com');
};
/**
* Resolve whether ignore or not `inputs` email validation
*
* @param {NodeElement} input
* @return {Boolean}
* @private
*/
Auth0Lock.prototype._ignoreEmailValidations = function (input) {
return input.attr('type') !== 'email';
};
/**
* Set an error `message` or clean element.
*
* @param {String} message
* @private
*/
Auth0Lock.prototype._showError = function (message) {
// if no error, clean
if (!message) {
// reset errors
this.query('.a0-error').html('').addClass('a0-hide');
this.query('.a0-errors').removeClass('a0-errors');
// reset animations
return animation_shake_reset(this.$container);
}
// else, show and render error message
setTimeout(animation_shake, 0, this.$container);
this.query('.a0-success').addClass('a0-hide');
this.query('.a0-error').html(message).removeClass('a0-hide');
this.emit('_error', message);
};
/**
* Set a success `message` or clean element.
* XXX: This is mostly used on password reset,
* we should consider moving it to `ResetPanel`
*
* @param {String} message
* @private
*/
Auth0Lock.prototype._showSuccess = function (message) {
// if no message, clean success span
if (!message) return this.query('.a0-success').html('').addClass('a0-hide');
// else, show and render success message
this.query('.a0-error').addClass('a0-hide');
this.query('.a0-success').html(message).removeClass('a0-hide');
};
/**
* Set an `input`s style to focus some
* error going on, and optionaly
* append a `message`
*
* @param {NodeElement} input
* @param {String} message
* @private
*/
Auth0Lock.prototype._focusError = function(input, message) {
// remove all `_focusError` resources
if (!arguments.length) {
// reset errors
this.query('.a0-errors').removeClass('a0-errors');
this.query('.a0-error-input').removeClass('a0-error-input');
this.query('.a0-error-message').remove();
// reset animations
return animation_shake_reset(this.$container);
}
// animation
setTimeout(animation_shake, 0, this.$container);
input
.parent()
.addClass('a0-error-input');
if (!message) return;
input.parent()
.append($.create('<span class="a0-error-message">' + message + '</span>'));
};
/**
* Set widget's `title`
*
* @param {String} title
* @private
*/
Auth0Lock.prototype._setTitle = function(title) {
this.header.setTitle(title);
};
/**
* Restore widget's image
*
* @param {String} title
* @private
*/
Auth0Lock.prototype.restoreImage = function(title) {
this.header.restoreImage(title);
};
/**
* Set widget's image
*
* @param {String} title
* @private
*/
Auth0Lock.prototype.setImage = function(title) {
this.header.setImage(title);
};
/**
* Signin entry point method for resolving
* username and password connections or enterprise
*
* @param {SigninPanel|SignupPanel} panel
* @private
*/
Auth0Lock.prototype._signin = function (panel) {
var valid = true;
var message;
var emailD = panel.query('.a0-email');
var email_input = panel.query('input[name=email]');
var email = null, domain, connection;
var input_email_domain = this.options._extractEmailDomain(email_input.val().toLowerCase());
var conn_obj = this.options._findConnectionByDomain(
input_email_domain,
this.$client.strategies
);
// Gets suffix
if (!conn_obj) {
if (this.options.auth0Strategies.length > 0) {
return this._signinWithAuth0(panel);
}
if (input_email_domain === 'gmail.com') {
return this._signinSocial('google-oauth2', null, null, panel);
}
message = this.options.i18n.t('signin:strategyDomainInvalid');
message = message.replace('{domain}', input_email_domain);
this._showError(message);
this._focusError(email_input);
return;
}
domain = conn_obj.domain;
email = email_input.val();
connection = conn_obj.name;
valid &= (!domain && !emailD.addClass('a0-invalid')) || (!!domain && !!emailD.removeClass('a0-invalid'));
// XXX: We should throw something here...
// There has to be an action!
if (!valid) { return; }
if (this.options.popup && 'token' === this.options.responseType) {
return this._signinPopupNoRedirect(connection, this.options.popupCallback, undefined, panel);
}
message = this.options.i18n.t('signin:loadingMessage').replace('{connection}', connection);
this._loadingPanel({ mode: 'signin', message: message });
var loginOptions = _.extend({}, {
connection: connection,
popup: this.options.popup,
popupOptions: this.options.popupOptions
}, this.options.authParams);
this.$auth0.login(loginOptions);
};
/**
* Signin method for username and password credentials
*
* @param {SigninPanel|SignupPanel} panel
* @private
*/
Auth0Lock.prototype._signinWithAuth0 = function (panel, connection) {
var self = this;
var options = this.options;
var email_input = panel.query('input[name=email]');
var password_input = panel.query('input[name=password]');
var username = email_input.val();
var password = password_input.val();
connection = connection || options._getAuth0Connection(username);