我已经进行了登录和注销操作以及userReducer . 如何将asyncstorage与redux集成?我使用redux thunk作为中间件 .
我能够使用内部状态变量实现登录和注销,但我无法理解如何将其分解为action和reducer以及使用asyncstorage来存储accessToken .
原始代码:
_onLogin = () => {
auth0.webAuth
.authorize({
scope: 'openid profile',
audience: 'https://' + credentials.domain + '/userinfo'
})
.then(credentials => {
this.setState({ accessToken: credentials.accessToken });
})
.catch(error => console.log(error));
};
_onLogout = () => {
if (Platform.OS === 'android') {
this.setState({ accessToken: null });
} else {
auth0.webAuth
.clearSession({})
.then(success => {
this.setState({ accessToken: null });
})
.catch(error => console.log(error));
}
};
loginAction.js:
import { LOGIN_USER } from './types';
import Auth0 from 'react-native-auth0';
var credentials = require('./auth0-credentials');
const auth0 = new Auth0(credentials);
export const loginUser = () => dispatch => {
auth0.webAuth
.authorize({
scope: 'openid profile',
audience: 'https://' + credentials.domain + '/userinfo'
})
.then(credentials =>
dispatch({
type: LOGIN_USER,
payload: credentials.accessToken
})
)
.catch(error => console.log(error));
}
logoutAction.js:
import { LOGOUT_USER } from './types';
import Auth0 from 'react-native-auth0';
var credentials = require('./auth0-credentials');
const auth0 = new Auth0(credentials);
export const logoutUser = () => dispatch => {
auth0.webAuth
.clearSession({})
.then(success =>
dispatch({
type: LOGOUT_USER,
payload: null
})
)
.catch(error => console.log(error));
}
userReducer.js:
import { LOGIN_USER, LOGOUT_USER } from '../actions/types';
const initialState = {
accessToken: null
}
export default function (state = initialState, action) {
switch (action.type) {
case LOGIN_USER:
_storeData = async () => {
try {
await AsyncStorage.setItem('accessToken', action.payload);
} catch (error) {
console.log(error)
}
}
return {
...state,
accessToken:action.payload
};
case LOGOUT_USER:
_removeData = async (accessToken) => {
try {
await AsyncStorage.removeItem(accessToken);
} catch (error) {
console.log(error)
}
}
return {
...state,
accessToken:action.payload
};
default:
return state;
}
}
我是redux的新手,所以我尝试将原始代码转换为actions和reducers,但我不确定我是否在userReducer.js中正确实现了asyncstorage?
2 回答
您可以方便地单独使用AsyncStorage OR redux来管理身份验证状态 . 取决于你感到满意 . 我会举两个例子 .
对于AsyncStorage:假设您的身份验证密钥仅在2周内有效 . 您可以在用户登录时记下并节省时间 . 例如:
在
../actions/auth.js
现在我们可以从任何组件
import { isSignedIn } from '../actions/auth';
使用它,如下所示:////////////////////////////////////////////////// //////////////////////////
如果你想使用redux
处理redux中的登录
在你的
types.js
在你的redux行动中
在您的loginReducer中
在你的
./reducers/index.js
在您使用redux-thunk的商店中,applyMiddleWare . 让我们称之为configureStore.js
在你的App.js中
您应该知道
auth.js
中不再需要isSignedIn方法 . 您的登录方法与LoginScreen中的上述方法保持一致 .现在您可以使用redux来检查登录状态,如下所示:
应该有一种更好的方法来使用redux来管理登录 - 比我在这里概述的要好 . 我认为您也可以使用redux来管理您的登录状态,而无需使用AsyncStorage . 您需要做的只是在您的loginScreen中,如果登录函数返回response.status =='ok',您可以向redux发送一个记录用户的操作 . 在上面的示例中,使用asyncstorage您可能只需要使用redux检查用户是否已登录 .
为了坚持redux状态,我建议你redux-persist .
Installation:
Usage:
首先,配置redux store
然后,使用
PersistGate
包装根组件