Skip to content

Commit d1f6dfa

Browse files
committed
add auth context and update path to constants
1 parent a13ff90 commit d1f6dfa

File tree

4 files changed

+86
-8
lines changed

4 files changed

+86
-8
lines changed

src/App.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import { BrowserRouter, Route, Routes } from "react-router-dom";
22
import Login from "pages/Login";
33
import Signup from "pages/Signup";
4+
import { MantineProvider } from "@mantine/core";
5+
import { Notifications } from "@mantine/notifications";
6+
import { HOME_PATH, LOGIN_PATH, SIGNUP_PATH } from "routes/constants";
7+
import { AuthProvider } from "context/AuthContext";
8+
import { getUserData } from "api/profile";
49

510
function App() {
611
return (
7-
<BrowserRouter>
8-
<Routes>
9-
<Route path="/" element={<div>L:o</div>} />
10-
<Route path="login" element={<Login />} />
11-
<Route path="signup" element={<Signup />} />
12-
</Routes>
13-
</BrowserRouter>
12+
<MantineProvider withNormalizeCSS withGlobalStyles>
13+
<AuthProvider>
14+
<Notifications />
15+
<BrowserRouter>
16+
<Routes>
17+
<Route path={HOME_PATH} element={<></>} />
18+
<Route path={LOGIN_PATH} element={<Login />} />
19+
<Route path={SIGNUP_PATH} element={<Signup />} />
20+
</Routes>
21+
</BrowserRouter>
22+
</AuthProvider>
23+
</MantineProvider>
1424
);
1525
}
1626

src/api/profile.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UserCredential } from "firebase/auth";
22
import { auth, db } from "../firebase/config";
3-
import { doc, setDoc, updateDoc } from "@firebase/firestore";
3+
import { doc, setDoc, updateDoc, getDoc } from "@firebase/firestore";
44
import { UserProfile } from "../types/profile";
55

66
const USER_PATH = "users";
@@ -31,3 +31,20 @@ export async function updateUserData(params: UpdateUserRequest) {
3131

3232
await updateDoc(doc(db, USER_PATH, uid), profile);
3333
}
34+
35+
export async function getUserData(): Promise<UserProfile> {
36+
if (!auth.currentUser) {
37+
throw new Error("User needs to log in");
38+
}
39+
40+
const uid = auth.currentUser.uid;
41+
42+
const docSnap = await getDoc(doc(db, USER_PATH, uid));
43+
44+
if (!docSnap.exists()) {
45+
throw new Error("User does not exist");
46+
}
47+
48+
const data = docSnap.data() as UserProfile;
49+
return data;
50+
}

src/context/AuthContext.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react";
2+
import { onAuthStateChanged } from "firebase/auth";
3+
import { auth } from "../firebase/config";
4+
import { getUserData } from "api/profile";
5+
import { UserProfile } from "types/profile";
6+
7+
type AuthContextType = {
8+
user: UserProfile | null;
9+
};
10+
11+
const AuthContext = React.createContext<AuthContextType | null>(null);
12+
13+
// Call this hook if you need user data or check whether user is logged in
14+
// If user is not authenticated, the value will be null
15+
export const useAuth = () => {
16+
const context = React.useContext(AuthContext);
17+
18+
if (!context) {
19+
throw new Error("useAuth has to be used within AuthProvider");
20+
}
21+
22+
return context;
23+
};
24+
25+
export function AuthProvider(props: React.PropsWithChildren) {
26+
const { children } = props;
27+
28+
const [user, setUser] = React.useState<UserProfile | null>(null);
29+
30+
React.useEffect(() => {
31+
const unsubscribe = onAuthStateChanged(auth, (user) => {
32+
if (user) {
33+
// Get details
34+
getUserData().then((data) => {
35+
setUser(data);
36+
});
37+
} else {
38+
setUser(user);
39+
}
40+
});
41+
42+
return unsubscribe;
43+
});
44+
45+
return (
46+
<AuthContext.Provider value={{ user }}>{children}</AuthContext.Provider>
47+
);
48+
}

src/routes/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const HOME_PATH = "/";
2+
export const LOGIN_PATH = "login";
3+
export const SIGNUP_PATH = "signup";

0 commit comments

Comments
 (0)