Skip to content

Commit dd24c58

Browse files
Merge pull request #193 from Speedrunyourknowledge/fix/sessions
fix: refactor sessions in frontend
2 parents c35bce1 + 3407087 commit dd24c58

File tree

9 files changed

+31
-66
lines changed

9 files changed

+31
-66
lines changed

calc-frontend/src/App/App.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import './app.css'
55
import './widgets.css'
66

77
import RoutesList from "./RoutesList";
8-
import { AuthProvider } from "./AuthContext";
98

109
import { Toaster } from "@/components/ui/sonner";
1110

@@ -19,10 +18,10 @@ function App() {
1918
createRoutesFromElements(RoutesList));
2019

2120
return (
22-
<AuthProvider>
21+
<>
2322
<Toaster position="bottom-left" richColors />
2423
<RouterProvider router={router} />
25-
</AuthProvider>
24+
</>
2625
)
2726
}
2827

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +0,0 @@
1-
import { createContext, useContext } from "react";
2-
import { authClient } from "../lib/auth-client";
3-
import { Session } from "../lib/auth-client";
4-
import { BetterFetchError } from "better-auth/react";
5-
6-
interface AuthContextType {
7-
session: Session | null;
8-
isPending: boolean;
9-
error: BetterFetchError | null;
10-
refetch: () => void;
11-
}
12-
13-
const AuthContext = createContext<AuthContextType | undefined>(undefined);
14-
15-
export const useAuth = () => {
16-
const context = useContext(AuthContext);
17-
if(context === undefined) {
18-
throw new Error("useAuth must be used within an AuthProvider");
19-
}
20-
return context;
21-
}
22-
// To get the data from useSession(). {data: session, isPending, error, refetch}
23-
// instead call useAuth() anywhere in the application, you will get the same data
24-
export const AuthProvider = ({children}: {children: React.ReactNode}) => {
25-
const {data: session, isPending, error, refetch } = authClient.useSession();
26-
27-
return (
28-
<AuthContext.Provider value={{session, isPending, error, refetch}}>
29-
{children}
30-
</AuthContext.Provider>
31-
);
32-
};

calc-frontend/src/App/RootLayout.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { ScrollRestoration } from "react-router";
44
import ucfLogo from "../assets/ucf-logo.png"
55
import CalcLogo from "../components/CalcLogo"
66

7-
import { useAuth } from "./AuthContext";
7+
import { authClient } from "../lib/auth-client";
88
import { Avatar, AvatarFallback, AvatarImage } from "../components/ui/avatar"
99

10-
1110
function RootLayout() {
12-
const { session } = useAuth();
11+
const session = authClient.useSession();
1312

1413
return (
1514
<>
@@ -29,12 +28,12 @@ function RootLayout() {
2928
</div>
3029

3130
</div>
32-
{session ? (
33-
session.user.image ? (
31+
{session.data?.session ? (
32+
session.data?.user.image ? (
3433
<Link to="/dashboard">
3534
<Avatar className="mr-5 cursor-pointer w-12 h-12">
36-
<AvatarImage src={session.user.image} />
37-
<AvatarFallback>{session.user.name.charAt(0)}</AvatarFallback>
35+
<AvatarImage src={session.data?.user.image} />
36+
<AvatarFallback>{session.data?.user.name.charAt(0)}</AvatarFallback>
3837
</Avatar>
3938
</Link>
4039
) : (

calc-frontend/src/__tests__/App.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {expect, test} from 'vitest'
22
import {render, screen} from '@testing-library/react'
33
import { createMemoryRouter, createRoutesFromElements, RouterProvider } from 'react-router'
44

5-
import { AuthProvider } from '../App/AuthContext'
65
import RoutesList from '../App/RoutesList'
76
import { Toaster } from '@/components/ui/sonner'
87

@@ -13,10 +12,10 @@ test('full app rendering', () => {
1312
})
1413

1514
render(
16-
<AuthProvider>
15+
<>
1716
<Toaster />
1817
<RouterProvider router={router} />
19-
</AuthProvider>,
18+
</>,
2019
{}
2120
);
2221

calc-frontend/src/components/ProtectedRoute.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { Navigate, Outlet, } from "react-router";
2-
import { useAuth } from "../App/AuthContext";
2+
import { authClient } from "../lib/auth-client";
33
import { Blocks } from "react-loader-spinner";
44

55
function ProtectedRoute()
66
{
7-
const { session, isPending } = useAuth();
7+
const session = authClient.useSession();
88

99
// show loading if waiting for session info
10-
if(isPending){
10+
if(session.isPending){
1111
return <Blocks height="80" width="80" color="#4fa94d" ariaLabel="loading"
1212
wrapperStyle={{marginLeft:'auto', marginRight:'auto'}}
1313
wrapperClass="blocks-wrapper loading" visible={true}
1414
/>
1515
}
1616

1717
// user is not logged in - go to sign in page
18-
if(!session) {
18+
if(!session.data?.session) {
1919
return <Navigate to="/sign-in" replace />
2020
}
2121

calc-frontend/src/components/RedirectIfAuth.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Navigate, Outlet } from "react-router";
2-
import { useAuth } from "../App/AuthContext";
2+
import { authClient } from "../lib/auth-client";
33

44
function RedirectIfAuth()
55
{
6-
const { session, isPending } = useAuth();
6+
const session = authClient.useSession();
77

88
// user is not logged in - let them go to sign in page
9-
if(isPending || !session) {
9+
if(session.isPending || !session.data?.session) {
1010
return <Outlet/>
1111
}
1212

calc-frontend/src/components/ui/SaveFunctionButton.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useNavigate } from "react-router";
2-
import { useAuth } from "../../App/AuthContext";
2+
import { authClient } from "../../lib/auth-client";
33
import { Session } from "../../lib/auth-client";
44
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, TooltipArrow } from "@radix-ui/react-tooltip";
55

@@ -8,14 +8,14 @@ function SaveFunctionButton({onSave, saving, enableSave}: {onSave: (session: Ses
88
saving: boolean, enableSave: boolean})
99
{
1010
const navigate = useNavigate();
11-
const { session } = useAuth();
11+
const session = authClient.useSession();
1212

1313
const handleClick = () => {
14-
if(!session) {
14+
if(!session.data?.session ) {
1515
navigate("/sign-in");
1616
return;
1717
}
18-
onSave(session);
18+
onSave(session.data);
1919
}
2020

2121
if(saving){

calc-frontend/src/pages/Dashboard.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from "react";
2-
import { useAuth } from "../App/AuthContext";
2+
import { authClient } from "../lib/auth-client";
33
import FunctionCard from "../components/ui/FunctionCard";
44
import axios from "axios";
55
import { Blocks } from "react-loader-spinner";
@@ -19,23 +19,23 @@ function Dashboard()
1919
// each user starts with a userFunction array of size 0
2020
const [userFunctions, setUserFunctions] = useState<userFunction[]>([]);
2121

22-
const { session, isPending } = useAuth();
22+
const session = authClient.useSession();
2323

2424
const navigate = useNavigate()
2525

2626
const serverUrl = import.meta.env.VITE_SERVER_URL || 'http://localhost:3000'
2727

2828
useEffect(() => {
2929
// user is not logged in
30-
if(!isPending && !session){
30+
if(!session.isPending && !session.data?.session){
3131
navigate("/sign-in", { replace: true })
3232
return
3333
}
3434

3535
if(session){
3636
getFuncs();
3737
}
38-
}, [isPending]);
38+
}, [session.isPending]);
3939

4040
// show loading while checking if user is logged in
4141
if(!session){
@@ -46,7 +46,7 @@ function Dashboard()
4646
}
4747

4848
const getFuncs = async () => {
49-
const userId = session.user.id;
49+
const userId = session.data?.user.id;
5050
try {
5151
const response = await axios.get(serverUrl + `/func/all/${userId}`);
5252
setUserFunctions(response.data);
@@ -72,7 +72,7 @@ function Dashboard()
7272

7373
return (
7474
<div>
75-
<h2 className="mb-5 ml-[20px]">{session.user.name}'s Dashboard</h2>
75+
<h2 className="mb-5 ml-[20px]">{session.data?.user.name}'s Dashboard</h2>
7676

7777
{userFunctions.length > 0?
7878
<div className="dashboard">

calc-frontend/src/pages/Home.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useLayoutEffect, useRef } from "react";
22
import { Link } from "react-router"
3-
import { useAuth } from "../App/AuthContext";
3+
import { authClient } from "../lib/auth-client";
44
import SignInButton from "../components/ui/SignInButton";
55
import SignOutButton from "../components/ui/SignOutButton";
66
import derivativeVideo from '../animations/manim-videos/derivativeAnimation/1080p60/DerivativeAnimation.mp4';
77
import integralVideo from '../animations/manim-videos/integralAnimation/1080p60/RightRiemannSum.mp4';
88
import limitVideo from '../animations/manim-videos/limitsAnimation/1080p60/Limit.mp4';
99

1010
function Home() {
11-
const { session, isPending } = useAuth();
11+
const session = authClient.useSession();
1212

1313
const d1 = useRef(null);
1414
const d2 = useRef(null);
@@ -78,10 +78,10 @@ function Home() {
7878
<Link to="/dashboard" tabIndex={-1} style={{marginRight:'2px'}}>
7979
<button className="link-box cursor-pointer" style={{borderRadius:'5px'}}>Dashboard</button>
8080
</Link>
81-
{isPending === true?
81+
{session.isPending === true?
8282
null
8383
:
84-
!session ? <SignInButton/> : <SignOutButton/>
84+
!session.data?.session ? <SignInButton/> : <SignOutButton/>
8585
}
8686
</div>
8787

0 commit comments

Comments
 (0)