gpt4 book ai didi

react-native - 在 React Native 中处理刷新 token

转载 作者:行者123 更新时间:2023-12-03 09:42:25 28 4
gpt4 key购买 nike

我有一个应用程序验证正常并返回 access_tokenrefresh_token .我用 AsyncStorage 存储它们并保存/获取 access_token与还原。这是我正在构建的第一个应用程序,我正在为如何以及在何处使用 refresh_token 而苦苦挣扎。 .

这是组件中的 axios 调用 loginForm.js

axios({
url: `${base}/oauth/token`,
method: 'POST',
data: formData,
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
}
})
.then(response => {
setStatus({ succeeded: true });
// console.log(response.data);
deviceStorage.saveKey("userToken", response.data.access_token);
deviceStorage.saveKey("refreshToken", response.data.refresh_token);
Actions.main();
})
.catch(error => {
if (error.response) {
console.log(error);
}
});

这是服务 deviceStorage.js
import { AsyncStorage } from 'react-native';

const deviceStorage = {
async saveItem(key, value) {
try {
await AsyncStorage.setItem(key, value);
} catch (error) {
console.log('AsyncStorage Error: ' + error.message);
}
}
};

export default deviceStorage;

这是 token 操作文件
import { AsyncStorage } from 'react-native';
import {
GET_TOKEN,
SAVE_TOKEN,
REMOVE_TOKEN,
LOADING_TOKEN,
ERROR_TOKEN
} from '../types';

export const getToken = token => ({
type: GET_TOKEN,
token,
});

export const saveToken = token => ({
type: SAVE_TOKEN,
token
});

export const removeToken = () => ({
type: REMOVE_TOKEN,
});

export const loading = bool => ({
type: LOADING_TOKEN,
isLoading: bool,
});

export const error = tokenError => ({
type: ERROR_TOKEN,
tokenError,
});

export const getUserToken = () => dispatch =>
AsyncStorage.getItem('userToken')
.then((data) => {
dispatch(loading(false));
dispatch(getToken(data));
})
.catch((err) => {
dispatch(loading(false));
dispatch(error(err.message || 'ERROR'));
});

export const saveUserToken = (data) => dispatch =>
AsyncStorage.setItem('userToken', data)
.then(() => {
dispatch(loading(false));
dispatch(saveToken('token saved'));
})
.catch((err) => {
dispatch(loading(false));
dispatch(error(err.message || 'ERROR'));
});

export const removeUserToken = () => dispatch =>
AsyncStorage.removeItem('userToken')
.then((data) => {
dispatch(loading(false));
dispatch(removeToken(data));
})
.catch((err) => {
dispatch(loading(false));
dispatch(error(err.message || 'ERROR'));
});

这是 token 缩减文件
import {
GET_TOKEN,
SAVE_TOKEN,
REMOVE_TOKEN,
LOADING_TOKEN,
ERROR_TOKEN
} from '../actions/types';

const INITIAL_STATE = {
token: {},
loading: true,
error: null
};

export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case GET_TOKEN:
return {
...state,
token: action.token
};
case SAVE_TOKEN:
return {
...state,
token: action.token
};
case REMOVE_TOKEN:
return {
...state,
token: action.token
};
case LOADING_TOKEN:
return {
...state,
loading: action.isLoading
};
case ERROR_TOKEN:
return {
...state,
error: action.error
};
default:
return state;
}
};

这是认证文件
import React from 'react';
import {
StatusBar,
StyleSheet,
View,
} from 'react-native';
import { connect } from 'react-redux';
import { Actions } from 'react-native-router-flux';
import { Spinner } from '../common';
import { getUserToken } from '../../actions';

class AuthLoadingScreen extends React.Component {

componentDidMount() {
this.bootstrapAsync();
}

bootstrapAsync = () => {
this.props.getUserToken().then(() => {
if (this.props.token.token !== null) {
Actions.main();
} else {
Actions.auth();
}
})
.catch(error => {
this.setState({ error });
});
};

render() {
return (
<View style={styles.container}>
<Spinner />
<StatusBar barStyle="default" />
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
});

const mapStateToProps = state => ({
token: state.token,
});


const mapDispatchToProps = dispatch => ({
getUserToken: () => dispatch(getUserToken()),
});

export default connect(mapStateToProps, mapDispatchToProps)(AuthLoadingScreen);


我相信我需要创建一个 Action 和 reducer 来获得 refresh_token (这是正确的吗?)但我不知道如何处理它以及在哪里调用它(也许在身份验证文件中?)。
任何可能与我的代码相关的代码示例的帮助将不胜感激。谢谢

最佳答案

下面是步骤
执行 Login ,从响应中获取 accessToken , refreshToken 并将其保存到 AsyncStorage。
为API调用制作通用函数

    async function makeRequest(method, url, params, type) {
const token = await AsyncStorage.getItem('access_token');

let options = {
method: method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
};

if (!token) {
delete options['Authorization'];
}

if (['GET', 'OPTIONS'].includes(method)) {
url += (url.indexOf('?') === -1 ? '?' : '&') + queryParams(params);
} else {
Object.assign(options, {body: JSON.stringify(params)});
}

const response = fetch(ENV.API_URL+url, options);

return response;
}
在 redux 中为 getAceessTokenFromRefreshToken 制作一种方法。
session 过期时使用此方法
你怎么知道 session 已过期?
从每个 API 调用中,如果您在

async componentWillReceiveProps(nextProps) {
if (nextProps.followResponse && nextProps.followResponse != this.props.followResponse) {
if (nextProps.followResponse.status) {

if (nextProps.followResponse.status == 440) {
// call here get acceesstokenfrom refresh token method and save again accesstoken in asyncstorage and continue calling to API
}
}
}
}

关于react-native - 在 React Native 中处理刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58051379/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com