I’m trying to use realm.login.
- I’m using expo-auth-session/providers/google for getting the oAuth 2.0 access_code
- The Google oAuth response params have access_token, code, id_token, etc.
- I’m trying to use the access_token, id_token, or code for creating the credential for using realm.login()
- The error code is 47 and message is
“error exchanging access code with OAuth2 provider” or “error fetching info from OAuth2 provider”.
`
const [request, response, promptAsync] = Google.useAuthRequest({
expoClientId: process.env.GOOGLE_EXPO_CLIENT_ID,
androidClientId: process.env.GOOGLE_ANDROID_CLIENT_ID,
iosClientId: process.env.GOOGLE_IOS_CLIENT_ID,
webClientId: process.env.GOOGLE_WEB_CLIENT_ID,
});
useEffect(async () => {
// try {
if (response?.type === “success”) {
const idToken = response.params.id_token;
const { access_token, code } = response.params;
if (idToken) {
const credentials = Realm.Credentials.google({
idToken,
// authCode: code,
// idToken: access_token,
});
realm
.logIn(credentials)
.then((user) => {
console.log(`Logged in with id: ${user.id}`);
})
.catch((err) => {
console.log("@realm.login: err", err);
});
}
}
}, [response]);
`
With the above code, if I do console.log(credentials) it’s an empty object. Is it normal?
For Realm,
- I turned on Google for [Authentication Providers] and added client ID and secret from Google console for the web application credential for Realm.
- Open ID is off
and Google console.
I have the web application credential for Realm;
Authorized JavaScript origins: https://realm.mongodb.com
Authorized Redirect URIs: https://ap-southeast-1.aws.stitch.mongodb.com/api/client/v2.0/auth/callback
And 4 more credentials (Web application for Expo Go Proxy, Web application for web client, iOS, and Android)
And also have /auth/providers.json (I’m not sure it’s needed even I set it on the Realm UI page)
{ "oauth2-google": { "name": "oauth2-google", "type": "oauth2-google", "disabled": false, "config": { "clientId": process.env.GOOGLE_CLIENT_ID, "openId": false }, "secret_config": { "clientSecret": process.env.GOOGLE_CLIENT_SECRET }, "metadata_fields": ["name", "first_name", "last_name", "picture", "email" ], "redirect_uris": [], "domain_restrictions": [] } }
FYI,
If I run this code with the access_token, I can receive the user data without any issue.
const getUserInfo = async (token) => { let userInfoResponse = await fetch( "https://www.googleapis.com/oauth2/v2/userinfo", { headers: { Authorization:
Bearer ${token}` },
},
).catch((err) => console.log(“@getUserInfo: err”, err));
userInfoResponse
.json()
.then((data) => {
console.log("@userInfoResponse > data:", data);
const user = data;
})
.catch((err) => console.log("@userInfoResponse: err", err));
};`