简体   繁体   中英

AccessToken and ExpiryDate from Firebase SDK in React Native

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": "admin@chronotechsv.info",
    "emailVerified": false,
    "isAnonymous": false,
    "lastLoginAt": "1611933308747",
    "phoneNumber": null,
    "photoURL": null,
    "providerData": Array [
      Object {
        "displayName": null,
        "email": "admin@chronotechsv.info",
        "phoneNumber": null,
        "photoURL": null,
        "providerId": "password",
        "uid": "admin@chronotechsv.info",
      },
    ],
    "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:

第1部分

第2部分

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM