-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
98 lines (81 loc) · 2.71 KB
/
App.js
File metadata and controls
98 lines (81 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React, { useState, useEffect } from 'react';
import { ActivityIndicator, StyleSheet, Text, View, Button } from 'react-native';
import { Amplify } from 'aws-amplify';
import { getCurrentUser } from '@aws-amplify/auth';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import SignIn from './src/screens/SignIn';
import SignUp from './src/screens/SignUp';
import ConfirmSignUp from './src/screens/ConfirmSignUp';
import Home from './src/screens/Home';
import awsconfig from './src/aws-exports';
Amplify.configure(awsconfig);
const AuthenticationStack = createStackNavigator();
const AppStack = createStackNavigator();
// 로그인하지 않았거나 로그아웃한 경우 -> SignIn
const AuthenticationNavigator = props => {
return (
<AuthenticationStack.Navigator screenOptions={{ headerShown: false }}>
<AuthenticationStack.Screen name="SignIn">
{screenProps => (
<SignIn {...screenProps} updateAuthState={props.updateAuthState} />
)}
</AuthenticationStack.Screen>
<AuthenticationStack.Screen name="SignUp" component={SignUp} />
<AuthenticationStack.Screen
name="ConfirmSignUp"
component={ConfirmSignUp}
/>
</AuthenticationStack.Navigator>
);
};
// 성공적으로 로그인한 경우 -> Home
const AppNavigator = props => {
return (
<AppStack.Navigator screenOptions={{ headerShown: false }}>
<AppStack.Screen name="Home">
{screenProps => (
<Home {...screenProps} updateAuthState={props.updateAuthState} />
)}
</AppStack.Screen>
</AppStack.Navigator>
);
};
const Initializing = () => {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<ActivityIndicator size="large" color="tomato" />
</View>
);
};
function App() {
const [isUserLoggedIn, setUserLoggedIn] = useState('initializing');
useEffect(() => {
checkAuthState();
}, []);
async function checkAuthState() {
try {
await getCurrentUser();
console.log('✅ User is signed in');
setUserLoggedIn('loggedIn');
} catch (err) {
console.log('❌ User is not signed in');
setUserLoggedIn('loggedOut');
}
}
function updateAuthState(isUserLoggedIn) {
setUserLoggedIn(isUserLoggedIn);
}
return (
<NavigationContainer>
{isUserLoggedIn === 'initializing' && <Initializing />}
{isUserLoggedIn === 'loggedIn' && (
<AppNavigator updateAuthState={updateAuthState} />
)}
{isUserLoggedIn === 'loggedOut' && (
<AuthenticationNavigator updateAuthState={updateAuthState} />
)}
</NavigationContainer>
);
}
export default App;