-
Notifications
You must be signed in to change notification settings - Fork 16
D.3 Manage Users
Before trying to access any user information, you must first check if FirebaseUser.isAvailable
returns true. While remembering that, you can extract all user information like below:
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("-------------")
}
To make sure you have the latest user information, you must listen to AuthEvents.AUTH_STATE_CHANGED to know about auth changes. Moreover, there are times when you need to use FirebaseUser.reload();
to get the latest information. when the reload method is called, the following two events will be dispatched: FirebaseUserEvents.RELOAD_USER_INFO
and AuthEvents.AUTH_STATE_CHANGED
.
You can update a user's basic profile information—the user's display name and profile photo URL—with the FirebaseUser.updateProfile
method. For example:
FirebaseUser.listener.addEventListener(FirebaseUserEvents.PROFILE_UPDATE_RESULT, onProfileUpdate);
if (FirebaseUser.isAvailable) FirebaseUser.updateProfile("Jane Q. User", "https://example.com/jane-q-user/profile.jpg");
function onProfileUpdate(e:FirebaseUserEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onProfileUpdate successful")
}
else
{
trace("onProfileUpdate: " + e.msg);
}
}
You can set a user's email address with the updateEmail method. For example:
FirebaseUser.listener.addEventListener(FirebaseUserEvents.EMAIL_UPDATE_RESULT, onEmailUpdate);
if (FirebaseUser.isAvailable) FirebaseUser.updateEmail("[email protected]");
function onEmailUpdate(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 protected]", "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);
}
}
You can send an address verification email to a user with the sendEmailVerification method. For example:
if (!FirebaseUser.isEmailVerified)
{
FirebaseUser.listener.addEventListener(FirebaseUserEvents.SEND_EMAIL_VERIFICATION_RESULT, onSendEmailVerification);
FirebaseUser.sendEmailVerification();
}
function onSendEmailVerification(e:FirebaseUserEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onSendEmailVerification successful");
}
else
{
trace("onSendEmailVerification: " + e.msg);
}
}
You can customize the email template that is used in Authentication section of the Firebase console, on the Email Templates page. See Email Templates in Firebase Help Center.
It is also possible to pass state via a continue URL to redirect back to the app when sending a verification email. Notice that if you are going to use ActionCodeSettings, you must have added the Firebase DynamicLinks ANE to your project first.
// 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);
Additionally you can localize the verification email by updating the language code on the Auth instance before sending the email. For example:
Auth.languageCode = "fr";
// To apply the default app language instead of explicitly setting it.
Auth.useAppLanguage();
You can set a user's password with the updatePassword
method. For example:
FirebaseUser.listener.addEventListener(FirebaseUserEvents.PASSWORD_UPDATE_RESULT, onPasswordUpdate);
if (FirebaseUser.isAvailable) FirebaseUser.updatePassword("newPass");
function onPasswordUpdate(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 protected]", "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("onPasswordUpdate successful");
}
else
{
trace("onPasswordUpdate: " + e.msg);
}
}
You can send a password reset email to a user with the Auth.sendPasswordResetEmail
method. For example:
Auth.listener.addEventListener(AuthEvents.SEND_PASSWORD_RESET_EMAIL_RESULT, onPassResetResult);
Auth.sendPasswordResetEmail("[email protected]");
function onPassResetResult(e:AuthEvents):void
{
if(e.result == Auth.RESULT_SUCCESS)
{
trace("onPassResetResult successfully");
}
else
{
trace("onPassResetResult: " + e.msg);
}
}
You can customize the email template that is used in Authentication section of the Firebase console, on the Email Templates page. See Email Templates in Firebase Help Center.
It is also possible to pass state via a continue URL to redirect back to the app when sending a password reset email. Notice that if you are going to use ActionCodeSettings, you must have added the Firebase DynamicLinks ANE to your project first.
// 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 protected]", settings);
Additionally you can localize the verification email by updating the language code on the Auth instance before sending the email. For example:
Auth.languageCode = "fr";
// To apply the default app language instead of explicitly setting it.
Auth.useAppLanguage();
You can also send password reset emails from the Firebase console.
You can delete a user account with the deleteUser
method. For example:
FirebaseUser.listener.addEventListener(FirebaseUserEvents.DELETE_USER_RESULT, onDeleteUser);
FirebaseUser.deleteUser();
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 protected]", "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);
}
}
You can also delete users from the Authentication section of the Firebase console, on the Users page.
Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate. For example:
// Get auth credentials from the user for re-authentication. The example below shows
// email and password credentials but there are multiple possible providers,
// such as GoogleAuthProvider or FacebookAuthProvider.
FirebaseUser.listener.addEventListener(FirebaseUserEvents.REAUTHENTICATE_RESULT, onReauthenticate);
// 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());
function onReauthenticate(e:FirebaseUserEvents):void
{
trace("onReauthenticate result=" + e.result, " msg=" + e.msg);
}
Read Here: https://firebase.google.com/docs/auth/ios/manage-users#import_user_accounts
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