I came here to ask you for suggestions about best practices for storing Firebase's user data in own SQL database.
I have made .NET Minimal API project for back-end, React Native for front-end and I am using Firebase Auth for authenticate users (before I was using my own implementation made in .NET). I need to store user data in my own database because of relationships between users and other aggregates/entities.
The only problem I have is to came up with idea what code flow should be to register user in Firebase and then based on that create this user in my own SQL database. I came up with 3 solutions (maybe very bad or not):
- Create register endpoint in my Minimal API project and create user in FB by myself using FirebaseAdmin and store it in DB. The biggest issue with this solution is that CreateUserAsync() not returning Bearer token for the user.
app.MapPost("users/register", async ([FromBody] RegisterRequest request, ISender sender) =>
{
UserRecordArgs args = new UserRecordArgs()
{
Email = request.Email,
EmailVerified = false,
PhoneNumber = request.PhoneNumber,
Password = request.Password,
DisplayName = request.Username,
PhotoUrl = request.PhotoUrl,
Disabled = false,
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args); // Creating user in Firebase
var result = await sender.Send(new RegisterCommand(
args.DisplayName, args.Password, args.Email, args.PhotoUrl)); // Using MediatR to create user in own DB
return Results.Ok(userRecord);
});
- Again creating an endpoint but this time register user as a client (this way I can return token to user):
private const string API_KEY = "super-hyper-api-key";
public FirebaseAuthClient AuthClient()
{
FirebaseAuthConfig config = new FirebaseAuthConfig
{
ApiKey = API_KEY,
AuthDomain = "appname.firebaseapp.com",
Providers = new FirebaseAuthProvider[]
{
new EmailProvider()
}
};
return new FirebaseAuthClient(config);
}
app.MapPost("users/register", async ([FromBody] RegisterRequest request, ISender sender) =>
{
var user = await AuthClient().CreateUserWithEmailAndPasswordAsync(
request.Email, request.Password, request.Username);
return Results.Ok(user);
});
- In React make two different calls one for creating an user in FB and if it succeed call my minimal API user data (or Bearer Token and encode it after) and store user data in my DB for example (simplified):
const handleSignUp = () => {
auth
.createUserWithEmailAndPassword(email, password)
.then(userCredentials => {
const user = userCredentials.user;
register(user.uid, user.username, user.email)
})
.catch(error => alert(error.message))
}
const register = (uid, username, email) => {
setIsLoading(true);
let data = JSON.stringify({
"uid": uid,
"username": username,
"email": email
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: `${BASE_URL}/users/register`,
headers: {
'Content-Type': 'application/json'
},
data : data
};
axios.request(config)
.then((response) => {
let data = JSON.stringify(response.data)
console.log(data);
})
.catch((error) => {
console.log(error);
});
setIsLoading(false);
};
What do you think about those solutions? Or maybe there are some better solutions (I am sure there are ;)) you can share :)