Skip to content

Commit

Permalink
feat(auth): display auth error message in user menu
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverDudgeon committed Jun 19, 2023
1 parent 06a1e21 commit 5b295a1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 53 deletions.
72 changes: 42 additions & 30 deletions layouts/navigation/UserMenuContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Person as PersonIcon } from "@mui/icons-material";
import { Box, Chip, Typography, useMediaQuery, useTheme } from "@mui/material";
import { Alert, Box, Chip, Typography, useMediaQuery, useTheme } from "@mui/material";

import { AuthButton } from "../../components/auth/AuthButton";
import { CenterLoader } from "../../components/CenterLoader";
Expand All @@ -12,44 +12,56 @@ import { useKeycloakUser } from "../../hooks/useKeycloakUser";
* Content of the user menu
*/
export const UserMenuContent = () => {
const { user, isLoading } = useKeycloakUser();
const asRole = useASAuthorizationStatus();
const dmRole = useDMAuthorizationStatus();

const theme = useTheme();

const biggerThanMd = useMediaQuery(theme.breakpoints.up("md"));

return (
<Box textAlign={biggerThanMd ? "center" : undefined}>
<Typography gutterBottom variant="h3">
Account
</Typography>
{isLoading ? (
<CenterLoader />
) : user.username ? (
<>
{biggerThanMd && (
<Box sx={{ fontSize: 80 }}>
<PersonIcon color="disabled" fontSize="inherit" />
</Box>
)}
<Typography fontWeight="bold">{user.username}</Typography>
<Typography>
Roles:
<Chips>
<Chip label={dmRole ?? "No DM Role"} size="small" />
<Chip label={asRole ?? "No AS Role"} size="small" />
</Chips>
</Typography>
<AuthButton mode="logout" sx={{ marginY: 1 }} />
</>
) : (
<Typography>
<AuthButton mode="login" />
</Typography>
)}
<UserMenuContentInner />
<DarkModeSwitch />
</Box>
);
};

const UserMenuContentInner = () => {
const asRole = useASAuthorizationStatus();
const dmRole = useDMAuthorizationStatus();
const { user, isLoading, error } = useKeycloakUser();

const theme = useTheme();
const biggerThanMd = useMediaQuery(theme.breakpoints.up("md"));

if (error) {
return <Alert severity="error" title={error.message} />;
}

if (isLoading) {
return <CenterLoader />;
}

if (user.username) {
return (
<>
{biggerThanMd && (
<Box sx={{ fontSize: 80 }}>
<PersonIcon color="disabled" fontSize="inherit" />
</Box>
)}
<Typography fontWeight="bold">{user.username}</Typography>
<Typography>
Roles:
<Chips>
<Chip label={dmRole ?? "No DM Role"} size="small" />
<Chip label={asRole ?? "No AS Role"} size="small" />
</Chips>
</Typography>
<AuthButton mode="logout" sx={{ marginY: 1 }} />
</>
);
}

return <AuthButton mode="login" />;
};
22 changes: 0 additions & 22 deletions pages/api/auth/[...auth0].ts

This file was deleted.

22 changes: 22 additions & 0 deletions pages/api/auth/[auth0].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { handleAuth, handleLogin } from "@auth0/nextjs-auth0";
import { captureException } from "@sentry/nextjs";

export default handleAuth({
login: handleLogin({
authorizationParams: {
// Add the `offline_access` scope to also get a Refresh Token
scope: "openid profile email offline_access",
},
}),
onError(_req, res, error) {
console.error(error);
res.status(error.status || 500).end();
captureException(error);
},
});

export const config = {
api: {
externalResolver: true,
},
};
2 changes: 1 addition & 1 deletion types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module "nextjs-routes" {

export type Route =
| DynamicRoute<"/api/as-api/[...asProxy]", { "asProxy": string[] }>
| DynamicRoute<"/api/auth/[...auth0]", { "auth0": string[] }>
| DynamicRoute<"/api/auth/[auth0]", { "auth0": string }>
| StaticRoute<"/api/configuration/ui-version">
| DynamicRoute<"/api/dm-api/[...dmProxy]", { "dmProxy": string[] }>
| DynamicRoute<"/api/viewer-proxy/[...viewerProxy]", { "viewerProxy": string[] }>
Expand Down

0 comments on commit 5b295a1

Please sign in to comment.