2 years ago

#65366

test-img

Benjamin Ikwuagwu

Display username when user login in react native

I am not sure if am doing the right thing in my code, but I want to display username in my profile page/screen when user login. I am learning react native and I know am missing something that is making my code not to work. Please I need help on how to display username on my profile screen. Here's the code for my login screen: loginScreen.js

import React, {useState, useEffect} from 'react';
import {
  View,
  TextInput,
  StyleSheet,
  Pressable,
  Text,
  Alert,
} from 'react-native';
import {Voximplant} from 'react-native-voximplant';
import {APP_NAME, ACC_NAME} from '../Constants';
import {useNavigation} from '@react-navigation/core';
import Parse from 'react-native-parse'

const LoginScreen = () => {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  const voximplant = Voximplant.getInstance();

  const navigation = useNavigation();

  useEffect(() => {
    const connect = async () => {
      const status = await voximplant.getClientState();
      console.log(status);
      if (status === Voximplant.ClientState.DISCONNECTED) {
        await voximplant.connect();
      } else if (status === Voximplant.ClientState.LOGGED_IN) {
        redirectHome();
      }
    };

    const getCurrentUser = async () => {
      if (username === '') {
        const currentUser = await Parse.User.currentAsync();
        if (currentUser != null) {
          setUsername(currentUser.getUsername());
        }
      }
    }
    getCurrentUser();

    connect();
  }, [username]);

  const signIn = async () => {
    try {
      const fqUsername = `${username}@${APP_NAME}.${ACC_NAME}.voximplant.com`;
      await voximplant.login(fqUsername, password);

      redirectHome();
    } catch (e) {
      console.log(e);
      Alert.alert(e.name, `Error Code: ${e.code}`);
    }
  };

  const redirectHome = () => {
    navigation.reset({
      index: 0,
      routes: [
        {
          name: 'Profile',
        },
      ],
    });
  };

  return (
    <View style={styles.page}>
      <TextInput
        value={username}
        onChangeText={setUsername}
        placeholder="username"
        style={styles.input}
        autoCapitalize="none"
      />
      <TextInput
        value={password}
        onChangeText={setPassword}
        placeholder="password"
        style={styles.input}
        secureTextEntry
      />

      <Pressable style={styles.button} onPress={signIn}>
        <Text style={{alignSelf: 'center'}}>Sign In</Text>
      </Pressable>
    </View>
  );
};

And this is my profile screen: Profile

import React from 'react';
import { View, Text } from 'react-native';
// import {useRoute} from '@react-navigation/core';

const ProfileScreen = ({username}) => {
    return (
        <View>
            <Text style={{ justifyContent: 'center'}}>{username}</Text>
        </View>
    );
};

export default ProfileScreen;

javascript

reactjs

react-native

voximplant

0 Answers

Your Answer

Accepted video resources