From 32219acf337b7c5c65aba98b1c4bbd5af525707a Mon Sep 17 00:00:00 2001 From: John Shaver Date: Mon, 10 Apr 2023 14:26:02 -0700 Subject: [PATCH] get authentication working for supabase --- src/App.tsx | 26 ++++++++++++++++++------- src/LoginForm.tsx | 48 ++++++++++++++++++++++++++++++++++------------- src/api.tsx | 8 +++++--- src/constants.ts | 12 ++++++++++-- 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c4b941c..ee39ad3 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,15 +3,15 @@ import React from 'react'; import LoginForm from './LoginForm'; import ScoreCounter from './ScoreCounter'; import Row from './Row'; -import Icon from './Icon'; import { useJSONLocalStorage, useLocalStorage } from './hooks/useLocalStorage'; -import {toot, starman, flush, getCheer} from './sound'; +import {toot, starman, flush} from './sound'; + +import {supabase} from './api'; +import {User} from '@supabase/supabase-js'; import './App.css'; - - interface Stats { attempts: number; scheduledPoops: number; @@ -34,10 +34,22 @@ const initialStreakTracker:StreakTracker = { function App() { const [stats, setStats] = useJSONLocalStorage("stats", initialStats); - const [name] = useLocalStorage("name", {}); - const [password] = useLocalStorage("password", {}); + const [user, setUser] = React.useState(); + const [loading, setLoading] = React.useState(true); + React.useEffect(() => { + supabase.auth.getUser().then(res => { + if(res.data.user) { + setUser(res.data.user); + } + setLoading(false); + if(res.error) { + console.error(res.error); + } + }); - return !name || !password ? :( + }, []); + + return loading ? <> : !user ? : (
Hello Pax!
diff --git a/src/LoginForm.tsx b/src/LoginForm.tsx index fe7e3b1..dd9b409 100644 --- a/src/LoginForm.tsx +++ b/src/LoginForm.tsx @@ -1,32 +1,54 @@ import React from 'react'; import './LoginForm.css'; -import { useLocalStorage } from './hooks/useLocalStorage'; +import {supabase} from './api'; +import {User} from '@supabase/supabase-js'; -const LoginForm = () => { - const [, setName] = useLocalStorage('name', {}); - const [, setPassword] = useLocalStorage('password', {}); +interface LoginFormProps { + setUser: React.Dispatch>; +} + +const LoginForm:React.FC = ({setUser}) => { + const [error, setError] = React.useState(); + const [submitting, setSubmitting] = React.useState(false); + const [login, setLogin] = React.useState(""); + const [password, setPassword] = React.useState(""); return (
{ + onSubmit={async e => { e.preventDefault(); - console.log('BLARG'); - const inputs = new FormData(e.target as HTMLFormElement); - setName(inputs.get('name') as string); - setPassword(inputs.get('password') as string); + if(login) { + setSubmitting(true); + setError(undefined); + const {data, error: loginError} = await supabase.auth.signInWithPassword({ + email: login, + password + }); + setSubmitting(false) + if(loginError) { + setError(loginError.message || "Error authenticating"); + console.error(loginError); + return; + } + if(data.user) { + setUser(data.user); + } + } }} > +
{error && error}
- +
); diff --git a/src/api.tsx b/src/api.tsx index e030936..fe5fd75 100644 --- a/src/api.tsx +++ b/src/api.tsx @@ -1,7 +1,9 @@ import { createClient } from "@supabase/supabase-js"; -import { REACT_APP_SUPABASE_URL, REACT_APP_SUPABASE_KEY } from './constants.ts'; +import { SB_ANON_KEY, SB_URL } from './constants'; export const supabase = createClient( - REACT_APP_SUPABASE_URL, - REACT_APP_SUPABASE_KEY + SB_URL, + SB_ANON_KEY ); + + diff --git a/src/constants.ts b/src/constants.ts index eaab883..e34acd7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,12 @@ -export const SB_ANON_KEY = process.env.REACT_APP_SB_ANON_KEY; -export const SB_URL = process.env.REACT_APP_SB_URL; + +if(!process.env.REACT_APP_SB_ANON_KEY) { + console.error("No REACT_APP_SB_ANON_KEY env var provided. Closing App.") +} +if(!process.env.REACT_APP_SB_URL){ + console.error("No REACT_APP_SB_URL env var provided. Closing App.") +} + +export const SB_ANON_KEY = process.env.REACT_APP_SB_ANON_KEY as string; +export const SB_URL = process.env.REACT_APP_SB_URL as string;