-
Notifications
You must be signed in to change notification settings - Fork 16
D.2 Init Authentication
You can initialize the Authentication by calling Auth.init();
As soon as you initialized the firebaseAuth.ane in your project, you will be ready to signup or signin users!
NOTE: Make sure you have already initialized the Firebase core before trying any other Firebase child ANEs.
check to see if the user is currently signed in and if it is, get user's current information
if (Auth.isLoggin)
{
trace("user is logged in");
getUserInfo();
}
else
{
trace("user is NOT logged in");
}
function getUserInfo():void
{
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)
{
trace("creationTime = " + 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];
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("-------------")
}
}
After initializing the Auth with Auth.init()
, you can listen to Auth changes by watching this event: AUTH_STATE_CHANGED
Auth.listener.addEventListener(AuthEvents.AUTH_STATE_CHANGED, onAuthStateChanged);
function onAuthStateChanged(e:AuthEvents):void
{
// check if the user info is available or not
if (FirebaseUser.isAvailable)
{
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");
// 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);
}
}
Create a form that allows new users to register with your app using their email address and a password. When a user completes the form, validate the email address and password provided by the user, then pass them to the createUserWithEmailAndPassword
method:
// listen to new user creation result
Auth.listener.addEventListener(AuthEvents.CREATE_NEW_USER_RESULT, onCreateNewUserResult);
// send email and password to create a new user
Auth.createUserWithEmailAndPassword("[email protected]", "123456");
function onCreateNewUserResult(e:AuthEvents):void
{
trace("onCreateNewUserResult result=" + e.result, " msg=" + e.msg);
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);
}
}
Create a form that allows existing users to sign in using their email address and password. When a user completes the form, call Auth.signIn
// listen to SIGN_IN_RESULT event to know how the process goes
Auth.listener.addEventListener(AuthEvents.SIGN_IN_RESULT, onSignInResult);
// create a new authProvider object first
var authProvider:AuthProvider = new AuthProvider();
// decide what kind of credential this authProvider instance will hold
authProvider.setEmailPassAuthProvider("[email protected]", "123456");
// and finally feed the Auth.signIn method with the parsed credential info from the authProvider instance.
Auth.signIn(authProvider.getCredential());
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);
}
}
To sign out a user, simply call Auth.signOut();
when signout happens, AUTH_STATE_CHANGED
will be invoked again.
Enjoy building Air apps – With ♥ from MyFlashLabs Team
Introduction to Firebase ANEs collection for Adobe Air apps
Get Started with Firebase Core in AIR
- Prerequisites
- Add Firebase to your app
- Add the Firebase SDK
- Init Firebase Core
- Available ANEs
- Managing Firebase iid
Get Started with Authentication
- Add Authentication
- Init Authentication
- Manage Users
- Phone Number
- Custom Auth
- Anonymous Auth
- State in Email Actions
- Email Link Authentication
Get Started with FCM + OneSignal
- Add FCM ANE
- Init FCM ANE
- Send Your 1st Message
- Send Msg to Topics
- Understanding FCM Messages
- init OneSignal
- Add Firestore
- Init Firestore
- Add Data
- Transactions & Batches
- Delete Data
- Manage the Console
- Get Data
- Get Realtime Updates
- Simple and Compound
- Order and Limit Data
- Paginate Data
- Manage Indexes
- Secure Data
- Offline Data
- Where to Go From Here
Get Started with Realtime Database
- Add Realtime Database
- Init Realtime Database
- Structure Your Database
- Save Data
- Retrieve Data
- Enable Offline Capabilities
Get Started with Remote Config
- Add Storage ANE
- Init Storage ANE
- Upload Files to Storage
- Download Files to Air
- Use File Metadata
- Delete Files