Reputation: 65
I am trying to access Firebase database using Firebase ID token. As per Firebase base docs, "When a user or device signs in using Firebase Authentication, Firebase creates a corresponding ID token that uniquely identifies them and grants them access to several resources, such as Realtime Database.You can re-use that ID token to authenticate the Realtime Database REST API and make requests on behalf of that user."
How can I get the ID token after the user Sign-in using email and password? so I can pass it to https://<DATABASE_NAME>.firebaseio.com/users/ada/name.json?auth=<ID_TOKEN>
Upvotes: 1
Views: 1297
Reputation: 74094
You can use the C
# await task wrapper instead of the Java listener:
var tokenResult = await FirebaseAuth.GetInstance(fireApp).CurrentUser.GetTokenAsync(true);
Log.Debug(App.TAG, tokenResult.Token);
FirebaseAuth.GetInstance(fireApp)
.CurrentUser
.GetToken(true)
.AddOnCompleteListener(this, new GmsTaskCompletion((sender, e) =>
{
var task = (e as GmsTaskCompletion.GmsTaskEvent).task;
if (task.IsSuccessful)
{
var tokenResult = task.Result as GetTokenResult;
Log.Debug(App.TAG, tokenResult.Token);
}
}));
Using this IOnCompleteListener
implementation:
public class GmsTaskCompletion : Java.Lang.Object, IOnCompleteListener
{
public class GmsTaskEvent : EventArgs
{
public readonly Android.Gms.Tasks.Task task;
public GmsTaskEvent(Android.Gms.Tasks.Task task) => this.task = task;
}
readonly EventHandler handler;
public GmsTaskCompletion(EventHandler handler) => this.handler = handler;
public void OnComplete(Android.Gms.Tasks.Task task)
{
if (handler != null)
handler.Invoke(this, new GmsTaskEvent(task));
}
}
Upvotes: 1