Skip to content

Commit

Permalink
🔥 wip: add namespace support
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
zcubbs committed Oct 24, 2023
1 parent 3945c8c commit 57562ff
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/server/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ function App() {
<Route path="/login" element={<LoginPage/>}/>
<Route path="/register" element={<RegisterPage/>}/>
<Route path="unauthorized" element={<Unauthorized />} />
<Route element={<RequireAuth allowedRole={ROLES.User}/>}>
<Route element={<RequireAuth allowedRoles={[ROLES.User, ROLES.Admin]}/>}>
<Route path="/" element={<Content/>}/>
<Route path="/domains" element={<Content/>}/>
</Route>
<Route element={<RequireAuth allowedRole={ROLES.Admin}/>}>
<Route element={<RequireAuth allowedRoles={[ROLES.Admin]}/>}>
<Route path="/users" element={<UsersPage/>}/>
</Route>
<Route path="/logout" element={<h1>Logout</h1>}/>
Expand Down
3 changes: 3 additions & 0 deletions cmd/server/web/src/components/require-auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const RequireAuth = ({ allowedRoles }: RequireAuthProps): ReactElement | null =>
const { auth } = useAuth();
const location = useLocation();

console.log("RequireAuth: auth", auth);
console.log("RequireAuth: allowedRoles", allowedRoles);

// If the user is not authenticated, redirect to login
if (!auth?.user) {
return <Navigate to="/login" state={{ from: location }} replace />;
Expand Down
10 changes: 6 additions & 4 deletions cmd/server/web/src/context/auth-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContext, PropsWithChildren, useState, ReactNode } from "react";
import {createContext, Dispatch, PropsWithChildren, SetStateAction, useMemo, useState} from "react";

interface AuthContextProps {
auth?: Auth;
setAuth?: React.Dispatch<React.SetStateAction<Auth | undefined>>;
setAuth?: Dispatch<SetStateAction<Auth | undefined>>;
}

const AuthContext = createContext<AuthContextProps>({});
Expand All @@ -24,11 +24,13 @@ type Auth = {
refresh_token_expires_at: string;
}

export const AuthProvider = ({ children }: PropsWithChildren<{}>): ReactNode => {
export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
const [auth, setAuth] = useState<Auth>();

const value = useMemo(() => ({ auth, setAuth }), [auth, setAuth]);

return (
<AuthContext.Provider value={{ auth, setAuth }}>
<AuthContext.Provider value={value}>
{children}
</AuthContext.Provider>
);
Expand Down
9 changes: 0 additions & 9 deletions cmd/server/web/src/hooks/auth.tsx

This file was deleted.

18 changes: 9 additions & 9 deletions cmd/server/web/src/hooks/use-refresh-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import useAuth from "@/hooks/use-auth.ts";
const useRefreshToken = () => {
const { setAuth } = useAuth();

const refresh = async () => {
return async () => {
const response = await axios.post('/api/v1/refresh_token', {
withCredentials: true
});
setAuth((prev: any) => {
console.log(JSON.stringify(prev));
console.log(response.data.accessToken);
return { ...prev, accessToken: response.data.accessToken }
});
if (setAuth) {
setAuth((prev: any) => {
console.log(JSON.stringify(prev));
console.log(response.data.accessToken);
return {...prev, accessToken: response.data.accessToken}
});
}
return response.data.accessToken;
}

return refresh;
};
}

export default useRefreshToken;
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export function UserLoginForm({className, ...props}: UserLoginFormProps) {

if (response.ok) {
response.json().then((data) => {
setAuth(data)
if (setAuth) {
setAuth(data)
}
});
navigate(from, { replace: true });
}
Expand Down

0 comments on commit 57562ff

Please sign in to comment.