-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add supabase connection and auth context with login and logout functions
- Loading branch information
1 parent
ffe39c1
commit 3eccaa4
Showing
13 changed files
with
322 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { supabase } from "../lib/supabase"; | ||
import { UserModel } from "../models/user.model"; | ||
import { showToast } from "../utils/toast"; | ||
|
||
export const getUserByEmail = async (email: string) => { | ||
try { | ||
const { data, error } = await supabase.from("users").select().eq("email", email).single(); | ||
if (error) { | ||
if (error.details === "The result contains 0 rows") { | ||
showToast("error", "Invalid credential."); | ||
} else { | ||
showToast("error", String(error)); | ||
} | ||
} | ||
return data as UserModel; | ||
} catch (error) { | ||
console.error("Failed to get user:", error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createContext, ReactNode, useEffect, useState } from "react"; | ||
import { getUserByEmail } from "../api/useUserApi"; | ||
import { UserModel } from "../models/user.model"; | ||
|
||
type AuthContextProps = { | ||
userId: string | null; | ||
isAuthenticated: boolean; | ||
hasAvatar: boolean; | ||
handleLogin: (email: string) => Promise<void>; | ||
handleLogout: () => Promise<void>; | ||
}; | ||
|
||
type AuthContextProvider = { | ||
children: ReactNode; | ||
}; | ||
|
||
export const AuthContext = createContext({} as AuthContextProps); | ||
|
||
export const AuthContextProvider = ({ children }: AuthContextProvider) => { | ||
const [userId, setUserId] = useState<string | null>(null); | ||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false); | ||
const [hasAvatar, setHasAvatar] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const userLocal = localStorage.getItem("userLocal"); | ||
if (userLocal) { | ||
const user = JSON.parse(userLocal); | ||
setUserData(user); | ||
} | ||
}, []); | ||
|
||
const setUserData = (user: UserModel) => { | ||
if (user) { | ||
setUserId(user.id); | ||
setIsAuthenticated(true); | ||
setHasAvatar(user.hasAvatar); | ||
} | ||
}; | ||
|
||
const handleLogin = async (email: string) => { | ||
try { | ||
const user = await getUserByEmail(email); | ||
if (user) { | ||
setUserData(user); | ||
window.localStorage.setItem("userLocal", JSON.stringify(user)); | ||
} | ||
} catch (error) { | ||
console.error("Login failed:", error); | ||
} finally { | ||
return; | ||
} | ||
}; | ||
|
||
const handleLogout = async () => { | ||
setUserId(null); | ||
setIsAuthenticated(false); | ||
setHasAvatar(false); | ||
window.localStorage.removeItem("userLocal"); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ userId, isAuthenticated, hasAvatar, handleLogin, handleLogout }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const ENV_VARS = { | ||
SUPABASE_URL: import.meta.env.VITE_SUPABASE_URL, | ||
SUPABASE_KEY: import.meta.env.VITE_SUPABASE_KEY | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createClient } from "@supabase/supabase-js"; | ||
import { ENV_VARS } from "./constants"; | ||
|
||
const SUPABASE_URL = ENV_VARS.SUPABASE_URL; | ||
const SUPABASE_KEY = ENV_VARS.SUPABASE_KEY; | ||
|
||
export const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.