diff --git a/client/src/App.tsx b/client/src/App.tsx index 96c4c18..98a3f15 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,31 +1,63 @@ +// react imports import { BrowserRouter, Route, Routes } from 'react-router-dom' -import Home from './components/Home/Home' -import Register from './components/User/Register' -import { SupabaseProvider, getSupabaseClient, useSupabase } from './supabase/SupabaseContext' -import './App.css' -import { useEffect } from 'react' -import Login from './components/User/Login' +import { useEffect, useState } from 'react' -function App() { +// components +import Register from './components/Auth/Register' +import Login from './components/Auth/Login' +import Home from './components/Home' + +// util +import { SupabaseProvider, getSupabaseClient, useSupabase } from './supabase/SupabaseContext' +import { initialState } from './util/initialState' +import { AppState } from './util/types' +import './App.css' +import Navbar from './components/Nav/Navbar' + +export default function App() { + const [state, setState] = useState(initialState); const supabase = useSupabase(); useEffect(() => { - console.log(supabase); + setState((prev: AppState) => { + let newUser; + let newSession; + + if (supabase) { + newSession = supabase.auth.session(); + newUser = supabase.auth.user(); + } + + return { + ...prev, + supabase: supabase, + user: newUser ?? prev.user, + session: newSession ?? prev.session + } + }) }, [supabase]) + useEffect(() => { + console.log(state); + }, [state]); + return (
+ + + {/* Top level route */} } /> + + {/* Second level routes */} } /> } /> +
) } - -export default App diff --git a/client/src/components/User/Login.tsx b/client/src/components/Auth/Login.tsx similarity index 51% rename from client/src/components/User/Login.tsx rename to client/src/components/Auth/Login.tsx index 4c7cc04..154e11a 100644 --- a/client/src/components/User/Login.tsx +++ b/client/src/components/Auth/Login.tsx @@ -1,33 +1,15 @@ -import { useState } from "react" +import { FormInput, getSession, handleLogin } from "../../util/authHelpers"; import { useSupabase } from "../../supabase/SupabaseContext"; - -interface FormInput { - email: string - password: string -} +import { useState } from "react" export default function Login() { const [input, setInput] = useState({ email: "", password: "" }); const supabase = useSupabase(); - const handleLogin = async () => { - if (typeof supabase === "string") return; - if (!input.email || !input.password) return; - console.log(input); - - const { user, session, error } = await supabase.auth.signIn({ email: input.email, password: input.password }); - if (error) throw error; - console.log(user, session); - return { user, session }; - } - - const getSession = async () => { - if (typeof supabase === "string") return; - console.log(supabase.auth.session()); - } - return (
+

Login

+
@@ -39,8 +21,8 @@ export default function Login() {
- - + +
) } \ No newline at end of file diff --git a/client/src/components/User/Register.tsx b/client/src/components/Auth/Register.tsx similarity index 52% rename from client/src/components/User/Register.tsx rename to client/src/components/Auth/Register.tsx index 7024d86..9946423 100644 --- a/client/src/components/User/Register.tsx +++ b/client/src/components/Auth/Register.tsx @@ -1,5 +1,6 @@ import { useState } from "react"; import { useSupabase } from "../../supabase/SupabaseContext"; +import { getSession, handleRegister } from "../../util/authHelpers"; interface FormInput { email: string @@ -10,27 +11,6 @@ export default function Register() { const [input, setInput] = useState({email: "", password: ""}); const supabase = useSupabase(); - const handleClick = async () => { - if (typeof supabase === "string") return; - - const { email, password } = input; - if (email && password) { - const { user, session, error} = await supabase.auth.signUp({ email, password }); - if (error) throw error; - console.log(user, session); - } - } - - const getSession = async () => { - if (typeof supabase === "string") return; - - console.log(supabase.auth.session()); - } - - const checkSupabase = () => { - if (supabase) console.log(supabase); - } - return (

Register

@@ -46,9 +26,8 @@ export default function Register() { - - - + +
) } \ No newline at end of file diff --git a/client/src/components/Home.tsx b/client/src/components/Home.tsx new file mode 100644 index 0000000..fe64de3 --- /dev/null +++ b/client/src/components/Home.tsx @@ -0,0 +1,17 @@ +import { useNavigate } from "react-router-dom" + +export default function Home() { + const navigate = useNavigate(); + + return ( +
+

Vite + React + Supabase

+

Check out the user stuff below:

+ +
+ + +
+
+ ) +} \ No newline at end of file diff --git a/client/src/components/Home/Home.tsx b/client/src/components/Home/Home.tsx deleted file mode 100644 index 1f9ed58..0000000 --- a/client/src/components/Home/Home.tsx +++ /dev/null @@ -1,9 +0,0 @@ -export default function Home() { - return ( -
-

Vite + React + Supabase

-

Check out the user stuff below:

- User stuff -
- ) -} \ No newline at end of file diff --git a/client/src/components/Nav/Navbar.css b/client/src/components/Nav/Navbar.css new file mode 100644 index 0000000..5bf3555 --- /dev/null +++ b/client/src/components/Nav/Navbar.css @@ -0,0 +1,4 @@ +#navbar-section { + display: flex; + align-items: baseline; +} \ No newline at end of file diff --git a/client/src/components/Nav/Navbar.tsx b/client/src/components/Nav/Navbar.tsx new file mode 100644 index 0000000..5cd569b --- /dev/null +++ b/client/src/components/Nav/Navbar.tsx @@ -0,0 +1,35 @@ +import { useEffect, useState } from "react"; +import { useNavigate } from "react-router-dom"; +import { useSupabase } from "../../supabase/SupabaseContext" +import "./Navbar.css"; + +export default function Navbar() { + const [view, setView] = useState(

Loading...

); + + const supabase = useSupabase(); + const navigate = useNavigate(); + + const handleLogout = async () => { + await supabase?.auth.signOut(); + } + + useEffect(() => { + const user = supabase?.auth.user(); + + setView( + + ) + }, [supabase]); + + return view; +} \ No newline at end of file diff --git a/client/src/main.tsx b/client/src/main.tsx index 611e848..94ad62a 100644 --- a/client/src/main.tsx +++ b/client/src/main.tsx @@ -1,10 +1,5 @@ -import React from 'react' import ReactDOM from 'react-dom/client' import App from './App' import './index.css' -ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - - - -) +ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(); diff --git a/client/src/supabase/SupabaseContext.tsx b/client/src/supabase/SupabaseContext.tsx index 3b01a9a..c385ba5 100644 --- a/client/src/supabase/SupabaseContext.tsx +++ b/client/src/supabase/SupabaseContext.tsx @@ -16,6 +16,6 @@ export const SupabaseProvider: FC<{children: ReactNode, value: SupabaseClient}> } export const useSupabase = () => { - const context = useContext(SupabaseContext); - return context || "Context currently undefined"; + const context = useContext(SupabaseContext) || undefined; + return context; } \ No newline at end of file diff --git a/client/src/util/authHelpers.ts b/client/src/util/authHelpers.ts new file mode 100644 index 0000000..281f083 --- /dev/null +++ b/client/src/util/authHelpers.ts @@ -0,0 +1,32 @@ +import { SupabaseClient } from "@supabase/supabase-js"; + +export interface FormInput { + email: string + password: string +} + +export const handleLogin = async (supabase: SupabaseClient | undefined, input: FormInput) => { + if (!supabase || !input.email || !input.password) return; + console.log(input); + + const { user, session, error } = await supabase.auth.signIn({ email: input.email, password: input.password }); + if (error) throw error; + console.log(user, session); + return { user, session }; +} + +export const handleRegister = async (supabase: SupabaseClient | undefined, input: FormInput) => { + if (!supabase) return; + + const { email, password } = input; + if (email && password) { + const { user, session, error} = await supabase.auth.signUp({ email, password }); + if (error) throw error; + console.log(user, session); + } +} + +export const getSession = async (supabase: SupabaseClient | undefined) => { + if (!supabase) return; + console.log(supabase.auth.session()); +} diff --git a/client/src/util/initialState.ts b/client/src/util/initialState.ts new file mode 100644 index 0000000..c550c1a --- /dev/null +++ b/client/src/util/initialState.ts @@ -0,0 +1,7 @@ +import { AppState } from "./types"; + +export const initialState: AppState = { + supabase: undefined, + session: undefined, + user: undefined +} \ No newline at end of file diff --git a/client/src/util/types.ts b/client/src/util/types.ts new file mode 100644 index 0000000..1b98444 --- /dev/null +++ b/client/src/util/types.ts @@ -0,0 +1,7 @@ +import { Session, SupabaseClient, User } from "@supabase/supabase-js"; + +export interface AppState { + supabase?: SupabaseClient + session?: Session + user?: User +} \ No newline at end of file