Hello
I am using firebase, to make a note-taking app.
I am using this documentation
https://firebase.google.com/docs/reference/rest/auth
and this is my auth
using (HttpClient client = new HttpClient()) {
var bodyJson = JsonConvert.SerializeObject(new {
email = user.Email,
password = user.Password,
returnSecureToken = true });
var data = new StringContent(bodyJson, Encoding.UTF8, "application/json");
var response = await client.PostAsync
($"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key={FirebaseKey}", data);
if (response.IsSuccessStatusCode) {
string resutJson = await response.Content.ReadAsStringAsync();
var res = JsonConvert.DeserializeObject<AuthResult>(resutJson);
App.UserId = res.localId;
Console.WriteLine($"**************************Register : {res.email} with token {res.localId}");
return true;
} else {
string errorJson = await response.Content.ReadAsStringAsync();
var res = JsonConvert.DeserializeObject<AuthError>(errorJson);
DispayError(res);
return false;
}
}
}
public static async Task<bool> LoginUser(User user) {
using (HttpClient client = new HttpClient()) {
var bodyJson = JsonConvert.SerializeObject(new {
email = user.Email,
password = user.Password,
returnSecureToken = true
});
var data = new StringContent(bodyJson, Encoding.UTF8, "application/json");
var response = await client.PostAsync
($"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FirebaseKey}", data);
if (response.IsSuccessStatusCode) {
string resutJson = await response.Content.ReadAsStringAsync();
var res = JsonConvert.DeserializeObject<AuthResult>(resutJson);
App.UserId = res.localId;
Console.WriteLine($"**************************Login : {res.email} with token {res.localId}");
return true;
} else {
string errorJson = await response.Content.ReadAsStringAsync();
var res = JsonConvert.DeserializeObject<AuthError>(errorJson);
DispayError(res);
return false;
}
}
}
private static void DispayError(AuthError res) {
Application.Current.MainPage.DisplayAlert("Error", res.error.message, "OK");
}
When the user logs in, I grabbed the user UID of firebase, and get the notebooks, and the notes within that notebook
The brig problem is that for some reason, my notebook variable gets emptied
private async void GetNotebooksAsync() {
var notebooks = await Database.ReadAsync<Notebook>();
if (notebooks != null) {
notebooks = notebooks.Where(book => book.Id == App.UserId).ToList();
NotebooksCollection.Clear();
foreach (var item in notebooks)
{
NotebooksCollection.Add(item);
}
}
}
The strange part is that I am filtering by the UserId of the notebook, and as you can see
the user a@a.com with the id of 8zOEcqbNxxxxxxxx4qVRSpWEv2 has one notebook
So I do not understand by the filter do not work
here is my project if you want to test it
https://github.com/eduardoagr/safe.Xamarin/tree/main/Safe
Furthermore, you can log in with a@a.com/ // this user has one notebook assigned to it
user b@b.com does not have a notebook
And if I do
var notebooks = await Database.ReadAsync<Notebook>();
if (notebooks != null) {
notebooks.Where(book => book.Id == App.UserId).ToList();
NotebooksCollection.Clear();
foreach (var item in notebooks)
{
NotebooksCollection.Add(item);
}
if I do that I can see the same notebook in user a@a.com and b@b.com
