javascriptfirebasereact-nativefirebase-authentication

Passing checkbox value to show / hide Password via react native


I'm using Firebase auth I will want to add a Check box, it will display the password in the password text box and hide it when it is clicked again

How to Passing checkbox value to show / hide Password?

This is my Login Page Code:

export default class Login extends Component {
    constructor(props) {
        super(props)
        this.state = {
            email: '',
            password: '',
            response: ''
        }
        this.signUp = this.signUp.bind(this)
        this.login = this.login.bind(this)
    }

    async signUp() {
        try {
            await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
            this.setState({
                response: 'Account Created!'
            })
            setTimeout(() => {
                this.props.navigator.push({
                    id: 'App'
                })
            }, 500)
        } catch (error) {
            this.setState({
                response: error.toString()
            })
        }
    }
    async login() {
        try {
            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            this.setState({
                response: 'user login in'
            })

            setTimeout(() => {
                this.props.navigator.push({
                    id: 'App'
                })
            })

        } catch (error) {
            this.setState({
                response: error.toString()
            })
        }
    }

    render() {
        return (
            <View style={styles.container}>
                <View style={styles.containerInputes}>
                    <TextInput
                        placeholderTextColor="gray"
                        placeholder="Email"
                        style={styles.inputText}
                        onChangeText={(email) => this.setState({ email })}
                    />
                    <TextInput
                        placeholderTextColor="gray"
                        placeholder="Password"
                        style={styles.inputText}
                        password={true}
                        secureTextEntry={true}
                        onChangeText={(password) => this.setState({ password })}
                    />
                </View>
                <TouchableHighlight
                    onPress={this.login}
                    style={[styles.loginButton, styles.button]}
                >
                    <Text
                        style={styles.textButton}
                    >Login</Text>
                </TouchableHighlight>
                <TouchableHighlight
                    onPress={this.signUp}
                    style={[styles.loginButton, styles.button]}
                >
                    <Text
                        style={styles.textButton}
                    >Signup</Text>
                </TouchableHighlight>
            </View>
        )
    }
}

Solution

  • One way of doing that is to set a state variable like showPassword and toggle it whenever the checkbox is checked. Like so:

    import React, { Component } from 'react';
    import {
      AppRegistry,
      Text,
      View, 
      TextInput,
      Switch
    } from 'react-native';
    
    export default class DemoProject extends Component {
      constructor(props) {
        super(props);
        this.toggleSwitch = this.toggleSwitch.bind(this);
        this.state = {
          showPassword: true,
        }
      }
    
      toggleSwitch() {
        this.setState({ showPassword: !this.state.showPassword });
      }
    
      render() {
        return (
          <View>
            <TextInput
              placeholderTextColor="gray"
              placeholder="Password"
              secureTextEntry={this.state.showPassword}
              onChangeText={(password) => this.setState({ password })}
            />
            <Switch
              onValueChange={this.toggleSwitch}
              value={!this.state.showPassword}
            /> 
            <Text>Show</Text>
          </View>
        )
      }
    }
    
    AppRegistry.registerComponent('DemoProject', () => DemoProject);
    

    NOTE: This won't work if you set the password prop!!!

    So just use a regular TextInput and utilize the secureTextEntry prop.