Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
samarth52 committed Feb 6, 2024
1 parent bdadb84 commit 6ffa104
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 22 additions & 5 deletions src/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosResponse } from "axios";
import { getAuth } from "firebase/auth";
import { User, getAuth } from "firebase/auth";
import { InternalRequestData, InternalResponseData } from "./types";
import firebaseInit from "./firebase/config";
// import firebaseInit from "./firebase/config";

export async function internalRequest<T>({
Expand All @@ -11,11 +12,29 @@ export async function internalRequest<T>({
authRequired = true,
}: InternalRequestData): Promise<T> {
try {
const idToken: string = await getAuth().currentUser.getIdToken();
let idToken: string | undefined;
let newParams = queryParams;
let newBody = body;
if (authRequired) {
const { email } = getAuth().currentUser;
firebaseInit();
const auth = getAuth();

const currentUser: User = await new Promise((resolve, reject) => {
const unsubscribe = auth.onAuthStateChanged((user) => {
unsubscribe();
if (user) {
resolve(user);
} else {
reject(new Error("Unable to get user"));
}
}, reject);
});

idToken = await currentUser.getIdToken();
const { email } = currentUser;
if (email === null) {
throw new Error("Email does not exist on user");
}
newParams = {
...queryParams,
email,
Expand All @@ -41,12 +60,10 @@ export async function internalRequest<T>({
});

if (response.data.success === false) {
console.log(response.data);
throw new Error(`Unable to connect to API: ${response.data.message}`);
}
return response.data.payload;
} catch (e) {
console.log(e);
throw new Error(`Unable to connect to API: ${e}`);
}
}
3 changes: 1 addition & 2 deletions src/screens/SignUp/PersonalInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ function PersonalInfoScreen() {
};

const formatPhoneNumber = (currentNumber: string) => {
console.log("curr", currentNumber);
const digitsOnly = currentNumber.replace(/\D/g, "");

if (digitsOnly.length < 3) {
Expand All @@ -142,7 +141,7 @@ function PersonalInfoScreen() {
const inputDigitsOnly = input.replace(/[()\-\s]/g, "");
const phoneNumberDigitsOnly = currentNumber.replace(/\D/g, "");

if (inputDigitsOnly.length < phoneNumberDigitsOnly.length) {
if (inputDigitsOnly.length <= phoneNumberDigitsOnly.length) {
// there was a backspace
setNumber(phoneNumberDigitsOnly.slice(0, -1));
} else {
Expand Down

0 comments on commit 6ffa104

Please sign in to comment.