Skip to content

Commit

Permalink
feat: finish sign in when coming from auth link
Browse files Browse the repository at this point in the history
  • Loading branch information
hetd54 committed Mar 26, 2024
1 parent 70036bd commit bfac2e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
74 changes: 38 additions & 36 deletions src/components/DownloadModal.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<Inputs>()

const options = [
Expand All @@ -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<Inputs> = 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}`)
Expand Down
2 changes: 1 addition & 1 deletion src/firebase.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit bfac2e0

Please sign in to comment.