Browse Source

update logout method in AccountAPI

update store: AppAction & AppSage for logout op.
update setting screen pass token params to logout action
pull/19011/head
masumulu28 3 years ago
parent
commit
1d0cab2ef9
  1. 28
      templates/app/react-native/src/api/AccountAPI.js
  2. 32
      templates/app/react-native/src/screens/Settings/SettingsScreen.js
  3. 11
      templates/app/react-native/src/store/actions/AppActions.js
  4. 26
      templates/app/react-native/src/store/sagas/AppSaga.js

28
templates/app/react-native/src/api/AccountAPI.js

@ -4,13 +4,12 @@ import { getEnvVars } from '../../Environment';
const { oAuthConfig } = getEnvVars(); const { oAuthConfig } = getEnvVars();
getLoginData = (username, password) => { getLoginData = (username, password) => {
const formData = { const formData = {
grant_type: 'password', grant_type: 'password',
scope: oAuthConfig.scope, scope: oAuthConfig.scope,
username: username, username: username,
password: password, password: password,
client_id: oAuthConfig.clientId client_id: oAuthConfig.clientId,
}; };
if (oAuthConfig.clientSecret) if (oAuthConfig.clientSecret)
@ -19,7 +18,7 @@ getLoginData = (username, password) => {
return Object.entries(formData) return Object.entries(formData)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`) .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join('&'); .join('&');
} };
export const login = ({ username, password }) => export const login = ({ username, password }) =>
api({ api({
@ -27,15 +26,28 @@ export const login = ({ username, password }) =>
url: '/connect/token', url: '/connect/token',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: getLoginData(username, password), data: getLoginData(username, password),
baseURL: oAuthConfig.issuer baseURL: oAuthConfig.issuer,
}).then(({ data }) => data); }).then(({ data }) => data);
export const Logout = (
input = { client_id: '', token: '', token_type_hint: '' },
) => {
if (!input.token_type_hint) {
input.token_type_hint = 'access_token';
}
export const Logout = () => const _data = Object.entries(input)
api({ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
method: 'GET', .join('&');
url: '/api/account/logout',
return api({
method: 'POST',
url: '/connect/revocat',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: _data,
baseURL: oAuthConfig.issuer,
}).then(({ data }) => data); }).then(({ data }) => data);
};
export const getTenant = tenantName => export const getTenant = tenantName =>
api({ api({

32
templates/app/react-native/src/screens/Settings/SettingsScreen.js

@ -1,17 +1,31 @@
import { Ionicons } from '@expo/vector-icons'; import { Ionicons } from '@expo/vector-icons';
import { useFocusEffect } from '@react-navigation/native'; import { useFocusEffect } from '@react-navigation/native';
import i18n from 'i18n-js'; import i18n from 'i18n-js';
import { Avatar, Button, Divider, FormControl, List, Select, Stack, Text, View } from 'native-base'; import {
Avatar,
Button,
Divider,
FormControl,
List,
Select,
Stack,
Text,
View,
} from 'native-base';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import { getProfileDetail } from '../../api/IdentityAPI'; import { getProfileDetail } from '../../api/IdentityAPI';
import AppActions from '../../store/actions/AppActions'; import AppActions from '../../store/actions/AppActions';
import { import {
createLanguageSelector, createLanguageSelector,
createLanguagesSelector createLanguagesSelector,
} from '../../store/selectors/AppSelectors'; } from '../../store/selectors/AppSelectors';
import { createTenantSelector } from '../../store/selectors/PersistentStorageSelectors'; import { createTenantSelector } from '../../store/selectors/PersistentStorageSelectors';
import { connectToRedux } from '../../utils/ReduxConnect'; import { connectToRedux } from '../../utils/ReduxConnect';
import { store } from '../../store/index';
import { getEnvVars } from '../../../Environment';
const env = getEnvVars();
function SettingsScreen({ function SettingsScreen({
navigation, navigation,
@ -22,6 +36,7 @@ function SettingsScreen({
tenant = {}, tenant = {},
}) { }) {
const [user, setUser] = useState({}); const [user, setUser] = useState({});
const [token, setToken] = useState(store.getState().persistentStorage.token);
const fetchUser = () => { const fetchUser = () => {
getProfileDetail().then(data => { getProfileDetail().then(data => {
@ -35,6 +50,17 @@ function SettingsScreen({
}, []), }, []),
); );
const logout = () => {
const { clientId } = env.oAuthConfig;
const { access_token, refresh_token } = token;
logoutAsync({
client_id: clientId,
token: access_token,
refresh_token: refresh_token,
});
};
return ( return (
<View> <View>
<List px="0" py="0" borderWidth="0"> <List px="0" py="0" borderWidth="0">
@ -100,7 +126,7 @@ function SettingsScreen({
bg="danger.500" bg="danger.500"
style={{ borderRadius: 0 }} style={{ borderRadius: 0 }}
onPress={() => { onPress={() => {
logoutAsync(); logout();
}}> }}>
{i18n.t('AbpAccount::Logout')} {i18n.t('AbpAccount::Logout')}
</Button> </Button>

11
templates/app/react-native/src/store/actions/AppActions.js

@ -2,14 +2,21 @@ import { createAction } from '@reduxjs/toolkit';
const fetchAppConfigAsync = createAction( const fetchAppConfigAsync = createAction(
'app/fetchAppConfigAsync', 'app/fetchAppConfigAsync',
({ callback = () => {}, showLoading = true } = {}) => ({ payload: { callback, showLoading } }), ({ callback = () => {}, showLoading = true } = {}) => ({
payload: { callback, showLoading },
}),
); );
const setAppConfig = createAction('app/setAppConfig'); const setAppConfig = createAction('app/setAppConfig');
const setLanguageAsync = createAction('app/setLanguageAsync'); const setLanguageAsync = createAction('app/setLanguageAsync');
const logoutAsync = createAction('app/logoutAsync'); const logoutAsync = createAction(
'app/logoutAsync',
({ client_id = '', token = '', refresh_token = '' } = {}) => ({
payload: { client_id, token, refresh_token },
}),
);
export default { export default {
fetchAppConfigAsync, fetchAppConfigAsync,

26
templates/app/react-native/src/store/sagas/AppSaga.js

@ -6,10 +6,17 @@ import LoadingActions from '../actions/LoadingActions';
import PersistentStorageActions from '../actions/PersistentStorageActions'; import PersistentStorageActions from '../actions/PersistentStorageActions';
function* fetchAppConfig({ payload: { showLoading, callback } }) { function* fetchAppConfig({ payload: { showLoading, callback } }) {
if (showLoading) yield put(LoadingActions.start({ key: 'appConfig', opacity: 1 })); if (showLoading) {
yield put(LoadingActions.start({ key: 'appConfig', opacity: 1 }));
}
const data = yield call(getApplicationConfiguration); const data = yield call(getApplicationConfiguration);
yield put(AppActions.setAppConfig(data)); yield put(AppActions.setAppConfig(data));
yield put(PersistentStorageActions.setLanguage(data.localization.currentCulture.cultureName)); yield put(
PersistentStorageActions.setLanguage(
data.localization.currentCulture.cultureName,
),
);
if (showLoading) yield put(LoadingActions.stop({ key: 'appConfig' })); if (showLoading) yield put(LoadingActions.stop({ key: 'appConfig' }));
callback(); callback();
} }
@ -19,13 +26,22 @@ function* setLanguage(action) {
yield put(AppActions.fetchAppConfigAsync()); yield put(AppActions.fetchAppConfigAsync());
} }
function* logout() { function* logout({ payload: { client_id, token, refresh_token } }) {
yield call(Logout); const data = { client_id, token };
yield call(Logout, data);
if (!!refresh_token) {
data.token = refresh_token;
data.token_type_hint = 'refresh_token';
yield call(Logout, data);
}
yield put(PersistentStorageActions.setToken({})); yield put(PersistentStorageActions.setToken({}));
yield put(AppActions.fetchAppConfigAsync()); yield put(AppActions.fetchAppConfigAsync());
} }
export default function*() { export default function* () {
yield all([ yield all([
takeLatest(AppActions.setLanguageAsync.type, setLanguage), takeLatest(AppActions.setLanguageAsync.type, setLanguage),
takeLatest(AppActions.fetchAppConfigAsync.type, fetchAppConfig), takeLatest(AppActions.fetchAppConfigAsync.type, fetchAppConfig),

Loading…
Cancel
Save