들어가기
logout을 굳이 따로 정리할까 말까 고민하다가
token이 깔끔하게 지워지지가 않아서
고생을 해서
정리하기로 함
import {
makeVar,
ApolloClient,
InMemoryCache,
createHttpLink,
} from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
import { offsetLimitPagination } from '@apollo/client/utilities'
import AsyncStorage from '@react-native-async-storage/async-storage'
export const isLoggedInVar = makeVar(false)
export const tokenVar = makeVar('')
export const TOKEN = 'token'
export const logUserIn = async (token) => {
await AsyncStorage.setItem(TOKEN, token)
isLoggedInVar(true)
tokenVar(token)
}
///AsyncStorage에서 TOKEN을 지워주고, loggedInVar를 false로
///그리고 가장 중요한 tokenVar를 null로 바꿔줌.
export const logUserOut = async () => {
await AsyncStorage.removeItem(TOKEN)
isLoggedInVar(false)
tokenVar(null)
}
.
.
.
import React from 'react'
import { Text, View } from 'react-native'
import styled from 'styled-components/native'
import { logUserOut } from '../apollo'
const Button = styled.TouchableOpacity`
background-color: tomato;
padding: 13px 10px;
border-radius: 3px;
width: 100%;
opacity: ${(props) => (props.disabled ? '0.5' : '1')};
`
export default function Search() {
return (
<View
style={{
backgroundColor: 'black',
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}
>
<Text style={{ color: 'white' }}>Search</Text>
<Button onPress={()=>logUserOut()}> ///실행되는 부분 참고할 것,반드시 함수형태로 코딩할 것!!
<Text>Logout</Text>
</Button>
</View>
)
}