Skip to content

Commit

Permalink
Merge pull request #2 from lugvitc/main
Browse files Browse the repository at this point in the history
Deploy v2:  Add email auth
  • Loading branch information
WizzyGeek authored Jun 18, 2024
2 parents fce476d + 9baf9d4 commit ec00d6e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
1 change: 1 addition & 0 deletions public/mail.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/pages/apply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export default function Apply() {
<div className=' border-2 sm:rounded-md sm:w-fit w-full max-w-[640px] border-[#202020] text-white backdrop-blur-md bg-[#ffffff09]'>
{
(count || 0) < 2 ? (
<Formik initialValues={{ apps: [], name: session?.user?.user_metadata?.full_name, common_questions: []}} onSubmit={onSumbitFactory(sig, setSig, session)} validate={validateForm}>
<Formik initialValues={{ apps: [], name: session?.user?.user_metadata?.full_name || "", common_questions: []}} onSubmit={onSumbitFactory(sig, setSig, session)} validate={validateForm}>
{ Application }
</Formik>
) : (
Expand Down
90 changes: 78 additions & 12 deletions src/pages/login.tsx
Original file line number Diff line number Diff line change
@@ -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<number>(-1);
const [left, setLeft] = useState<number>(-1)

useEffect(() => {
if (session !== null) {
navigate("/apply");
Expand All @@ -21,17 +29,75 @@ export default function Login() {
<main>
<Card className="flex flex-col gap-4 max-w-80">
<span className='text-xl font-semibold'>Authentication</span>
<button className='flex items-center rounded-md text-center bg-yellow-400 text-black font-medium hover:bg-black hover:text-white transition-all border border-yellow-400'
onClick={() => {supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: `${window.location.origin}/auth/`
}
})}}>
<div className='bg-yellow-400 p-2 rounded-l-md'><img src='/google.svg' className=' h-8 w-8'/></div>
<div className='p-2 w-full'>Continue with Google</div>
</button>
<span className='text-sm'>Authenticate with your google account to secure your application</span>
<div className='flex flex-col gap-2 items-center justify-center'>
<Formik initialValues={{email: ""}} onSubmit={function (values: FormikValues,): void | Promise<any> {
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 }) => (
<Form className='flex flex-col gap-6 w-full'>
<label htmlFor='email' className='-mb-4 ml-1'>
Email
</label>
<Field type="email" name="email" id="email" placeholder="[email protected]"></Field>
<button
className={ "flex items-center rounded-md text-center text-black font-semibold transition-all border w-full" + (
(sentAt > 0 || isSubmitting) ? " bg-yellow-700 border-yellow-700" : " bg-yellow-400 hover:bg-black hover:text-white border-yellow-400"
)}
type='submit'
disabled={sentAt > 0 || isSubmitting}
>
<div className={'p-2 rounded-l-md ' + ((sentAt > 0 || isSubmitting) ? "" : " bg-yellow-400")}><img src='/mail.svg' className=' h-8 w-8'/></div>
<div className='p-2 w-full'>Continue with Email</div>
</button>
<div className='text-sm'>
{sentAt <= 0 && isSubmitting ? "Sending email..." : ""}
{sentAt > 0 ? (
`Check you email for login link${ left > 0 ? `, get another link in ${left} seconds` : null }`
) : null}
</div>
</Form>
)}
</Formik>
<div className='italic'>or</div>
<button className='flex items-center rounded-md text-center bg-yellow-400 text-black font-medium hover:bg-black hover:text-white transition-all border border-yellow-400 w-full'
onClick={() => {supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: `${window.location.origin}/auth/`
}
})}}>
<div className='bg-yellow-400 p-2 rounded-l-md'><img src='/google.svg' className=' h-8 w-8'/></div>
<div className='p-2 w-full'>Continue with Google</div>
</button>
</div>
<span className='text-sm'>Authenticate with yourself to secure your application</span>
</Card>
</main>
</section>
Expand Down

0 comments on commit ec00d6e

Please sign in to comment.