I have created a firebase authentication using google OAuth. When I am signing in for the first time, we have a function that spills the google OAuth’s access-token – I am using this token to call a googleapis. But I am looking for the same access-token when I am coming back to my web-app. I have the currentUser, but I am unable to figure out as on how to get the creds.
const signInWithGoogle = () => {
const provider = new GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/youtube.readonly');
const auth = getAuth(app);
signInWithPopup(auth, provider)
.then(async (result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
console.log("token", token)
// IdP data available using getAdditionalUserInfo(result)
// ...
}).catch((error) => {
console.log("error", error)
});
}
In this code token is giving me the output I want. I want the same thing once I come back or refresh my web-app.
Something that I can fetch with auth.currentUser or something like that.
I tried this code.
auth.currentUser.getIdTokenResult().then((idTokenResult) => {
const provider = new GoogleAuthProvider();
const credential = GoogleAuthProvider.credential(idTokenResult);
// const token = credential.accessToken;
console.log("idTokenResult", idTokenResult, "-----------------token", credential)
// Confirm the user is an Admin.
})
Result
{
"idToken": {
"claims": {
"name": "Shenor shai",
"picture": "https://lh3.googleusercontent.com/a/AGNmyxMdzHBqcvUriJvQPyYc2PhdjrIA=s96-c",
"iss": "https://securetoken.google.com/chainlink-hackathon-23",
"aud": "random",
"auth_time": 1684419631,
"user_id": "aofhafihfiafhafia",
"sub": "aofhafihfiafhafia",
"iat": 1684421034,
"exp": 1684424634,
"email": "[email protected]",
"email_verified": true,
"firebase": {
"identities": {
"google.com": [
"118112290937553408948"
],
"email": [
"[email protected]"
]
},
"sign_in_provider": "google.com"
}
},
"token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjFiYjI2MzY4YTNkMWExNDg1YmNhNTJiNGY4M2JkYjQ5YjY0ZWM2MmYiLCJ0eXAiOiJKV1QifQ..U5Gmr-vt_paxbUck2UNGlKifrVk2FhygoBGoxatTBcQ_j6wZi9FZs6s6-pajQLq5nrVIMOM_8W9yJN_gCg0V8W0gl2cmAH8uREZDg5G2lWOunTD8Nbze0yrg6us5_hctThZ8uxrXFD_kPvVLJO6tJZeoI8A2eW-kzg7xcP4B8_Y9qc6QpJguJf4PqxE7522bQ7fI3tu_T0vNAk0Bt36o-K-Adr1guL4Mxj3PNs0WKEPen9iiirAoPjOFtVY_Pk46-paXwVtBLP8tC3H1ZN0ysp9DwD99JsejvGUWthlX9Qz_0GqnrDXBrst3O5OFwQKYzbbtv_vs_A8PdJc6A9Uk-A",
"authTime": "Thu, 18 May 2023 14:20:31 GMT",
"issuedAtTime": "Thu, 18 May 2023 14:43:54 GMT",
"expirationTime": "Thu, 18 May 2023 15:43:54 GMT",
"signInProvider": "google.com",
"signInSecondFactor": null
},
"pendingToken": null,
"providerId": "google.com",
"signInMethod": "google.com"
}