Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authentication: added logic for login and signup #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ npm-debug.*
*.mobileprovision
*.orig.*
web-build/
<<<<<<< HEAD
web-report/

# OSX
#
=======
env
.env

secret/
coverage/

# macOS
>>>>>>> ee31d0c0b1df76c426a6e045192669acdc820312
.DS_Store

# Xcode
Expand Down
202 changes: 122 additions & 80 deletions app/(Auth)/LogInModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Text, View, StyleSheet, Pressable, Modal } from "react-native"
import React, { useState } from "react";
import { TextInput as PaperTextInput, IconButton } from 'react-native-paper';
import { TextInput as PaperTextInput, IconButton, Props } from 'react-native-paper';

import { AuthError, User } from "firebase/auth";

import { emailVerification, logIn, logOut } from "./firebaseAuth";

type LogInProps = {
isVisible: boolean,
Expand All @@ -17,87 +21,106 @@ const LogInModal = (props: LogInProps) => {
const [emailBorderColor, setEmailBorderColor] = useState('gray');
const [PasswordBorderColor, setPasswordBorderColor] = useState('gray');

const handleLogIn = async () => {
try {
const user = await logIn(emailText, passwordText)
await checkIfEmailVerified(user, props.onClose)

} catch (error: unknown) {
if (
(error as AuthError).code === "auth/user-not-found" ||
(error as AuthError).code === "auth/wrong-password"
) {
alert('Email already in use');
} else if ((error as AuthError).code === "auth/too-many-request") {
alert("Too many unsuccessful login attempts. Please try again later.")
} else {
alert("Sign In error: " + (error as Error).message)
}
}
}

return (
<Modal
transparent={true}
animationType="fade"
visible={props.isVisible}
onRequestClose={props.onClose}
>
<Pressable style={styles.container} onPress={props.onClose}>
<Pressable style={styles.box} onPress={(e) => e.stopPropagation()} >
<View style={styles.header}>
<IconButton icon="close" size={24} onPress={props.onClose} />
</View>
<Text style={styles.label}>Email</Text>
<PaperTextInput
style={[styles.paperInput, { borderColor: emailBorderColor }]}
onFocus={() => setEmailBorderColor('black')} // border color on focus
onBlur={() => setEmailBorderColor('gray')} // border color on focus

placeholder='Value'
placeholderTextColor="#a9a9a9"
value={emailText}
onChangeText={setEmailText}

theme={{ colors: { primary: "transparent" } }} // this removes the underline
underlineColor="transparent" // this removes the any extra underline
/* obtain data here */
/>
<Text style={styles.label}>Password</Text>
<PaperTextInput
style={[styles.paperInput, { borderColor: PasswordBorderColor }]}
onFocus={() => setPasswordBorderColor('black')} // border color on focus
onBlur={() => setPasswordBorderColor('gray')} // border color on focus

placeholder='Value'
placeholderTextColor="#a9a9a9"
secureTextEntry={passwordVisible}
value={passwordText}
onChangeText={setPasswordText}
right={
<PaperTextInput.Icon
icon={passwordVisible ? 'eye' : 'eye-off'}
onPress={() => setPasswordVisible(!passwordVisible)}
style={styles.icon} // this adjusts eye icon position
/>
}
theme={{ colors: { primary: "transparent" } }} // this removes the underline
underlineColor="transparent" // this removes any extra underline
/* obtain data here */
/>
<Pressable
style={styles.button}
onPress={() => {
/* handle action here */
}}
>
<Text style={styles.buttonText}>Sign In</Text>
</Pressable>
<Text
style={styles.textSpace}>
<Text
style={styles.underline}
onPress={() => {
/* handle action here */
}}
>
Forgot password?
</Text>
</Text>
<Text
style={styles.underline}
onPress={() => {
props.toggleSignUp()
console.log("Signup Pressed")
props.onClose()
}}
>
Need an account? Sign-up here.
</Text>
</Pressable>
<Modal
transparent={true}
animationType="fade"
visible={props.isVisible}
onRequestClose={props.onClose}
>
<Pressable style={styles.container} onPress={props.onClose}>
<Pressable style={styles.box} onPress={(e) => e.stopPropagation()} >
<View style={styles.header}>
<IconButton icon="close" size={24} onPress={props.onClose} />
</View>
<Text style={styles.label}>Email</Text>
<PaperTextInput
style={[styles.paperInput, { borderColor: emailBorderColor }]}
onFocus={() => setEmailBorderColor('black')} // border color on focus
onBlur={() => setEmailBorderColor('gray')} // border color on focus

placeholder='Value'
placeholderTextColor="#a9a9a9"
value={emailText}
onChangeText={setEmailText}

theme={{ colors: { primary: "transparent" } }} // this removes the underline
underlineColor="transparent" // this removes the any extra underline
/* obtain data here */
/>
<Text style={styles.label}>Password</Text>
<PaperTextInput
style={[styles.paperInput, { borderColor: PasswordBorderColor }]}
onFocus={() => setPasswordBorderColor('black')} // border color on focus
onBlur={() => setPasswordBorderColor('gray')} // border color on focus

placeholder='Value'
placeholderTextColor="#a9a9a9"
secureTextEntry={passwordVisible}
value={passwordText}
onChangeText={setPasswordText}
right={
<PaperTextInput.Icon
icon={passwordVisible ? 'eye' : 'eye-off'}
onPress={() => setPasswordVisible(!passwordVisible)}
style={styles.icon} // this adjusts eye icon position
/>
}
theme={{ colors: { primary: "transparent" } }} // this removes the underline
underlineColor="transparent" // this removes any extra underline
/* obtain data here */
/>
<Pressable
style={styles.button}
onPress={() => {
handleLogIn()
}}
>
<Text style={styles.buttonText}>Sign In</Text>
</Pressable>
</Modal>
<Text
style={styles.textSpace}>
<Text
style={styles.underline}
onPress={() => {
/* handle action here */
}}
>
Forgot password?
</Text>
</Text>
<Text
style={styles.underline}
onPress={() => {
props.toggleSignUp()
console.log("Signup Pressed")
props.onClose()
}}
>
Need an account? Sign-up here.
</Text>
</Pressable>
</Pressable>
</Modal>
);
};

Expand Down Expand Up @@ -167,6 +190,25 @@ const styles = StyleSheet.create({

export default LogInModal;

const checkIfEmailVerified = async (user: User, onClose: () => void) => {
if(user) {
if(!user.emailVerified) {
// means that user is still not verified yet, need them to be verified
await emailVerification()
await logOut();

//give a warning like an alert to ask to verify
alert("Please verify your email.");
}
//If verified, navigate to other place
console.log("Woo Verified, I'll navigate you later")

onClose();
} else {
throw new Error("Failed to check User")
}
}

/* self-note:
when clocking the navigation 'link',
it erases the most components from the main background. figure out how to prevent that.
Expand Down
Loading
Loading