Refactor getAccessToken to return refreshToken

This commit is contained in:
Arnab Chakraborty
2024-09-18 00:37:13 -04:00
parent 4e4e964d48
commit 337c7c2025
5 changed files with 21 additions and 7 deletions

View File

@@ -7,7 +7,7 @@ import DeviceInfo from 'react-native-device-info';
import { ScrollView } from 'react-native-gesture-handler';
import { HardwareModel, NodeState, StatisticsResponse, useBridgeQuery } from '@sd/client';
import { tw, twStyle } from '~/lib/tailwind';
import { getAccessToken } from '~/utils';
import { getTokens } from '~/utils';
import Fade from '../layout/Fade';
import { Button } from '../primitive/Button';
@@ -48,8 +48,8 @@ const Devices = ({ node, stats }: Props) => {
const [accessToken, setAccessToken] = useState<string>('');
useEffect(() => {
(async () => {
const at = await getAccessToken();
setAccessToken(at ?? '');
const at = await getTokens();
setAccessToken(at.accessToken);
})();
}, []);

View File

@@ -71,6 +71,7 @@ async function signInClicked(
toast.success('Sign in successful');
// Save the access token to AsyncStorage, because SuperTokens doesn't store it correctly. Thanks to the React Native SDK.
await AsyncStorage.setItem('access_token', req.headers.get('st-access-token')!);
await AsyncStorage.setItem('refresh_token', req.headers.get('st-refresh-token')!);
// Refresh the page to show the user is logged in
navigator.navigate('AccountProfile');
}

View File

@@ -13,12 +13,14 @@ import Card from '~/components/layout/Card';
import { Button } from '~/components/primitive/Button';
import { tw } from '~/lib/tailwind';
import { SettingsStackScreenProps } from '~/navigation/tabs/SettingsStack';
import { getTokens } from '~/utils';
const DebugScreen = ({ navigation }: SettingsStackScreenProps<'Debug'>) => {
const debugState = useDebugState();
const featureFlags = useFeatureFlags();
const origin = useBridgeQuery(['cloud.getApiOrigin']);
const setOrigin = useBridgeMutation(['cloud.setApiOrigin']);
const cloudBootstrap = useBridgeMutation(['cloud.bootstrap']);
const queryClient = useQueryClient();
@@ -71,6 +73,14 @@ const DebugScreen = ({ navigation }: SettingsStackScreenProps<'Debug'>) => {
>
<Text style={tw`text-ink`}>Logout</Text>
</Button>
<Button
onPress={async () => {
const tokens = await getTokens();
cloudBootstrap.mutate([tokens.accessToken, tokens.refreshToken]);
}}
>
<Text style={tw`text-ink`}>Cloud Bootstrap</Text>
</Button>
</Card>
</View>
);

View File

@@ -1,8 +1,12 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
export async function getAccessToken() {
const fetched = await AsyncStorage.getItem('access_token');
return fetched;
export async function getTokens() {
const fetchedToken = await AsyncStorage.getItem('access_token');
const fetchedRefreshToken = await AsyncStorage.getItem('refresh_token');
return {
accessToken: fetchedToken ?? '',
refreshToken: fetchedRefreshToken ?? ''
};
}
export const AUTH_SERVER_URL = __DEV__ ? 'http://localhost:9420' : 'https://auth.spacedrive.com';

View File

@@ -1,5 +1,4 @@
import { Envelope } from '@phosphor-icons/react';
import { getAccessToken } from 'supertokens-web-js/recipe/session';
import { useBridgeMutation, useBridgeQuery } from '@sd/client';
import { Button, Card } from '@sd/ui';
import StatCard from '~/app/$libraryId/overview/StatCard';