I have followed all of the Dgraph Auth tutorials I can find, and I still cannot get Firebase auth to work. At this point I am able to create a user in Firebase and retrieve the JWT in the app, but the token does not include a namespace and therefore I cannot connect it to my queries/schema.
Note: It looks like Firebase upgraded to v9, and that may mean Dgraph’s firebase docs are out of date.
From what I have read, it sounds like I have to set up a Cloud Function in order to add the Namespace field. I do that here (and I see it running in the Firebase console):
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addUserClaim = functions.https.onCall(async (data, context) => {
return admin
.auth()
.getUserByEmail(data.email)
.then((user) => {
return admin.auth().setCustomUserClaims(user.uid, {
'https://dgraph.io/jwt/claims': {
USER: data.email,
},
});
})
.then(() => {
return {
message: `Success! ${data.email}`,
};
})
.catch((err: any) => {
return err;
});
});
However, the JWT I get back isn’t any different than it was before I created the function:
{
"name": "Jake",
"picture": "https://lh3.googleusercontent.com/a-/AOh14Gh8FTRv1Ze7DkDH0luxxxkeKTX5cJBCbtuN-=s96-c",
"iss": "https://securetoken.google.com/contributor-credits",
"aud": "contributor-credits",
"auth_time": 1643219275,
"user_id": "ZD0s2fmOXuQcqIVZaUqQ3Pf8X3e2",
"sub": "ZD0s2fmOXuQcqIVZaUqQ3Pf8X3e2",
"iat": 1643223533,
"exp": 1643227133,
"email": "jake@xxxx.io",
"email_verified": true,
"firebase": {
"identities": {
"google.com": [
"111980424334d4207177753"
],
"email": [
"jake@xxxxx.io"
]
},
"sign_in_provider": "google.com"
}
}
Here is how I retrieve the token:
const [user, loading, error] = useAuthState(fireApp.auth());
const authLink = setContext(async (_, { headers }) => {
if (!user) {
return headers;
}
const token = user.getIdToken();
const key = process.env.NEXT_PUBLIC_NETLIFY_CLIENT_CC;
return {
headers: {
...headers,
'X-Auth-Token': token ? token : '',
// 'DG-Auth': key ?? undefined,
},
};
});
The question is how I add the Namespace and associated fields to the JWT. Once I have that, I believe I can get authorization working.