Skip to content

Commit

Permalink
feat: auto sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
timepresent95 committed Oct 11, 2024
1 parent 4011b3b commit a54a098
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
7 changes: 0 additions & 7 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { QueryClientProvider } from '@tanstack/react-query';
import React, { useEffect, useState } from 'react';
import ReactNativeModule, { NativeModules } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import SplashScreen from 'react-native-splash-screen';

import queryClient from '@/api/queryClient';
import ConfirmModal from '@/entities/ConfirmModal';
Expand All @@ -26,12 +25,6 @@ FirebaseMessagingModule.eventsAddListener('messaging_message_received');
export default function App() {
const [isMockingEnabled, setMockingEnabled] = useState(false);

useEffect(() => {
setTimeout(() => {
SplashScreen.hide();
}, 300); //스플래시 활성화 시간
});

useEffect(() => {
const eventEmitter = new ReactNativeModule.NativeEventEmitter(
FirebaseEventModule,
Expand Down
28 changes: 28 additions & 0 deletions src/business/hooks/useInitApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useEffect } from 'react';
import SplashScreen from 'react-native-splash-screen';

import { useGetUserInfo } from '@/store/queries/user';
import useCredentialStore from '@/store/stores/credentialStore';
import { getEncryptStorage } from '@/utils/encryptStorage';

export default function useInitApp() {
const { setToken } = useCredentialStore();
const { data: userInfo, refetch: refetchUserInfo } = useGetUserInfo();

useEffect(() => {
(async () => {
const credentials = await getEncryptStorage('credential_token');

if (credentials) {
setToken(credentials);
refetchUserInfo();
}

setTimeout(() => {
SplashScreen.hide();
}, 100);
})();
}, [setToken, refetchUserInfo]);

return { isAuthroized: !!userInfo };
}
7 changes: 4 additions & 3 deletions src/navigations/RootNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import React from 'react';

import DefaultTabNavigator from './DefaultTabNavigator';

import useInitApp from '@/business/hooks/useInitApp';
import AuthStackNavigator from '@/navigations/stack/AuthStackNavigator';
import ErrorScreen from '@/screen/ErrorScreen';
import { useAuthStore, useErrorStore } from '@/store/stores';
import { useErrorStore } from '@/store/stores';

export default function RootNavigator() {
const { isLogin } = useAuthStore();
const { isAuthroized } = useInitApp();
const { errorType } = useErrorStore();

if (errorType) {
return <ErrorScreen type={400} />;
}

return <>{isLogin ? <DefaultTabNavigator /> : <AuthStackNavigator />}</>;
return <>{isAuthroized ? <DefaultTabNavigator /> : <AuthStackNavigator />}</>;
}
3 changes: 3 additions & 0 deletions src/store/queries/user/useUserInfoQuery.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { useQuery } from '@tanstack/react-query';

import { getMy } from '@/api/my';
import useCredentialStore from '@/store/stores/credentialStore';

export const userQueryKeys = {
my: () => ['user_my'],
};

export function useGetUserInfo() {
const { accessToken, refreshToken } = useCredentialStore();
return useQuery({
queryKey: userQueryKeys.my(),
queryFn: () => getMy(),
staleTime: 1000 * 60 * 60 * 24,
enabled: !!accessToken && !!refreshToken,
});
}

0 comments on commit a54a098

Please sign in to comment.