diff --git a/src/components/DownloadModal.tsx b/src/components/DownloadModal.tsx index 8c64953..b7dd491 100644 --- a/src/components/DownloadModal.tsx +++ b/src/components/DownloadModal.tsx @@ -1,15 +1,10 @@ -import React from "react" +import React, { useEffect, useState } from "react" import * as Dialog from "@radix-ui/react-dialog" import * as Form from "@radix-ui/react-form" import { Cross2Icon, DownloadIcon, PlusIcon } from "@radix-ui/react-icons" import { useForm, Controller, type SubmitHandler } from "react-hook-form" -// import { -// getAuth, -// isSignInWithEmailLink, -// sendSignInLinkToEmail, -// signInWithEmailLink, -// } from "firebase/auth" -// import { getDownloadUrl, actionCodeSettings, finishSignIn } from "../firebase" +import { isSignInWithEmailLink, sendSignInLinkToEmail } from "firebase/auth" +import { auth, getDownloadUrl, actionCodeSettings, finishSignIn } from "../firebase" import { CustomInput } from "./CustomInput.tsx" import { CustomTextarea } from "./CustomTextarea.tsx" import { CheckboxGroup } from "./CheckboxGroup.tsx" @@ -23,11 +18,10 @@ export interface Inputs { } const DownloadModal = () => { - //const auth = getAuth() const [isOpen, setIsOpen] = React.useState(false) const [downloadUrl, setDownloadUrl] = React.useState("") const [message, setMessage] = React.useState("") - + const [user, setUser] = useState(auth.currentUser) const { handleSubmit, control, register, setValue } = useForm() const options = [ @@ -46,40 +40,48 @@ const DownloadModal = () => { { value: "codebook_pratio", label: "PRATIO" }, ] - // // on load, check if user has validated email; set download url - // if (isSignInWithEmailLink(auth, window.location.href)) { - // // Additional state parameters can also be passed via URL. - // // This can be used to continue the user's intended action before triggering - // let email = window.localStorage.getItem("emailForSignIn") - // if (!email) { - // // User opened the link on a different device. To prevent session fixation - // // attacks, ask the user to provide the associated email again. For example: - // email = window.prompt("Please provide your email for confirmation") - // } - // try { - // const files = JSON.parse(window.localStorage.getItem("fileNameList") as string) - // console.log(files.length) - // // if (user.emailVerified && files) { - // // const urls = files.map(async (fileName: string) => await getDownloadUrl(fileName)) - // // console.log(urls) - // // - // // // TODO: push data to history table if signed in - // // } - // } catch (e) { - // const error = e as string - // setMessage(`Error: ${error}`) - // } - // } + useEffect(() => { + // on load, check if user has validated email; set download url + const getUser = async (email: any) => { + setUser(await finishSignIn(email)) + } + if (isSignInWithEmailLink(auth, window.location.href)) { + // Additional state parameters can also be passed via URL. + // This can be used to continue the user's intended action before triggering + let email = window.localStorage.getItem("emailForSignIn") + if (!email) { + // User opened the link on a different device. To prevent session fixation + // attacks, ask the user to provide the associated email again. For example: + email = window.prompt("Please provide your email for confirmation") + } + try { + getUser(email) + const files = JSON.parse(window.localStorage.getItem("fileNameList") as string) + console.log(files.length) + if (user && user.emailVerified && files) { + setIsOpen(true) + const urls = files.map(async (fileName: string) => await getDownloadUrl(fileName)) + console.log(urls) + setDownloadUrl(urls) + + // TODO: push data to history table if signed in + } + } catch (e) { + const error = e as string + setMessage(`Error: ${error}`) + } + } + }, []) const onSubmit: SubmitHandler = async (data) => { console.log(data) console.log(data.files) // send validation email try { - //const result = await sendSignInLinkToEmail(auth, data.email, actionCodeSettings) + const result = await sendSignInLinkToEmail(auth, data.email, actionCodeSettings) window.localStorage.setItem("emailForSignIn", data.email) window.localStorage.setItem("fileNameList", JSON.stringify(data.files)) - //return result + return result } catch (e) { const error = e as string setMessage(`Error: ${error}`) diff --git a/src/firebase.js b/src/firebase.js index 3e64687..2c229c7 100644 --- a/src/firebase.js +++ b/src/firebase.js @@ -1,5 +1,5 @@ import { initializeApp } from "firebase/app" -import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore" +import { getFirestore } from "firebase/firestore" import { EmailAuthProvider, getAuth,