Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display errors correctly on Unauthenticated-Status #1624

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ export const createErrorDialogApolloLink = () => {
let title: React.ReactNode | undefined;
let userMessage: React.ReactNode | undefined;
let httpStatus: string | undefined;
let isUnauthenticated = false;

if (graphQLErrors) {
error = graphQLErrors.map(({ message }) => message);
errorType = "graphql";
title = <FormattedMessage id="comet.errorDialog.graphQLErrors.title" defaultMessage="Server error" />;
isUnauthenticated = graphQLErrors.some(
(e) => e.extensions?.exception?.status === StatusCodes.UNAUTHORIZED || e.extensions?.code === "UNAUTHENTICATED",
);
}

if (networkError) {
Expand All @@ -41,23 +45,24 @@ export const createErrorDialogApolloLink = () => {
if (isServerError(networkError)) {
const { statusCode } = networkError;
httpStatus = `${statusCode} ${getReasonPhrase(statusCode)}`;

if (statusCode === StatusCodes.UNAUTHORIZED) {
title = <FormattedMessage id="comet.errorDialog.sessionExpired.title" defaultMessage="Session expired" />;
userMessage = (
<>
<Typography gutterBottom>
<FormattedMessage id="comet.errorDialog.sessionExpired.message" defaultMessage="Your login-session has expired." />
</Typography>
<Button href="/" color="info" variant="outlined">
<FormattedMessage id="comet.errorDialog.sessionExpired.button" defaultMessage="Re-login" />
</Button>
</>
);
}
isUnauthenticated = statusCode === StatusCodes.UNAUTHORIZED;
}
}

if (isUnauthenticated) {
title = <FormattedMessage id="comet.errorDialog.sessionExpired.title" defaultMessage="Session expired" />;
userMessage = (
<>
<Typography gutterBottom>
<FormattedMessage id="comet.errorDialog.sessionExpired.message" defaultMessage="Your login-session has expired." />
</Typography>
<Button href="/" color="info" variant="outlined">
<FormattedMessage id="comet.errorDialog.sessionExpired.button" defaultMessage="Re-login" />
</Button>
</>
);
}

errorDialogVar({
title,
error: error ?? "Unknown error",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { gql, useQuery } from "@apollo/client";
import { Loading } from "@comet/admin";
import { Button, Typography } from "@mui/material";
import isEqual from "lodash.isequal";
import React from "react";

Expand Down Expand Up @@ -38,7 +39,25 @@ export const CurrentUserProvider: React.FC<{
}
`);

if (error) throw error.message;
// As this Provider is very high up in the tree, don't rely on ErrorBoundary or a configured intl-Provider here
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved
if (error) {
const isUnauthenticated = error.graphQLErrors.some(
(e) => e.extensions?.exception?.status === 401 || e.extensions?.code === "UNAUTHENTICATED",
);
return (
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved
<>
<Typography gutterBottom>{isUnauthenticated ? "Your access-token is invalid. Re-login might help." : error.message}</Typography>
{isUnauthenticated && (
<Button href="/" color="info" variant="outlined">
{
// eslint-disable-next-line @calm/react-intl/missing-formatted-message
}
Re-Login
</Button>
)}
</>
);
}

if (!data) return <Loading behavior="fillPageHeight" />;

Expand Down
6 changes: 3 additions & 3 deletions packages/api/cms-api/src/auth/guards/comet.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CanActivate, ExecutionContext, HttpException, Injectable, mixin } from "@nestjs/common";
import { CanActivate, ExecutionContext, Injectable, mixin, UnauthorizedException } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { GqlExecutionContext } from "@nestjs/graphql";
import { AuthGuard, IAuthGuard, Type } from "@nestjs/passport";
Expand All @@ -19,14 +19,14 @@ export function createCometAuthGuard(type?: string | string[]): Type<IAuthGuard>
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
handleRequest<CurrentUserInterface>(err: unknown, user: any): CurrentUserInterface {
handleRequest<CurrentUserInterface>(err: unknown, user: any, info: any): CurrentUserInterface {
if (err) {
throw err;
}
if (user) {
return user;
}
throw new HttpException("UserNotAuthenticated", 200);
throw new UnauthorizedException(info[0]?.message);
}

async canActivate(context: ExecutionContext): Promise<boolean> {
Expand Down
Loading