support backward compatibility with admin login

This commit is contained in:
Roland Osborne 2024-05-24 12:42:32 -07:00
parent 5bd9d80258
commit 8a9216a934
3 changed files with 19 additions and 10 deletions

View File

@ -2,8 +2,10 @@ import { useState, useEffect, useContext } from 'react';
import { useWindowDimensions } from 'react-native'; import { useWindowDimensions } from 'react-native';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { AppContext } from 'context/AppContext'; import { AppContext } from 'context/AppContext';
import { getNodeStatus } from 'api/getNodeStatus';
import { setNodeStatus } from 'api/setNodeStatus'; import { setNodeStatus } from 'api/setNodeStatus';
import { setNodeAccess } from 'api/setNodeAccess'; import { setNodeAccess } from 'api/setNodeAccess';
import { getNodeConfig } from 'api/getNodeConfig';
import { getLanguageStrings } from 'constants/Strings'; import { getLanguageStrings } from 'constants/Strings';
export function useAdmin() { export function useAdmin() {
@ -90,16 +92,23 @@ export function useAdmin() {
try { try {
const session = await setNodeAccess(node, state.token, state.mfaCode); const session = await setNodeAccess(node, state.token, state.mfaCode);
updateState({ server: node, busy: false }); updateState({ server: node, busy: false });
navigate('/dashboard', { state: { server: node, token: session }}); navigate('/dashboard', { state: { server: node, token: session, mfa: true }});
} }
catch (err) { catch (err) {
if (err.message == '405' || err.message == '403' || err.message == '429') { if (err.message == '405' || err.message == '403' || err.message == '429') {
updateState({ mfaModal: true, mfaError: err.message }); updateState({ mfaModal: true, mfaError: err.message });
} }
else { else {
console.log(err.message); try {
updateState({ busy: false, showAlert: true }); await getNodeConfig(node, state.token);
throw new Error('login failed'); updateState({ server: node, busy: false });
navigate('/dashboard', { state: { server: node, token: state.token, mfa: false }});
}
catch (err) {
console.log(err.message);
updateState({ busy: false, showAlert: true });
throw new Error('login failed');
}
} }
} }
} }

View File

@ -15,8 +15,8 @@ import { InputCode } from 'utils/InputCode';
export function Dashboard(props) { export function Dashboard(props) {
const location = useLocation(); const location = useLocation();
const { server, token } = location.state; const { server, token, mfa } = location.state;
const { state, actions } = useDashboard(server, token); const { state, actions } = useDashboard(server, token, mfa);
const enableMFA = async () => { const enableMFA = async () => {
try { try {
@ -121,12 +121,12 @@ export function Dashboard(props) {
<TouchableOpacity onPress={actions.showEditConfig}> <TouchableOpacity onPress={actions.showEditConfig}>
<AntIcon style={styles.icon} name={'setting'} size={20} /> <AntIcon style={styles.icon} name={'setting'} size={20} />
</TouchableOpacity> </TouchableOpacity>
{ !state.mfaEnabled && ( { !state.mfaEnabled && mfa && (
<TouchableOpacity onPress={enableMFA}> <TouchableOpacity onPress={enableMFA}>
<MatIcon style={styles.icon} name={'shield-lock-open-outline'} size={20} /> <MatIcon style={styles.icon} name={'shield-lock-open-outline'} size={20} />
</TouchableOpacity> </TouchableOpacity>
)} )}
{ state.mfaEnabled && ( { state.mfaEnabled && mfa && (
<TouchableOpacity onPress={disableMFA}> <TouchableOpacity onPress={disableMFA}>
<MatIcon style={styles.icon} name={'shield-lock-outline'} size={20} /> <MatIcon style={styles.icon} name={'shield-lock-outline'} size={20} />
</TouchableOpacity> </TouchableOpacity>

View File

@ -20,7 +20,7 @@ import { addAdminMFAuth } from 'api/addAdminMFAuth';
import { setAdminMFAuth } from 'api/setAdminMFAuth'; import { setAdminMFAuth } from 'api/setAdminMFAuth';
import { removeAdminMFAuth } from 'api/removeAdminMFAuth'; import { removeAdminMFAuth } from 'api/removeAdminMFAuth';
export function useDashboard(server, token) { export function useDashboard(server, token, mfa) {
const [state, setState] = useState({ const [state, setState] = useState({
strings: getLanguageStrings(), strings: getLanguageStrings(),
@ -74,7 +74,7 @@ export function useDashboard(server, token) {
} }
const syncNode = async () => { const syncNode = async () => {
const mfaEnabled = await getAdminMFAuth(server, token); const mfaEnabled = mfa ? await getAdminMFAuth(server, token) : false;
const config = await getNodeConfig(server, token); const config = await getNodeConfig(server, token);
const nodeAccounts = await getNodeAccounts(server, token); const nodeAccounts = await getNodeAccounts(server, token);
const accounts = nodeAccounts.map(setAccountItem); const accounts = nodeAccounts.map(setAccountItem);