Reputation: 541
I'm trying to use the Firebase Auth to Authenticate my App before I use the Rest API from Firebase and it works for me and my Reducer as the Reducer requires the uid, the accessToken, and the expirationTime, I was able to get this code to work on Sign In (at least to get the response as to log In I need to get data in my Reducer).
export const login = (email, password) => async dispatch => {
try {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(resData => {
console.log(resData);
})
.catch(err => {
console.log(err);
});
} catch (err) {
console.log(err);
}
Now when I log the Entire resData I get this JSON Object:
Object {
"additionalUserInfo": ig {
"isNewUser": false,
"providerId": "password",
},
"credential": null,
"operationType": "signIn",
"user": Object {
"apiKey": "[My API Key]",
"appName": "[DEFAULT]",
"authDomain": "alianzafc2021.firebaseapp.com",
"createdAt": "1611239757130",
"displayName": null,
"email": "[email protected]",
"emailVerified": false,
"isAnonymous": false,
"lastLoginAt": "1611933308747",
"phoneNumber": null,
"photoURL": null,
"providerData": Array [
Object {
"displayName": null,
"email": "[email protected]",
"phoneNumber": null,
"photoURL": null,
"providerId": "password",
"uid": "[email protected]",
},
],
"redirectEventId": null,
"stsTokenManager": Object {
"accessToken": "[***THE ACCESS TOKEN I NEED***]",
"apiKey": "[My API Key]",
"expirationTime": 1611937026869,
"refreshToken": "[A Refres Token]",
},
"tenantId": null,
"uid": "5Vzshkdlu8W3sDSZMt9bc9Sn9k92",
},
}
Anyways I thought it will simply be to get to that specific object by doing a Console log like:
console.log(resData.user.stsTokenManager.accessToken);
I tried that and the result is Undefined as you can see here:
Now I tried that the AutoComplete function shows me what is inside of the resData Obeject and I get this:
I assumed that inside of user I would have the stsTokenManager but no such thing displayed:
Finally Here is my Reducer for you guys to check out why I need the UID and the Token with Expiration day:
import { AUTHENTICATE, LOGOUT, SET_DID_TRY_AL } from "../actions/auth";
const initialState = {
token: null,
userId: null,
didTryAutoLogin: false,
};
export default(state = initialState, action) => {
switch (action.type) {
case AUTHENTICATE:
return{
token: action.token,
userId: action.userId,
didTryAutoLogin: true,
};
case SET_DID_TRY_AL:
return {
...state,
didTryAutoLogin: true,
}
case LOGOUT:
return {
...initialState,
didTryAutoLogin: true,
};
// case SIGNUP:
// return{
// token: action.token,
// userId: action.userId,
// };
default:
return state;
}
};
PS: I read online about a method of getToken(), which should be applied to the result of the signInWithEmailAndPassword(email, password) in this case resData, but when I do I get the error message:
resData.getToken is not a function. (In 'resData.getToken()', 'resData.getToken' is undefined)
Any Ideas?
Upvotes: 0
Views: 236
Reputation: 86
The stsTokenManager is a promisse, you will need do this to work perfectly:
const res = await Api.loginEmailSenha(emailField, passwordField);
const token = await res.user.getIdToken();
console.log(token);
Upvotes: 2