Skip to content

Commit

Permalink
Fix login/sign with email & password
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaman190 committed Mar 13, 2023
1 parent 3a05135 commit d9456d6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 97 deletions.
76 changes: 31 additions & 45 deletions app/screens/LoginScreen/loginScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,56 @@ import {
ScrollView,
} from "react-native";
import { AccessToken, LoginManager } from "react-native-fbsdk";
import { f, auth } from "../../../config/config.js";
import { app, auth, db } from "../../../config/config.js";
import { signInWithEmailAndPassword, onAuthStateChanged } from "firebase/auth";
import * as EmailValidator from "email-validator";
import styles from "./style";
import { SocialIcon } from "react-native-elements";
import styles from "./style";

export default class LoginScreen extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
Password: "",
password: "",
};
}

componentDidMount() {
var that = this;

auth.onAuthStateChanged(function (user) {
onAuthStateChanged(auth, function (user) {
if (user) {
that.redirectUser();
}
});
}

login() {
let email = this.state.email;
let password = this.state.Password;

let { navigate } = this.props.navigation;

auth
.signInWithEmailAndPassword(email, password)
.then(function (data) {
navigate("App");
})
.catch(function (error) {
var errorMessage = error.message;
alert(errorMessage.toString());
});
}

redirectUser() {
const { navigate } = this.props.navigation;
navigate("App");
}

_signInAsync = async () => {
if (EmailValidator.validate(this.state.email) === true) {
if (this.state.Pasword != "") {
this.login();
} else {
alert("Enter the password");
}
async login() {
try {
let email = this.state.email;
let password = this.state.password;

const res = await signInWithEmailAndPassword(auth, email, password);
console.log("User signed in successfully!");
} catch (error) {
alert(error.message.toString());
}
}

async _signInAsync() {
if (!EmailValidator.validate(this.state.email)) {
alert("Please enter a valid email!");
} else if (this.state.password == "") {
alert("Enter your password!");
} else {
alert("Please enter A Valid Email");
this.login();
}
};
}

onPressLogin() {
LoginManager.logInWithReadPermissions(["public_profile", "email"]).then(
Expand Down Expand Up @@ -124,17 +118,9 @@ export default class LoginScreen extends Component {
.update({ ...userData, ...defaults });
};

_signInAsync = async () => {
if (EmailValidator.validate(this.state.email) === true) {
if (this.state.Pasword != "") {
this.login();
} else {
alert("Enter the password");
}
} else {
alert("Please enter A Valid Email");
}
};
handleInput(input, text) {
this.setState(prevState => ({ ...prevState, [input]: text }));
}

render() {
return (
Expand All @@ -151,7 +137,7 @@ export default class LoginScreen extends Component {
keyboardType="email-address"
placeholderTextColor="rgba(255,255,255,0.7)"
style={styles.input}
onChangeText={text => this.setState({ email: text })}
onChangeText={text => this.handleInput("email", text)}
ref={input => {
this.textInput = input;
}}
Expand All @@ -161,14 +147,14 @@ export default class LoginScreen extends Component {
secureTextEntry={true}
placeholderTextColor="rgba(255,255,255,0.7)"
style={styles.input}
onChangeText={text => this.setState({ Password: text })}
onChangeText={text => this.handleInput("password", text)}
ref={input => {
this.textInput = input;
}}
/>
</View>
</KeyboardAvoidingView>
<TouchableOpacity onPress={this._signInAsync} style={styles.loginButton}>
<TouchableOpacity onPress={this._signInAsync.bind(this)} style={styles.loginButton}>
<Text style={styles.buttonText}>Sign In</Text>
</TouchableOpacity>

Expand Down
95 changes: 43 additions & 52 deletions app/screens/SignupScreen/signupScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
import styles from "./style";
import * as EmailValidator from "email-validator";
import { AccessToken, LoginManager } from "react-native-fbsdk";
import { f, auth } from "../../../config/config.js";
import { auth, db } from "../../../config/config.js";
import { createUserWithEmailAndPassword, onAuthStateChanged, updateProfile } from "firebase/auth";
import { SocialIcon } from "react-native-elements";

export default class SignUpScreen extends Component {
Expand All @@ -21,14 +22,14 @@ export default class SignUpScreen extends Component {
this.state = {
email: "",
name: "",
Password: "",
ConfirmPassword: "",
password: "",
confirmPassword: "",
};
}

componentDidMount() {
var that = this;
auth.onAuthStateChanged(function (user) {
onAuthStateChanged(auth, function (user) {
if (user) {
that.redirectUser();
}
Expand All @@ -40,6 +41,35 @@ export default class SignUpScreen extends Component {
navigate("App");
}

async signup() {
try {
let name = this.state.name;
let email = this.state.email;
let password = this.state.password;

const { user } = await createUserWithEmailAndPassword(auth, email, password);
await updateProfile(user, { displayName: name });
console.log("\n\nUpdated user data successfully! : ", auth.currentUser);
} catch (error) {
console.log(error);
alert(error.message.toString());
}
}

async _signUpAsync() {
if (this.state.name == "") {
alert("Please enter your name!");
} else if (!EmailValidator.validate(this.state.email)) {
alert("Please enter a valid email!");
} else if (this.state.password == "") {
alert("Please enter a password!");
} else if (this.state.password != this.state.confirmPassword) {
alert("Password and Confirm password must be same");
} else {
this.signup();
}
}

onPressLogin() {
LoginManager.logInWithReadPermissions(["public_profile", "email"]).then(
result => this._handleCallBack(result),
Expand Down Expand Up @@ -97,6 +127,10 @@ export default class SignUpScreen extends Component {
.update({ ...userData, ...defaults });
};

handleInput(input, text) {
this.setState(prevState => ({ ...prevState, [input]: text }));
}

render() {
return (
<View style={styles.firstContainer}>
Expand All @@ -111,32 +145,32 @@ export default class SignUpScreen extends Component {
placeholder="Name"
placeholderTextColor="rgba(255,255,255,0.7)"
style={styles.input}
onChangeText={text => this.setState({ name: text })}
onChangeText={text => this.handleInput("name", text)}
/>
<TextInput
placeholder="Email"
keyboardType="email-address"
placeholderTextColor="rgba(255,255,255,0.7)"
style={styles.input}
onChangeText={text => this.setState({ email: text })}
onChangeText={text => this.handleInput("email", text)}
/>
<TextInput
placeholder="Pasword"
secureTextEntry={true}
placeholderTextColor="rgba(255,255,255,0.7)"
style={styles.input}
onChangeText={text => this.setState({ Password: text })}
onChangeText={text => this.handleInput("password", text)}
/>
<TextInput
placeholder="Confirm Pasword"
secureTextEntry={true}
placeholderTextColor="rgba(255,255,255,0.7)"
style={styles.input}
onChangeText={text => this.setState({ ConfirmPassword: text })}
onChangeText={text => this.handleInput("confirmPassword", text)}
/>
</View>
</KeyboardAvoidingView>
<TouchableOpacity onPress={this.signUpAsync} style={styles.loginButton}>
<TouchableOpacity onPress={this._signUpAsync.bind(this)} style={styles.loginButton}>
<Text style={styles.buttonText}>Sign Up</Text>
</TouchableOpacity>

Expand Down Expand Up @@ -166,47 +200,4 @@ export default class SignUpScreen extends Component {
</View>
);
}
register() {
let email = this.state.email;
let name = this.state.name;
let password = this.state.Password;

const { navigate } = this.props.navigation;

auth
.createUserWithEmailAndPassword(email, password)
.then(function (data) {
data.user
.updateProfile({
displayName: name,
})
.then(
function () {
console.log("Updated User Data..");
},
function (error) {
console.log("Error Updating User Data.." + error);
}
);
alert("Welcome to Go Social!");
navigate("App");
})
.catch(function (error) {
var errorMessage = error.message;
console.log("Error = " + errorMessage);
alert(errorMessage);
});
}

signUpAsync = async () => {
if (EmailValidator.validate(this.state.email) === true) {
if (this.state.Password === this.state.ConfirmPassword) {
this.register();
} else {
alert("password Missmatch");
}
} else {
alert("Please enter A Valid Email");
}
};
}

0 comments on commit d9456d6

Please sign in to comment.