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

Fixed Login Bug and Phone Number input #203

Merged
merged 4 commits into from
Feb 6, 2024
Merged
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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# localhost will not work!! Replace this with your IP address even when testing locally
AXIOS_BASEURL=143.215.53.11
AXIOS_BASEURL=127.0.0.1
30 changes: 26 additions & 4 deletions src/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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>({
url,
Expand All @@ -10,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 @@ -33,10 +53,12 @@ export async function internalRequest<T>({
withCredentials: true,
/** PersonalInfo POST doesn't work with mode: "cors" */
// mode: "cors",
Auth: idToken,
accesstoken: idToken,
},
data: newBody,
data: method.toLowerCase() !== "get" ? newBody : undefined,
});

if (response.data.success === false) {
throw new Error(`Unable to connect to API: ${response.data.message}`);
}
Expand Down
Loading