Created
April 8, 2020 15:32
-
-
Save zhenyuan0502/653ddaca0a1724c3e4aa2ecd8899b793 to your computer and use it in GitHub Desktop.
Account Manager for using Firebase with FirebaseAuthentication.net library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountManager | |
{ | |
private static AccountManager mInstance; | |
public static FirebaseAuthProvider mAuthProvider = new FirebaseAuthProvider(new FirebaseConfig("firebase_api_key")); | |
public FirebaseAuthLink mAuthLink; | |
public static bool IsSignOut = false; | |
public static AccountManager Instance() | |
{ | |
if (mInstance == null) | |
mInstance = new AccountManager(); | |
return mInstance; | |
} | |
public async Task<bool> Refreshable() | |
{ | |
try | |
{ | |
var auth = new FirebaseAuth(); | |
auth.FirebaseToken = Properties.Settings.Default.token_txt; | |
auth.RefreshToken = Properties.Settings.Default.refresh_token_txt; | |
mAuthLink = await mAuthProvider.RefreshAuthAsync(auth); | |
mAuthLink = await mAuthLink.GetFreshAuthAsync(); | |
mAuthLink.FirebaseAuthRefreshed += AuthLink_FirebaseAuthRefreshedAsync; | |
await mAuthLink.RefreshUserDetails(); | |
if (string.IsNullOrEmpty(mAuthLink.FirebaseToken)) | |
return false; | |
if (mAuthLink.User == null) | |
return false; | |
IsSignOut = false; | |
return true; | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.ToString()); | |
return false; | |
} | |
} | |
public async Task<bool> SignIn(string email, string password) | |
{ | |
try | |
{ | |
mAuthLink = await mAuthProvider.SignInWithEmailAndPasswordAsync(email, password); | |
var firebase = new FirebaseClient( | |
"firebase_site", | |
new FirebaseOptions | |
{ | |
AuthTokenAsyncFactory = () => Task.FromResult(mAuthLink.FirebaseToken) | |
}); | |
Properties.Settings.Default.email_txt = mAuthLink.User.Email; | |
Properties.Settings.Default.token_txt = mAuthLink.FirebaseToken; | |
Properties.Settings.Default.refresh_token_txt = mAuthLink.RefreshToken; | |
Properties.Settings.Default.Save(); | |
IsSignOut = false; | |
// Idk why? but it must be executed | |
await mAuthLink.RefreshUserDetails(); | |
mAuthLink.FirebaseAuthRefreshed += AuthLink_FirebaseAuthRefreshedAsync; | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
public bool SignOut() | |
{ | |
try | |
{ | |
Properties.Settings.Default.email_txt = mAuthLink.User.Email; | |
Properties.Settings.Default.token_txt = ""; | |
Properties.Settings.Default.refresh_token_txt = ""; | |
Properties.Settings.Default.Save(); | |
IsSignOut = true; | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
private async void AuthLink_FirebaseAuthRefreshedAsync(object sender, FirebaseAuthEventArgs e) | |
{ | |
Properties.Settings.Default.token_txt = e.FirebaseAuth.FirebaseToken; | |
Properties.Settings.Default.refresh_token_txt = e.FirebaseAuth.RefreshToken; | |
Properties.Settings.Default.Save(); | |
await mAuthLink.RefreshUserDetails(); | |
IsSignOut = !mAuthLink.IsExpired(); | |
} | |
public async Task<bool> SignUp(string email, string password) | |
{ | |
try | |
{ | |
mAuthLink = await mAuthProvider.CreateUserWithEmailAndPasswordAsync(email, password); | |
var firebase = new FirebaseClient( | |
"firebase_site", | |
new FirebaseOptions | |
{ | |
AuthTokenAsyncFactory = () => Task.FromResult(mAuthLink.FirebaseToken) | |
}); | |
await mAuthProvider.SendEmailVerificationAsync(mAuthLink.FirebaseToken); | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment