Graphql Apollo server and firebase authentication

Viewed 529

I have graphql api server and call the api from my flutter app.

Now I need to implement login authentication. So I decided to use firebase authentication.

I need email, google and facebook sign in.

I am a bit puzzle how to authenticate the user.

  1. From my flutter app, authenticate directly with firebase and get the token. If got the token call create user api and create the user. Of course, I will pass the token and verify the token against firebase.

  2. Create SignIn, SignInWithGoogle and SignInWithFacebook end point in my Apollo server and call from my flutter.

Which way is the common practice?

1 Answers

I have built using a similar stack, Firebase Auth to handle login with email, phone, and social and then pass the token to the backend to verify that authenticated and valid users are making those requests. I'm using a nodejs/graphql/mongodb backend. Using a middleware to authenticate each request and even handle push notifications in the graphql mutations.

First method would be to request to login to firebase and retrieve the user credentials.

Second method would be to send a request to the server, the server sends a request to firebase, firebase returns the credentials to the server, then the server returns the credentials to you.

Basically take the easy route and save server costs, no need to make a call to the server when you can simply do it on the client directly to firebase. When you do it on the client side you have access to the authStateChanges and token firebase gives you as well which will make your life a lot easier.

Related