Skip to content

Commit

Permalink
Implemented account creation only after onboarding (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
notforrest authored Sep 4, 2024
1 parent 8d133d2 commit 4148525
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
27 changes: 24 additions & 3 deletions app/(tabs)/account/choose-full-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ERROR_MESSAGES } from "../../../utils/ErrorMessages";

export default function ChooseFullName() {
const styles = getStyles(useTheme());
const { userId } = useLocalSearchParams();
const { userId, username, email, password } = useLocalSearchParams();

const [fullName, setFullName] = useState("");
const [errorMessage, setErrorMessage] = useState(" ");
Expand All @@ -27,7 +27,16 @@ export default function ChooseFullName() {
<Text style={styles.errorText}>{errorMessage}</Text>
<TouchableOpacity
disabled={!fullName}
onPress={() => handleSubmitFullName(userId, fullName, setErrorMessage)}
onPress={() =>
handleSubmitFullName(
userId,
username,
fullName,
email,
password,
setErrorMessage,
)
}
style={styles.button}
>
<Text style={fullName ? styles.buttonText : styles.buttonDisabled}>
Expand All @@ -40,7 +49,10 @@ export default function ChooseFullName() {

const handleSubmitFullName = async (
userId: string | string[],
username: string | string[],
fullName: string,
email: string | string[],
password: string | string[],
setErrorMessage: (message: string) => void,
) => {
try {
Expand All @@ -49,13 +61,22 @@ const handleSubmitFullName = async (
return;
}

const {
data: { session },
} = await supabase.auth.signUp({
email: email as string,
password: password as string,
});

const { error } = await supabase.from("profiles").upsert([
{
id: userId,
id: session?.user.id,
updated_at: new Date(),
username: username,
full_name: fullName,
},
]);

if (error) {
throw error;
} else {
Expand Down
35 changes: 19 additions & 16 deletions app/(tabs)/account/choose-username.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { router, useLocalSearchParams } from "expo-router";
import React, { useState } from "react";
import { StyleSheet, Text, TextInput, View } from "react-native";
import { Theme, useTheme } from "../../../utils/ThemeProvider";
import { supabase } from "../../../utils/supabase";
import { TouchableOpacity } from "react-native-gesture-handler";
import { ERROR_MESSAGES } from "../../../utils/ErrorMessages";

export default function ChooseUsername() {
const styles = getStyles(useTheme());
const { userId } = useLocalSearchParams();
const { userId, email, password } = useLocalSearchParams();

const [username, setUsername] = useState("");
const [errorMessage, setErrorMessage] = useState(" ");
Expand All @@ -27,7 +26,15 @@ export default function ChooseUsername() {
<Text style={styles.errorText}>{errorMessage}</Text>
<TouchableOpacity
disabled={!username}
onPress={() => handleSubmitUsername(userId, username, setErrorMessage)}
onPress={() =>
handleSubmitUsername(
userId,
username,
email,
password,
setErrorMessage,
)
}
style={styles.button}
>
<Text style={username ? styles.buttonText : styles.buttonDisabled}>
Expand All @@ -41,6 +48,8 @@ export default function ChooseUsername() {
const handleSubmitUsername = async (
userId: string | string[],
username: string,
email: string | string[],
password: string | string[],
setErrorMessage: (message: string) => void,
) => {
try {
Expand All @@ -49,19 +58,13 @@ const handleSubmitUsername = async (
return;
}

const { error } = await supabase.from("profiles").upsert([
{
id: userId,
updated_at: new Date(),
username: username,
},
]);
if (error) {
throw error;
} else {
router.push("/account/choose-full-name");
router.setParams({ userId: userId });
}
router.push("/account/choose-full-name");
router.setParams({
userId: userId,
username: username,
email: email,
password: password,
});
} catch (error) {
const typedError = error as { code: number };
setErrorMessage(ERROR_MESSAGES[typedError.code] || "Error saving");
Expand Down
20 changes: 7 additions & 13 deletions components/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,18 @@ export default function Auth() {
async function signUpWithEmail() {
setLoading(true);

const {
data: { session },
error,
} = await supabase.auth.signUp({
email: email,
password: password,
});
// Create a username
const { data, error } = await supabase.auth.getSession();

if (error) {
Alert.alert(error.message);
} else {
await supabase
.from("profiles")
.upsert({ id: session?.user.id, email: email });

// Create a username
router.replace("/account/choose-username");
router.setParams({ userId: session?.user.id });
router.setParams({
userId: data.session?.user.id,
email: email,
password: password,
});
}

setLoading(false);
Expand Down

0 comments on commit 4148525

Please sign in to comment.