Skip to content

Commit

Permalink
fix check permittion request
Browse files Browse the repository at this point in the history
  • Loading branch information
akiyatomohiro committed Jul 25, 2024
1 parent d07e474 commit 87c7598
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
74 changes: 42 additions & 32 deletions web/src/services/api/cerbosApi.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
import { useState, useCallback } from "react";

import { useAuth } from "@reearth/services/auth";

export default () => {
const [data, setData] = useState<any>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const { getAccessToken } = useAuth();

const checkPermission = useCallback(
async (resource: string, action: string) => {
setLoading(true);
setError(null);

try {
const accessToken = await getAccessToken();

const checkPermission = useCallback(async (roles: string[], action: string) => {
setLoading(true);
setError(null);
try {
const response = await fetch("http://localhost:8090/api/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `
query CheckPermission($input: CheckPermissionInput!) {
checkPermission(input: $input) {
allowed
const response = await fetch("http://localhost:8090/api/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
query: `
query CheckPermission($input: CheckPermissionInput!) {
checkPermission(input: $input) {
allowed
}
}
}
`,
variables: {
input: {
resource: "visualizer",
roles,
action,
`,
variables: {
input: {
service: "visualizer",
resource,
action,
},
},
},
}),
});
}),
});

const result = await response.json();
setData(result.data);
} catch (err) {
setError(err as Error);
} finally {
setLoading(false);
}
}, []);
const result = await response.json();
setData(result.data);
} catch (err) {
setError(err as Error);
} finally {
setLoading(false);
}
},
[getAccessToken],
);

return {
checkPermission,
Expand Down
2 changes: 1 addition & 1 deletion web/src/services/theme/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Provider: React.FC<{ children?: ReactNode }> = ({ children }) => {
const { checkPermission, data, loading, error } = useCerbosFetcher();

useEffect(() => {
checkPermission(["writer"], "read");
checkPermission("dashboard", "read");
}, [checkPermission]);

if (loading) {
Expand Down

0 comments on commit 87c7598

Please sign in to comment.