This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MainAuth.as
912 lines (757 loc) · 30.6 KB
/
MainAuth.as
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
package
{
import com.doitflash.consts.Direction;
import com.doitflash.consts.Orientation;
import com.doitflash.mobileProject.commonCpuSrc.DeviceInfo;
import com.doitflash.starling.utils.list.List;
import com.doitflash.text.modules.MySprite;
import com.luaye.console.C;
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.InvokeEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.ui.Keyboard;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import com.myflashlab.air.extensions.firebase.core.*;
import com.myflashlab.air.extensions.firebase.auth.*;
import com.myflashlab.air.extensions.firebase.dynamicLinks.*;
import com.myflashlab.air.extensions.dependency.OverrideAir;
/**
* ...
* @author Hadi Tavakoli - 7/13/2016 4:10 PM
*/
public class MainAuth extends Sprite
{
private const BTN_WIDTH:Number = 150;
private const BTN_HEIGHT:Number = 60;
private const BTN_SPACE:Number = 2;
private var _txt:TextField;
private var _body:Sprite;
private var _list:List;
private var _numRows:int = 1;
private var email:String = "[email protected]";
public function MainAuth():void
{
Multitouch.inputMode = MultitouchInputMode.GESTURE;
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate);
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate);
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys);
stage.addEventListener(Event.RESIZE, onResize);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
C.startOnStage(this, "`");
C.commandLine = false;
C.commandLineAllowed = false;
C.x = 20;
C.width = 250;
C.height = 150;
C.strongRef = true;
C.visible = true;
C.scaleX = C.scaleY = DeviceInfo.dpiScaleMultiplier;
_txt = new TextField();
_txt.autoSize = TextFieldAutoSize.LEFT;
_txt.antiAliasType = AntiAliasType.ADVANCED;
_txt.multiline = true;
_txt.wordWrap = true;
_txt.embedFonts = false;
_txt.htmlText = "<font face='Arimo' color='#333333' size='20'><b>Firebase Auth V"+Auth.VERSION+"</font>";
_txt.scaleX = _txt.scaleY = DeviceInfo.dpiScaleMultiplier;
this.addChild(_txt);
_body = new Sprite();
this.addChild(_body);
_list = new List();
_list.holder = _body;
_list.itemsHolder = new Sprite();
_list.orientation = Orientation.VERTICAL;
_list.hDirection = Direction.LEFT_TO_RIGHT;
_list.vDirection = Direction.TOP_TO_BOTTOM;
_list.space = BTN_SPACE;
init();
onResize();
}
private function onInvoke(e:InvokeEvent):void
{
NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke);
}
private function handleActivate(e:Event):void
{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
private function handleDeactivate(e:Event):void
{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
private function handleKeys(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.BACK)
{
e.preventDefault();
NativeApplication.nativeApplication.exit();
}
}
private function onResize(e:*=null):void
{
if (_txt)
{
_txt.y = 150 * (1 / DeviceInfo.dpiScaleMultiplier);
_txt.width = stage.stageWidth * (1 / DeviceInfo.dpiScaleMultiplier);
C.x = 0;
C.y = _txt.y + _txt.height + 0;
C.width = stage.stageWidth * (1 / DeviceInfo.dpiScaleMultiplier);
C.height = 300 * (1 / DeviceInfo.dpiScaleMultiplier);
}
if (_list)
{
_numRows = Math.floor(stage.stageWidth / (BTN_WIDTH * DeviceInfo.dpiScaleMultiplier + BTN_SPACE));
_list.row = _numRows;
_list.itemArrange();
}
if (_body)
{
_body.y = stage.stageHeight - _body.height;
}
}
private function init():void
{
// Remove OverrideAir debugger in production builds
OverrideAir.enableDebugger(function ($ane:String, $class:String, $msg:String):void
{
trace($ane+" ("+$class+") "+$msg);
});
// pass "true" for the init method so the ANE can prepare itself for accepting dynamic links.
var isConfigFound:Boolean = Firebase.init(true);
if (isConfigFound)
{
var config:FirebaseConfig = Firebase.getConfig();
C.log("default_web_client_id = " + config.default_web_client_id);
C.log("firebase_database_url = " + config.firebase_database_url);
C.log("gcm_defaultSenderId = " + config.gcm_defaultSenderId);
C.log("google_api_key = " + config.google_api_key);
C.log("google_app_id = " + config.google_app_id);
C.log("google_crash_reporting_api_key = " + config.google_crash_reporting_api_key);
C.log("google_storage_bucket = " + config.google_storage_bucket);
C.log("project_id = " + config.project_id);
trace("default_web_client_id = " + config.default_web_client_id);
trace("firebase_database_url = " + config.firebase_database_url);
trace("gcm_defaultSenderId = " + config.gcm_defaultSenderId);
trace("google_api_key = " + config.google_api_key);
trace("google_app_id = " + config.google_app_id);
trace("google_crash_reporting_api_key = " + config.google_crash_reporting_api_key);
trace("google_storage_bucket = " + config.google_storage_bucket);
trace("project_id = " + config.project_id);
initFirebaseAuth();
initDynamicLinks();
}
else
{
C.log("Config file is not found!");
trace("Config file is not found!");
}
}
private function initFirebaseAuth():void
{
Auth.init();
Auth.listener.addEventListener(AuthEvents.AUTH_STATE_CHANGED, onAuthStateChanged);
Auth.listener.addEventListener(AuthEvents.ID_TOKEN_CHANGED, onIdTokenChanged);
Auth.listener.addEventListener(AuthEvents.CREATE_NEW_USER_RESULT, onCreateNewUserResult);
Auth.listener.addEventListener(AuthEvents.SIGN_IN_RESULT, onSignInResult);
Auth.listener.addEventListener(AuthEvents.SEND_PASSWORD_RESET_EMAIL_RESULT, onPassResetResult);
Auth.listener.addEventListener(AuthEvents.SEND_SIGNIN_LINK_RESULT, onSignInLinkResult);
// -----------------------------------------------------------
var btn01:MySprite = createBtn("isLoggin?");
btn01.addEventListener(MouseEvent.CLICK, isLoggin);
_list.add(btn01);
function isLoggin(e:MouseEvent):void
{
if (Auth.isLoggin)
{
C.log("user is logged in");
trace("user is logged in");
getUserInfo();
}
else
{
C.log("user is NOT logged in");
trace("user is NOT logged in");
}
}
// -----------------------------------------------------------
var btn2:MySprite = createBtn("create User With Email & Pass");
btn2.addEventListener(MouseEvent.CLICK, register);
_list.add(btn2);
function register(e:MouseEvent):void
{
if (Auth.isLoggin)
{
C.log("user is already signed in.")
trace("user is already signed in.")
}
else
{
Auth.createUserWithEmailAndPassword(email, "123456");
}
}
// -----------------------------------------------------------
var btn3:MySprite = createBtn("signOut");
btn3.addEventListener(MouseEvent.CLICK, signOut);
_list.add(btn3);
function signOut(e:MouseEvent):void
{
Auth.signOut();
}
// -----------------------------------------------------------
var btn4:MySprite = createBtn("signIn");
btn4.addEventListener(MouseEvent.CLICK, signIn);
_list.add(btn4);
function signIn(e:MouseEvent):void
{
// create a new authProvider object first
var authProvider:AuthProvider = new AuthProvider();
// decide what kind of credential this authProvider instance will hold
authProvider.setEmailPassAuthProvider(email, "123456");
// and finally feed the Auth.signIn method with the parsed credential info from the authProvider instance.
Auth.signIn(authProvider.getCredential());
}
// -----------------------------------------------------------
var btn04:MySprite = createBtn("signIn anonymously");
btn04.addEventListener(MouseEvent.CLICK, signInAnonymously);
_list.add(btn04);
function signInAnonymously(e:MouseEvent):void
{
Auth.signIn(null);
}
// -----------------------------------------------------------
var btn5:MySprite = createBtn("send pass reset");
btn5.addEventListener(MouseEvent.CLICK, sendPassReset);
_list.add(btn5);
function sendPassReset(e:MouseEvent):void
{
/*
To use ActionCodeSettings, your app must have Firebase Dynamic Links enabled and ready:
https://github.com/myflashlab/Firebase-ANE/wiki/I.1-Add-Dynamic-links
// make sure you have whitelisted your domain in your firebase console/Authentication section, before trying this.
var settings:ActionCodeSettings = new ActionCodeSettings("https://example.com/");
settings.handleCodeInApp = true;*/
Auth.sendPasswordResetEmail(email/*, settings*/);
}
// -----------------------------------------------------------
var btn005:MySprite = createBtn("send signin link to email");
btn005.addEventListener(MouseEvent.CLICK, sendSigninLink);
_list.add(btn005);
function sendSigninLink(e:MouseEvent):void
{
/*
To use ActionCodeSettings, your app must have Firebase Dynamic Links enabled and ready:
https://github.com/myflashlab/Firebase-ANE/wiki/I.1-Add-Dynamic-links
*/
// make sure you have whitelisted your domain in your firebase console/Authentication section, before trying this.
var settings:ActionCodeSettings = new ActionCodeSettings("https://www.myflashlabs.com/");
settings.dynamicLinkDomain = "myflashlabs.page.link";
settings.handleCodeInApp = true; // The sign-in operation has to always be completed in the app.
settings.iOSBundleID = NativeApplication.nativeApplication.applicationID;
settings.androidPackageName = "air." + NativeApplication.nativeApplication.applicationID;
settings.androidInstallIfNotAvailable = false;
settings.androidMinVersion = "1";
Auth.sendSignInLinkToEmail(email, settings);
}
// -----------------------------------------------------------
var btn6:MySprite = createBtn("reload user info");
btn6.addEventListener(MouseEvent.CLICK, reloadUserInfo);
_list.add(btn6);
function reloadUserInfo(e:MouseEvent):void
{
if (FirebaseUser.isAvailable) {
FirebaseUser.reload()
} else {
C.log("FirebaseUser is not available");
trace("FirebaseUser is not available");
}
}
// -----------------------------------------------------------
var btn06:MySprite = createBtn("send verify email");
btn06.addEventListener(MouseEvent.CLICK, verifyEmail);
_list.add(btn06);
function verifyEmail(e:MouseEvent):void
{
if (FirebaseUser.isEmailVerified)
{
C.log("email is already verified.");
trace("email is already verified.");
return;
}
/*
To use ActionCodeSettings, your app must have Firebase Dynamic Links enabled and ready:
https://github.com/myflashlab/Firebase-ANE/wiki/I.1-Add-Dynamic-links
// make sure you have whitelisted your domain in your firebase console/Authentication section, before trying this.
var settings:ActionCodeSettings = new ActionCodeSettings("https://example.com/");
settings.handleCodeInApp = true;
*/
FirebaseUser.sendEmailVerification(/*settings*/);
}
// -----------------------------------------------------------
var btn7:MySprite = createBtn("update display name");
btn7.addEventListener(MouseEvent.CLICK, updateDisplayName);
_list.add(btn7);
function updateDisplayName(e:MouseEvent):void
{
if (FirebaseUser.isAvailable) {
FirebaseUser.updateProfile("Hadi")
} else {
C.log("FirebaseUser is not available")
trace("FirebaseUser is not available")
}
}
// -----------------------------------------------------------
var btn8:MySprite = createBtn("update email");
btn8.addEventListener(MouseEvent.CLICK, updateEmail);
_list.add(btn8);
function updateEmail(e:MouseEvent):void
{
if (FirebaseUser.isAvailable) {
FirebaseUser.updateEmail(email)
} else {
C.log("FirebaseUser is not available")
trace("FirebaseUser is not available")
}
}
// -----------------------------------------------------------
var btn9:MySprite = createBtn("get user token");
btn9.addEventListener(MouseEvent.CLICK, getIdToken);
_list.add(btn9);
function getIdToken(e:MouseEvent):void
{
if (FirebaseUser.isAvailable) {
FirebaseUser.getIdToken(false)
} else {
C.log("FirebaseUser is not available")
trace("FirebaseUser is not available")
}
}
// -----------------------------------------------------------
var btn10:MySprite = createBtn("verifyPhoneNumber");
btn10.addEventListener(MouseEvent.CLICK, verifyPhoneNumber);
_list.add(btn10);
function verifyPhoneNumber(e:MouseEvent):void
{
Auth.listener.addEventListener(AuthEvents.PHONE_VERIFICATION_RESULT, onPhoneVerificationResult);
Auth.listener.addEventListener(AuthEvents.PHONE_CODE_SENT, onPhoneCodeSent);
Auth.listener.addEventListener(AuthEvents.PHONE_AUTO_RETRIEVAL_TIME_OUT, onPhoneTimeout);
// phone number format: +(country code)(mobile number with no initial zero)
Auth.verifyPhoneNumber("+11222222222");
}
/*
Fake Testing Number: +995 555 55 55 55, SMS CODE: 555555
*/
// -----------------------------------------------------------
/*
var btn11:MySprite = createBtn("signin with phone");
btn11.addEventListener(MouseEvent.CLICK, signinWithPhone);
_list.add(btn11);
function signinWithPhone(e:MouseEvent):void
{
// create a new authProvider object first
var authProvider:AuthProvider = new AuthProvider();
// decide what kind of credential this authProvider instance will hold
authProvider.setPhoneAuthProvider("verificationId-received-from-AuthEvents.PHONE_CODE_SENT", "SMS-code-found-in-the-sms");
// and finally feed Auth.signIn method with the parsed credential info from the authProvider instance.
Auth.signIn(authProvider.getCredential());
}
*/
// -----------------------------------------------------------
var btn12:MySprite = createBtn("fetch SignIn Methods");
btn12.addEventListener(MouseEvent.CLICK, fetchSignInMethods);
_list.add(btn12);
function fetchSignInMethods(e:MouseEvent):void
{
Auth.fetchSignInMethodsForEmail(email, function ($methods:Array, $error:Error):void
{
if($error)
{
trace("fetchSignInMethods: " + $error.message);
}
else
{
trace("fetchSignInMethods: " + $methods);
}
});
}
}
private function initDynamicLinks():void
{
// DynamicLinks class must be initialized right after Firebase.init(true); and as soon as possible.
DynamicLinks.init();
// You should immediately listen for possible DynamicLinksEvents.INVOKE event
DynamicLinks.listener.addEventListener(DynamicLinksEvents.INVOKE, onDynamicLinksInvoke);
}
private function onDynamicLinksInvoke(e:DynamicLinksEvents):void
{
if(Auth.isSignInWithEmailLink(e.link))
{
// listen to SIGN_IN_RESULT like how you did for other signin methods
Auth.listener.addEventListener(AuthEvents.SIGN_IN_RESULT, onSignInResult);
Auth.signInWithEmailLink(email, e.link);
}
}
private function onPhoneVerificationResult(e:AuthEvents):void
{
// You don't need to do anything when the verification is successful
// On a successful verification, the ANE will try to sign-in the user automatically.
C.log("PHONE_VERIFICATION_RESULT, smsCode = " + e.smsCode);
C.log("PHONE_VERIFICATION_RESULT, msg = " + e.msg);
trace("PHONE_VERIFICATION_RESULT, smsCode = " + e.smsCode);
trace("PHONE_VERIFICATION_RESULT, msg = " + e.msg);
}
private function onPhoneCodeSent(e:AuthEvents):void
{
// On most Android devices, the ANE reads the sms automatically so you don't have to
// pass the verificationId + the user input (the sms code) to the signIn method.
// But on iOS, and sometimes on Android, you MUST save the verificationId and later
// pass it to Auth.signIn(authProvider.getCredential());
C.log("PHONE_CODE_SENT, verificationId = " + e.verificationId);
trace("PHONE_CODE_SENT, verificationId = " + e.verificationId);
}
private function onPhoneTimeout(e:AuthEvents):void
{
C.log("PHONE_AUTO_RETRIEVAL_TIME_OUT, verificationId = " + e.verificationId);
trace("PHONE_AUTO_RETRIEVAL_TIME_OUT, verificationId = " + e.verificationId);
}
private function getUserInfo():void
{
C.log("displayName = " + FirebaseUser.displayName);
C.log("email = " + FirebaseUser.email);
C.log("photoUrl = " + FirebaseUser.photoUrl);
C.log("phoneNumber = " + FirebaseUser.phoneNumber);
C.log("providerId = " + FirebaseUser.providerId);
C.log("userId = " + FirebaseUser.userId);
C.log("isEmailVerified = " + FirebaseUser.isEmailVerified);
trace("displayName = " + FirebaseUser.displayName);
trace("email = " + FirebaseUser.email);
trace("photoUrl = " + FirebaseUser.photoUrl);
trace("phoneNumber = " + FirebaseUser.phoneNumber);
trace("providerId = " + FirebaseUser.providerId);
trace("userId = " + FirebaseUser.userId);
trace("isEmailVerified = " + FirebaseUser.isEmailVerified);
if(FirebaseUser.metadata)
{
C.log("creatinTime = " + new Date(FirebaseUser.metadata.creationTime).toLocaleString());
C.log("lastSignInTime = " + new Date(FirebaseUser.metadata.lastSignInTime).toLocaleString());
trace("creatinTime = " + new Date(FirebaseUser.metadata.creationTime).toLocaleString());
trace("lastSignInTime = " + new Date(FirebaseUser.metadata.lastSignInTime).toLocaleString());
}
var userInfoOnProviders:UserInfo;
for (var i:int = 0; i < FirebaseUser.providersData.length; i++)
{
userInfoOnProviders = FirebaseUser.providersData[i];
C.log("-------------")
C.log("providerId = " + userInfoOnProviders.providerId);
C.log("displayName = " + userInfoOnProviders.displayName);
C.log("email = " + userInfoOnProviders.email);
C.log("photoUrl = " + userInfoOnProviders.photoUrl);
C.log("phoneNumber = " + userInfoOnProviders.phoneNumber);
C.log("userId = " + userInfoOnProviders.userId);
C.log("-------------")
trace("-------------")
trace("providerId = " + userInfoOnProviders.providerId);
trace("displayName = " + userInfoOnProviders.displayName);
trace("email = " + userInfoOnProviders.email);
trace("photoUrl = " + userInfoOnProviders.photoUrl);
trace("phoneNumber = " + userInfoOnProviders.phoneNumber);
trace("userId = " + userInfoOnProviders.userId);
trace("-------------")
}
}
private function onCreateNewUserResult(e:AuthEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("new user created successfully");
if(e.additionalUserInfo)
{
trace("-------------- additionalUserInfo ------------");
trace("providerId: " + e.additionalUserInfo.providerId);
trace("username: " + e.additionalUserInfo.username);
trace("isNewUser: " + e.additionalUserInfo.isNewUser);
trace("profile: " + JSON.stringify(e.additionalUserInfo.profile));
trace("----------------------------------------------");
}
/*
When a new user is created, it will automatically sign in. That means the
AUTH_STATE_CHANGED event will be dispatched which you can use to read user's
information.
*/
}
else
{
trace("onCreateNewUserResult: " + e.msg);
}
}
private function onSignInResult(e:AuthEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("signed in successfully");
if(e.additionalUserInfo)
{
trace("-------------- additionalUserInfo ------------");
trace("providerId: " + e.additionalUserInfo.providerId);
trace("username: " + e.additionalUserInfo.username);
trace("isNewUser: " + e.additionalUserInfo.isNewUser);
trace("profile: " + JSON.stringify(e.additionalUserInfo.profile));
trace("----------------------------------------------");
}
/*
When sign in is successful, the AUTH_STATE_CHANGED event will be dispatched which
you can use to read user's information.
*/
}
else
{
trace("onSignInResult: " + e.msg);
}
}
private function onSignInLinkResult(e:AuthEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onSignInLinkResult email sent");
}
else
{
trace("onSignInLinkResult: " + e.msg);
}
}
private function onPassResetResult(e:AuthEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onPassResetResult successfully");
}
else
{
trace("onPassResetResult: " + e.msg);
}
}
private function onIdTokenChanged(e:AuthEvents):void
{
trace("onIdTokenChanged, get id token from FirebaseUser.getIdToken(false)");
}
private function onAuthStateChanged(e:AuthEvents):void
{
// check if the user info is available or not
if (FirebaseUser.isAvailable)
{
C.log("user is logged in");
trace("user is logged in");
getUserInfo();
// when you are logged in, it's a good time to add user listeners
FirebaseUser.listener.addEventListener(FirebaseUserEvents.RELOAD_USER_INFO, onReloadUserInfo);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.REAUTHENTICATE_RESULT, onReauthenticate);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.GET_USER_TOKEN, onGetUserToken);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.UNLINK_RESULT, onUnlink);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.LINK_WITH_RESULT, onLink);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.PROFILE_UPDATE_RESULT, onProfileUpdate);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.PASSWORD_UPDATE_RESULT, onPassUpdate);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.EMAIL_UPDATE_RESULT, onEmailUpdate);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.DELETE_USER_RESULT, onDeleteUser);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.SEND_EMAIL_VERIFICATION_RESULT, onSendEmailVerification);
FirebaseUser.listener.addEventListener(FirebaseUserEvents.PHONE_UPDATE_RESULT, onPhoneUpdate);
}
else
{
C.log("user is NOT logged in");
trace("user is NOT logged in");
// When logged out, it's a good idea to remove the user listeners
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.RELOAD_USER_INFO, onReloadUserInfo);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.REAUTHENTICATE_RESULT, onReauthenticate);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.GET_USER_TOKEN, onGetUserToken);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.UNLINK_RESULT, onUnlink);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.LINK_WITH_RESULT, onLink);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.PROFILE_UPDATE_RESULT, onProfileUpdate);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.PASSWORD_UPDATE_RESULT, onPassUpdate);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.EMAIL_UPDATE_RESULT, onEmailUpdate);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.DELETE_USER_RESULT, onDeleteUser);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.SEND_EMAIL_VERIFICATION_RESULT, onSendEmailVerification);
FirebaseUser.listener.removeEventListener(FirebaseUserEvents.PHONE_UPDATE_RESULT, onPhoneUpdate);
}
}
private function onReloadUserInfo(e:FirebaseUserEvents):void
{
C.log("onReloadUserInfo result=" + e.result, " msg=" + e.msg);
trace("onReloadUserInfo result=" + e.result, " msg=" + e.msg);
}
private function onReauthenticate(e:FirebaseUserEvents):void
{
C.log("onReauthenticate result=" + e.result, " msg=" + e.msg);
trace("onReauthenticate result=" + e.result, " msg=" + e.msg);
}
private function onGetUserToken(e:FirebaseUserEvents):void
{
// C.log("onGetUserToken result=" + e.result, " token=" + e.token);
if(e.result == Auth.RESULT_SUCCESS)
{
C.log("onGetUserToken result == Auth.RESULT_SUCCESS");
C.log("e.tokenResult.token: " + e.tokenResult.token);
C.log("e.tokenResult.claims: " + JSON.stringify(e.tokenResult.claims));
C.log("e.tokenResult.authTimestamp: " + new Date(e.tokenResult.authTimestamp).toLocaleString());
C.log("e.tokenResult.expirationTimestamp: " + new Date(e.tokenResult.expirationTimestamp).toLocaleString());
C.log("e.tokenResult.issuedAtTimestamp: " + new Date(e.tokenResult.issuedAtTimestamp).toLocaleString());
C.log("e.tokenResult.signInProvider: " + e.tokenResult.signInProvider);
trace("onGetUserToken result == Auth.RESULT_SUCCESS");
trace("e.tokenResult.token: " + e.tokenResult.token);
trace("e.tokenResult.claims: " + JSON.stringify(e.tokenResult.claims));
trace("e.tokenResult.authTimestamp: " + new Date(e.tokenResult.authTimestamp).toLocaleString());
trace("e.tokenResult.expirationTimestamp: " + new Date(e.tokenResult.expirationTimestamp).toLocaleString());
trace("e.tokenResult.issuedAtTimestamp: " + new Date(e.tokenResult.issuedAtTimestamp).toLocaleString());
trace("e.tokenResult.signInProvider: " + e.tokenResult.signInProvider);
}
else
{
C.log("onGetUserToken result != Auth.RESULT_SUCCESS");
trace("onGetUserToken result != Auth.RESULT_SUCCESS");
}
}
private function onUnlink(e:FirebaseUserEvents):void
{
C.log("onUnlink result=" + e.result, " msg=" + e.msg);
trace("onUnlink result=" + e.result, " msg=" + e.msg);
}
private function onLink(e:FirebaseUserEvents):void
{
C.log("onLink result=" + e.result, " msg=" + e.msg);
trace("onLink result=" + e.result, " msg=" + e.msg);
if(e.additionalUserInfo)
{
trace("-------------- additionalUserInfo ------------");
trace("providerId: " + e.additionalUserInfo.providerId);
trace("username: " + e.additionalUserInfo.username);
trace("isNewUser: " + e.additionalUserInfo.isNewUser);
trace("profile: " + JSON.stringify(e.additionalUserInfo.profile));
trace("----------------------------------------------");
}
}
private function onProfileUpdate(e:FirebaseUserEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onProfileUpdate successful")
}
else
{
trace("onProfileUpdate: " + e.msg);
}
}
private function onPassUpdate(e:FirebaseUserEvents):void
{
C.log("onPassUpdate result=" + e.result, " msg=" + e.msg);
trace("onPassUpdate result=" + e.result, " msg=" + e.msg);
}
private function onEmailUpdate(e:FirebaseUserEvents):void
{
// when this happens, you have to call FirebaseUser.reauthenticate to resolve it. and then try your operation again., you have to call FirebaseUser.reauthenticate to resolve it. and then try your operation again.
if (e.result == Auth.RESULT_AUTH_RECENT_LOGIN_REQUIRED)
{
// create a new authProvider object first
var authProvider:AuthProvider = new AuthProvider();
// decide what kind of credential this authProvider instance will hold
authProvider.setEmailPassAuthProvider(email, "123456");
// and finally feed FirebaseUser.reauthenticate with the parsed credential info from the authProvider instance.
FirebaseUser.reauthenticate(authProvider.getCredential());
}
else if(e.result == Auth.RESULT_SUCCESS)
{
trace("onEmailUpdate successful");
}
else
{
trace("onEmailUpdate: " + e.msg);
}
}
private function onDeleteUser(e:FirebaseUserEvents):void
{
// when this happens, you have to call FirebaseUser.reauthenticate to resolve it. and then try your operation again.
if (e.result == Auth.RESULT_AUTH_RECENT_LOGIN_REQUIRED)
{
// create a new authProvider object first
var authProvider:AuthProvider = new AuthProvider();
// decide what kind of credential this authProvider instance will hold
authProvider.setEmailPassAuthProvider(email, "123456");
// and finally feed FirebaseUser.reauthenticate with the parsed credential info from the authProvider instance.
FirebaseUser.reauthenticate(authProvider.getCredential());
}
else if(e.result == Auth.RESULT_SUCCESS)
{
trace("onDeleteUser successful");
}
else
{
trace("onDeleteUser: " + e.msg);
}
}
private function onSendEmailVerification(e:FirebaseUserEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onSendEmailVerification successful");
}
else
{
trace("onSendEmailVerification: " + e.msg);
}
}
private function onPhoneUpdate(e:FirebaseUserEvents):void
{
C.log("onPhoneUpdate result=" + e.result, " msg=" + e.msg);
trace("onPhoneUpdate result=" + e.result, " msg=" + e.msg);
}
private function createBtn($str:String):MySprite
{
var sp:MySprite = new MySprite();
sp.addEventListener(MouseEvent.MOUSE_OVER, onOver);
sp.addEventListener(MouseEvent.MOUSE_OUT, onOut);
sp.addEventListener(MouseEvent.CLICK, onOut);
sp.bgAlpha = 1;
sp.bgColor = 0xDFE4FF;
sp.drawBg();
sp.width = BTN_WIDTH * DeviceInfo.dpiScaleMultiplier;
sp.height = BTN_HEIGHT * DeviceInfo.dpiScaleMultiplier;
function onOver(e:MouseEvent):void
{
sp.bgAlpha = 1;
sp.bgColor = 0xFFDB48;
sp.drawBg();
}
function onOut(e:MouseEvent):void
{
sp.bgAlpha = 1;
sp.bgColor = 0xDFE4FF;
sp.drawBg();
}
var format:TextFormat = new TextFormat("Arimo", 16, 0x666666, null, null, null, null, null, TextFormatAlign.CENTER);
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.antiAliasType = AntiAliasType.ADVANCED;
txt.mouseEnabled = false;
txt.multiline = true;
txt.wordWrap = true;
txt.scaleX = txt.scaleY = DeviceInfo.dpiScaleMultiplier;
txt.width = sp.width * (1 / DeviceInfo.dpiScaleMultiplier);
txt.defaultTextFormat = format;
txt.text = $str;
txt.y = sp.height - txt.height >> 1;
sp.addChild(txt);
return sp;
}
}
}