React Native 中来自 Firebase SDK 的 AccessToken 和 ExpiryDate
AccessToken and ExpiryDate from Firebase SDK in React Native
在使用来自 Firebase 的 Rest API 之前,我尝试使用 Firebase Auth 来验证我的应用程序,它对我和我的 Reducer 有效,因为 Reducer 需要 uid、accessToken 和expirationTime,我能够让这段代码在登录时工作(至少要获得登录的响应,我需要在我的 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);
}
现在,当我记录整个 resData 时,我得到这个 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",
},
}
无论如何,我认为它只是通过像这样的控制台日志来达到特定的 object:
console.log(resData.user.stsTokenManager.accessToken);
我试过了,结果是 Undefined 正如你在这里看到的:
现在我尝试让 AutoComplete 函数向我显示 resData 对象的内容,我得到了这个:
我假设在用户内部我会有 stsTokenManager 但没有显示这样的东西:
最后,这是我的 Reducer,供大家查看为什么我需要 UID 和带有到期日的 Token:
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:我在网上看到了一个getToken()的方法,应该应用于signInWithEmailAndPassword(email, password) 在这种情况下是 resData,但是当我这样做时,我收到错误消息:
resData.getToken 不是函数。 (在'resData.getToken()'中,'resData.getToken'未定义)
有什么想法吗?
stsTokenManager 是一个承诺,您需要这样做才能完美地工作:
const res = await Api.loginEmailSenha(emailField, passwordField);
const token = await res.user.getIdToken();
console.log(token);
在使用来自 Firebase 的 Rest API 之前,我尝试使用 Firebase Auth 来验证我的应用程序,它对我和我的 Reducer 有效,因为 Reducer 需要 uid、accessToken 和expirationTime,我能够让这段代码在登录时工作(至少要获得登录的响应,我需要在我的 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);
}
现在,当我记录整个 resData 时,我得到这个 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",
},
}
无论如何,我认为它只是通过像这样的控制台日志来达到特定的 object:
console.log(resData.user.stsTokenManager.accessToken);
我试过了,结果是 Undefined 正如你在这里看到的:
现在我尝试让 AutoComplete 函数向我显示 resData 对象的内容,我得到了这个:
我假设在用户内部我会有 stsTokenManager 但没有显示这样的东西:
最后,这是我的 Reducer,供大家查看为什么我需要 UID 和带有到期日的 Token:
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:我在网上看到了一个getToken()的方法,应该应用于signInWithEmailAndPassword(email, password) 在这种情况下是 resData,但是当我这样做时,我收到错误消息:
resData.getToken 不是函数。 (在'resData.getToken()'中,'resData.getToken'未定义)
有什么想法吗?
stsTokenManager 是一个承诺,您需要这样做才能完美地工作:
const res = await Api.loginEmailSenha(emailField, passwordField);
const token = await res.user.getIdToken();
console.log(token);