Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.0 kB
3
Indexable
Never
const ActivateScreen = ({ navigation }) => {
  const [code, setCode] = useState()
  const [token, setToken] = useState('')

  const fetchToken = async () => {
    fetch('http://192.168.1.60:5000/api/user/activate', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        code:code
      })
      }).then((response) => {
        return response.json();
      }).then((responseJSON) => {
        if (responseJSON.error){
          Alert.alert("Неверный код! Попробуйте еще раз.")
        } else if (responseJSON.token) {
          const tkn = responseJSON.token;
          save(tkn);
          console.warn(token)
          storeToken();
          
        }
      })
  }
  const save = () => {
    setToken(token)
  }

  const storeToken = async () => {
    await AsyncStorage.setItem("token", token);
    const tok = await AsyncStorage.getItem("token")
    console.warn(tok);
    navigation.replace('Profile');
  }

  return(
    <View style={styles.container}>
        <Image style={styles.logo} source = {require('./assets/logo.png')} />

        <StatusBar style="auto" />
        
        <View style={styles.inputView}>
          <Text style={
            {
              fontSize:11.5,
              textAlign:'center',
              opacity:0.3,
            }
          }>Код-пароль был отправлен {"\n"}на ваш номер телефона</Text>
          <TextInput
          onChangeText={(value) => setCode(value)}
          value={code}
          keyboardType={'phone-pad'} placeholder={'XXXXXX'} style={styles.input}></TextInput>
          <TouchableOpacity
            style={styles.button}
            onPress={fetchToken}
          >
            <Text style={{color: '#fff'}} >Войти</Text>
          </TouchableOpacity>
        </View>
      </View>
  );
}