Skip to content

Commit c777180

Browse files
author
Renato Rodrigues
committed
logout flow
1 parent 8a00381 commit c777180

File tree

6 files changed

+78
-9
lines changed

6 files changed

+78
-9
lines changed

src/modules/Auth/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SIGNUP_RESULT,
88
FORGOT_PASSWORD,
99
FORGOT_PASSWORD_RESULT,
10+
LOGOUT,
1011
} from './types';
1112

1213
export const processing = createAction(PROCESSING);
@@ -19,3 +20,5 @@ export const signUpResult = createAction(SIGNUP_RESULT);
1920

2021
export const doForgotPassword = createAction(FORGOT_PASSWORD);
2122
export const forgotPasswordResult = createAction(FORGOT_PASSWORD_RESULT);
23+
24+
export const doLogout = createAction(LOGOUT);

src/modules/Auth/saga.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { put, takeLatest } from 'redux-saga/effects';
2-
import { LOGIN, SIGNUP, FORGOT_PASSWORD } from './types';
2+
import { LOGIN, SIGNUP, FORGOT_PASSWORD, LOGOUT } from './types';
33
import { loginResult, signUpResult, forgotPasswordResult } from './actions';
44
// import request from '@utils/requests';
55
import { AuthSaga } from './interfaces';
6+
import { secureSetTokens, secureRemoveTokens } from '@utils/secureToken';
67

78
export function* authenticate({ payload: authData }: AuthSaga) {
89
try {
@@ -11,6 +12,7 @@ export function* authenticate({ payload: authData }: AuthSaga) {
1112
// method: 'POST',
1213
// data: authData,
1314
// });
15+
yield secureSetTokens({ expirationDate: 3600, token: '123' });
1416
yield put(
1517
loginResult({
1618
isLoggedIn: true,
@@ -104,8 +106,23 @@ export function* forgotPassword({ payload: forgotPasswordData }: AuthSaga) {
104106
}
105107
}
106108

109+
export function* logout() {
110+
try {
111+
yield secureRemoveTokens();
112+
yield put(
113+
loginResult({
114+
data: {},
115+
isLoggedIn: false,
116+
}),
117+
);
118+
} catch (err) {
119+
throw Error('Unexpected error when try to logout');
120+
}
121+
}
122+
107123
export default function* authSagas() {
108124
yield takeLatest(LOGIN, authenticate);
109125
yield takeLatest(SIGNUP, signUp);
110126
yield takeLatest(FORGOT_PASSWORD, forgotPassword);
127+
yield takeLatest(LOGOUT, logout);
111128
}

src/modules/Auth/screens/Login/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import {
33
SafeAreaView,
44
StyleSheet,
@@ -13,6 +13,8 @@ import { AuthRedux, AuthState } from '@modules/Auth/interfaces';
1313
import { useSelector, useDispatch } from 'react-redux';
1414
import { doLogin } from '@modules/Auth/actions';
1515
import { useNavigation } from 'react-navigation-hooks';
16+
import { secureGetTokens } from '@utils/secureToken';
17+
import _isEmpty from 'lodash/isEmpty';
1618

1719
const LoginScreen = () => {
1820
const dispatch = useDispatch();
@@ -26,7 +28,6 @@ const LoginScreen = () => {
2628

2729
const handleLogin = () => {
2830
dispatch(doLogin({ email: '', password: '' }));
29-
navigation.navigate('Main');
3031
};
3132

3233
const handleSignUp = () => {
@@ -37,6 +38,15 @@ const LoginScreen = () => {
3738
navigation.navigate('ForgotPassword');
3839
};
3940

41+
useEffect(() => {
42+
(async function checkLogged() {
43+
const isLogged = await secureGetTokens();
44+
if (!_isEmpty(isLogged)) {
45+
navigation.navigate('Main');
46+
}
47+
})();
48+
}, [navigation, authState.isLoggedIn]);
49+
4050
return (
4151
<>
4252
<StatusBar barStyle="dark-content" />

src/modules/Auth/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export const SIGNUP_RESULT = 'app/Auth/SIGNUP_RESULT';
88

99
export const FORGOT_PASSWORD = 'app/Auth/FORGOT_PASSWORD';
1010
export const FORGOT_PASSWORD_RESULT = 'app/Auth/FORGOT_PASSWORD_RESULT';
11+
12+
export const LOGOUT = 'app/Auth/LOGOUT';
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
1-
import * as React from 'react';
2-
import { View, Text } from 'react-native';
1+
import React, { useEffect } from 'react';
2+
import { View, Text, StyleSheet, Button } from 'react-native';
3+
import { useDispatch, useSelector } from 'react-redux';
4+
import { doLogout } from '@modules/Auth/actions';
5+
import { useNavigation } from 'react-navigation-hooks';
6+
import { AuthRedux, AuthState } from '@modules/Auth/interfaces';
37

48
export interface AppProps {}
59

6-
const App = () => {
10+
const App: React.FC = () => {
11+
const dispatch = useDispatch();
12+
const navigation = useNavigation();
13+
14+
const authState: AuthRedux = useSelector(({ auth }: AuthState) => ({
15+
isLoggedIn: auth.isLoggedIn,
16+
}));
17+
18+
const handleLogout = () => {
19+
dispatch(doLogout());
20+
};
21+
22+
useEffect(() => {
23+
if (!authState.isLoggedIn) {
24+
navigation.navigate('Login');
25+
}
26+
}, [authState.isLoggedIn, navigation]);
27+
728
return (
8-
<View>
9-
<Text>App</Text>
29+
<View style={styles.body}>
30+
<Text style={styles.title}>{`Main Screen!\nYou're Logged!`}</Text>
31+
<Button onPress={handleLogout} title="Logout" />
1032
</View>
1133
);
1234
};
1335

36+
const styles = StyleSheet.create({
37+
body: {
38+
flex: 1,
39+
justifyContent: 'space-evenly',
40+
alignItems: 'center',
41+
},
42+
title: {
43+
fontSize: 24,
44+
textAlign: 'center',
45+
},
46+
});
47+
1448
export default App;

src/store/reducers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { combineReducers } from 'redux';
22
import auth from '@modules/Auth/reducer';
3+
import { LOGOUT } from '@modules/Auth/types';
34

45
const reducers = combineReducers({
56
auth,
@@ -8,7 +9,9 @@ const reducers = combineReducers({
89
export default reducers;
910

1011
export const rootReducer = (state: any, action: any) => {
11-
// TODO: Logout to clear redux states
12+
if (action.type === LOGOUT) {
13+
state = {};
14+
}
1215
return reducers(state, action);
1316
};
1417

0 commit comments

Comments
 (0)