-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
162 lines (152 loc) · 4.05 KB
/
App.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React, { useEffect, useMemo, useReducer } from 'react';
import { StyleSheet, View, Text, Alert } from 'react-native';
import Navigation from './src/navigation/navigation';
import AsyncStorage from '@react-native-community/async-storage';
import Auth_Nav from './src/navigation/auth_nav';
import { AuthContext } from './src/services/AuthContext';
import axios from './src/axios/api'
const App = () => {
useEffect(() => {
setTimeout(async () => {
let token = null;
try {
token = await AsyncStorage.getItem('token')
} catch (err) {
console.log(err)
}
dispatch({type: 'RETRIEVE_TOKEN', token: token})
}, 1000)
}, [])
const authContextValue = useMemo(() => ({
signIn: async (trainerId, password) => {
axios.post('/auth/login',
{
trainerId,
password,
}).then(async (res) => {
try {
await AsyncStorage.setItem('token', res.data.token)
token = await AsyncStorage.getItem('token')
dispatch({type: 'LOGIN', token})
} catch (error) {
console.log(error)
}
}).catch(error => {
const errJson = JSON.parse(error.response.request._response)
const message = errJson.msg || errJson.message
Alert.alert(message)
console.log(message === '인증에 실패하였습니다')
if (message === '인증에 실패하였습니다') {
return String(trainerId)
} else {
throw new Error(message)
}
})
},
signUp: (name, trainerId, password) => {
axios.post('/auth/register',
{
name,
trainerId,
password,
}).then((res) => {
Alert.alert('인증코드가 발송되었습니다.')
}).catch(error => {
const errJson = JSON.parse(error.response.request._response)
const message = errJson.msg || errJson.message
Alert.alert(message)
return errJson
})
},
confirmSecret: (trainerId, secretText) => {
axios.post('/auth/confirmsecret',
{
trainerId,
secret: secretText,
}).then((res) => {
console.log(res.data)
Alert.alert('회원가입에 성공했습니다. 로그인 해주세요.')
}).catch(error => {
const errJson = JSON.parse(error.response.request._response)
console.log(errJson)
Alert.alert(errJson.message)
})
}
,
signOut: async () => {
try {
await AsyncStorage.removeItem('token')
} catch (err) {
console.log(err)
}
dispatch({type: 'LOGOUT'})
}
,
setTraineeIdGlobal: async (traineeId) => {
try {
await AsyncStorage.setItem('traineeId', traineeId)
} catch (err) {
console.log(err)
}
//dispatch({type: 'SET_TRAINEE_ID_GLOBAL', traineeId})
}
}), [])
const initialAuthState = {
isLoading: true,
token: null,
traineeId: null,
}
const AuthReducer = (prevState, action) => {
switch( action.type ) {
case 'RETRIEVE_TOKEN':
return {
...prevState,
token: action.token,
isLoading: false,
}
case 'LOGIN':
return {
...prevState,
token: action.token,
isLoading: false,
}
case 'LOGOUT':
return {
...prevState,
token: null,
isLoading: false,
}
case 'SET_TRAINEE_ID_GLOBAL':
return {
...prevState,
traineeId: action.traineeId,
}
}
}
const [authState, dispatch] = useReducer(AuthReducer, initialAuthState)
if (authState.isLoading) {
return (
<View style={{justifyContent:'center', alignItems:'center'}}>
<Text>This is loading...</Text>
</View>
)
}
const NavController = () => {
const isLoggedIn = authState.token
//console.log(`this is id: ${authState.traineeId}`)
return isLoggedIn !== null ? <Navigation /> : <Auth_Nav />
}
return (
<AuthContext.Provider value={authContextValue}>
<View style={styles.container}>
<NavController />
</View>
</AuthContext.Provider>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default App