From 9baf9d4e2e8a495a936a4a91cd869011b8e52872 Mon Sep 17 00:00:00 2001 From: WizzyGeek Date: Tue, 18 Jun 2024 17:50:20 +0530 Subject: [PATCH] Add email auth --- public/mail.svg | 1 + src/pages/apply.tsx | 2 +- src/pages/login.tsx | 90 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 public/mail.svg diff --git a/public/mail.svg b/public/mail.svg new file mode 100644 index 0000000..6b98060 --- /dev/null +++ b/public/mail.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/pages/apply.tsx b/src/pages/apply.tsx index fdee693..0b55c8b 100644 --- a/src/pages/apply.tsx +++ b/src/pages/apply.tsx @@ -312,7 +312,7 @@ export default function Apply() {
{ (count || 0) < 2 ? ( - + { Application } ) : ( diff --git a/src/pages/login.tsx b/src/pages/login.tsx index d6213f7..469c2f2 100644 --- a/src/pages/login.tsx +++ b/src/pages/login.tsx @@ -1,15 +1,23 @@ -import { useContext, useEffect } from 'react'; +import { useContext, useEffect, useState } from 'react'; import Card from '../components/Card'; import { SessionContext, supabase } from '../supabase'; import './mainbg.scss'; import { useNavigate } from 'react-router-dom'; +import { Field, Form, Formik, FormikValues } from 'formik'; + +function sleep(time: number) { + return new Promise((resolve) => setTimeout(resolve, time)); +} export default function Login() { const session = useContext(SessionContext); const navigate = useNavigate(); + const [sentAt, setSentAt] = useState(-1); + const [left, setLeft] = useState(-1) + useEffect(() => { if (session !== null) { navigate("/apply"); @@ -21,17 +29,75 @@ export default function Login() {
Authentication - - Authenticate with your google account to secure your application +
+ { + const promise = supabase.auth.signInWithOtp({ + email: values.email, + options: { + emailRedirectTo: `${window.location.origin}/auth/` + } + }).then((resp) => { + console.log(resp) + if (resp.error) { + alert("This service is currently unavailable"); + return; + } + const now = Date.now() / 1000; + setSentAt(now); + setLeft(60); + sleep(60 * 1000 + 1).then(() => { + setSentAt(-1); + setLeft(-1); + }) + + const r = () => { + const x = Math.floor(60 - (Date.now() / 1000 - now)); + setLeft(x); + if (x > 0) sleep(1000).then(r); + } + r(); + }) + + return promise; + } }> + {({ isSubmitting }) => ( +
+ + + +
+ {sentAt <= 0 && isSubmitting ? "Sending email..." : ""} + {sentAt > 0 ? ( + `Check you email for login link${ left > 0 ? `, get another link in ${left} seconds` : null }` + ) : null} +
+
+ )} +
+
or
+ +
+ Authenticate with yourself to secure your application