-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added Auth react context #91
Changes from all commits
f2c8717
6cdf4e8
d8101a2
6b3a65a
0834f05
5473cf1
5da46ca
a8cf67e
c52e9ab
b9847fe
775344e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"react-dom": "^18.3.1", | ||
"react-ga4": "^2.1.0", | ||
"react-router-dom": "^6.24.1", | ||
"react-supabase": "^0.2.0", | ||
"swr": "^2.2.5" | ||
}, | ||
"devDependencies": { | ||
|
@@ -37,6 +38,7 @@ | |
"ts-node": "^10.9.2", | ||
"typescript": "^5.5.2", | ||
"vite": "^5.3.1", | ||
"vite-plugin-string": "^1.2.3", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you add this? what is it for? |
||
"vite-tsconfig-paths": "^4.3.2" | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"जैठा तक दमल ड छोक ऐठा तक जाईस, आगुबिती जलेसे दमाल ड ऐठा जाए हुना दख्बो","Walk as far as you can see the road, The way forward is visible after reaching there" | ||
"सफलतर सुरुवत सबै दिन आसफलतारसे हल्कु","Success always begins with failure" | ||
"जैला ठिक हो ऐला करिस, जैला सजिलो लगेसोक ऐल ना करिस","Do what is right, not what is easy" | ||
"जैला जीवन डक समयर महत्व रखेसेक ओईला आप्नाई आप्नाक जनेसेक","The one who has accepted the time in life has known himself" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import React from 'react'; | ||
import { | ||
BrowserRouter as Router, | ||
Route, | ||
|
@@ -7,46 +7,20 @@ import { | |
} from "react-router-dom"; | ||
import User_auth from "./components/userAuth/UserAuth"; | ||
import Home from "./pages/Home/Home"; | ||
import supabase from "./components/userAuth/supabaseClient"; | ||
import ReactGA from 'react-ga4'; | ||
import { useAuth } from "./components/userAuth/AuthContext"; | ||
import ReactGA from "react-ga4"; | ||
|
||
const App: React.FC = () => { | ||
const TrackingID = import.meta.env.VITE_GOOGLE_ANALYTICS_TRACKING_ID; | ||
ReactGA.initialize(TrackingID); | ||
const [isAuthenticated, setIsAuthenticated] = useState(false); | ||
|
||
// useEffect to check the current session and subscribe to authentication state changes | ||
useEffect(() => { | ||
// Function to fetch the current session | ||
const fetchSession = async () => { | ||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession(); | ||
setIsAuthenticated(!!session); // Update authentication state | ||
}; | ||
|
||
fetchSession(); // Initial session fetch | ||
|
||
// Subscribe to authentication state changes | ||
const { | ||
data: { subscription }, | ||
} = supabase.auth.onAuthStateChange((_event, session) => { | ||
setIsAuthenticated(!!session); // Update authentication state on changes | ||
}); | ||
|
||
// Cleanup subscription on component unmount | ||
return () => subscription.unsubscribe(); | ||
}, []); | ||
const { user } = useAuth(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yessss!! This code looks much cleaner now! |
||
return ( | ||
<Router> | ||
<Routes> | ||
<Route path="/login" element={<User_auth />} /> | ||
{/* Protect the / route, redirect to /login if not authenticated */} | ||
<Route | ||
path="/" | ||
element={isAuthenticated ? <Home /> : <Navigate to="/login" />} | ||
/> | ||
<Route path="/" element={user ? <Home /> : <Navigate to="/login" />} /> | ||
{/* Default route redirects to /login */} | ||
<Route path="/" element={<Navigate to="/login" />} /> | ||
</Routes> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { | ||
createContext, | ||
useContext, | ||
useState, | ||
ReactNode, | ||
useEffect, | ||
} from "react"; | ||
import supabase from "./supabaseClient"; | ||
import { AuthError, AuthResponse, AuthTokenResponsePassword, SignInWithPasswordCredentials, SignUpWithPasswordCredentials, User } from "@supabase/supabase-js"; | ||
|
||
interface AuthContextProps { | ||
user: User | null; | ||
signUp: (data: SignUpWithPasswordCredentials) => Promise<AuthResponse>; | ||
signIn: (data: SignInWithPasswordCredentials) => Promise<AuthTokenResponsePassword>; | ||
signOut: () => Promise<{ error: AuthError | null }> | ||
} | ||
|
||
const AuthContext = createContext<AuthContextProps | undefined>(undefined); | ||
|
||
export const AuthProvider = ({ children }: { children: ReactNode }) => { | ||
const [user, setUser] = useState<User | null>(null); | ||
const [, setIsAuthenticated] = useState(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't need isAuthenticated anymore - you can get rid of this. Since you can figure out whether the user is authenticated from the user data |
||
|
||
useEffect(() => { | ||
const fetchSession = async () => { | ||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession(); | ||
setIsAuthenticated(!!session); | ||
setUser(session?.user ?? null); | ||
}; | ||
|
||
fetchSession(); | ||
|
||
const { | ||
data: { subscription }, | ||
} = supabase.auth.onAuthStateChange((_event, session) => { | ||
setIsAuthenticated(!!session); | ||
setUser(session?.user ?? null); | ||
}); | ||
|
||
return () => subscription.unsubscribe(); | ||
}, []); | ||
|
||
const value: AuthContextProps = { | ||
signUp: (data) => supabase.auth.signUp(data), | ||
signIn: (data) => supabase.auth.signInWithPassword(data), | ||
signOut: () => supabase.auth.signOut(), | ||
user, | ||
}; | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error("useAuth must be used within an AuthProvider"); | ||
} | ||
return context; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you still using react-supabase? You can run
pnpm remove react-supabase
to get rid of it if you don't need it anymorehttps://chatgpt.com/share/04fc4457-0e8f-40f1-b135-e59fe0c94d16