mirror of
https://github.com/Unleash/unleash.git
synced 2024-10-18 20:09:08 +02:00
213e8950d3
* refactor: allow existing tsc errors * refactor: add missing component key * refactor: port login auth to TS/SWR * refactor: replace incorrect CREATE_TAG_TYPE with UPDATE_TAG_TYPE * refactor: fix AccessProvider permission mocks * refactor: add types to AccessContext * refactor: fix file extension * refactor: remove default export * refactor: remove unused IAddedUser interface * refactor: comment on the permissions prop * refactor: split auth hooks * feat: auth tests * fix: setup separate e2e tests * fix: naming * fix: lint * fix: spec path * fix: missing store * feat: add more tests Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com>
85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import useSWR, { mutate } from 'swr';
|
|
import { useCallback } from 'react';
|
|
import { formatApiPath } from '../../../../utils/format-path';
|
|
import { IPermission, IUser } from '../../../../interfaces/user';
|
|
|
|
// The auth endpoint returns different things depending on the auth status.
|
|
// When the user is logged in, the endpoint returns user data and permissions.
|
|
// When the user is logged out, the endpoint returns details on how to log in.
|
|
type AuthEndpointResponse =
|
|
| IAuthEndpointUserResponse
|
|
| IAuthEndpointDetailsResponse;
|
|
|
|
export interface IAuthEndpointUserResponse {
|
|
user: IUser;
|
|
feedback: IAuthFeedback[];
|
|
permissions: IPermission[];
|
|
splash: IAuthSplash;
|
|
}
|
|
|
|
export interface IAuthEndpointDetailsResponse {
|
|
type: string;
|
|
path: string;
|
|
message: string;
|
|
defaultHidden: boolean;
|
|
options: IAuthOptions[];
|
|
}
|
|
|
|
export interface IAuthOptions {
|
|
type: string;
|
|
message: string;
|
|
path: string;
|
|
}
|
|
|
|
export interface IAuthFeedback {
|
|
neverShow: boolean;
|
|
feedbackId: string;
|
|
given?: string;
|
|
userId: number;
|
|
}
|
|
|
|
export interface IAuthSplash {
|
|
[key: string]: boolean;
|
|
}
|
|
|
|
interface IUseAuthEndpointOutput {
|
|
data?: AuthEndpointResponse;
|
|
refetchAuth: () => void;
|
|
loading: boolean;
|
|
error?: Error;
|
|
}
|
|
|
|
// This helper hook returns the raw response data from the user auth endpoint.
|
|
// Check out the other hooks in this directory for more ergonomic alternatives.
|
|
export const useAuthEndpoint = (): IUseAuthEndpointOutput => {
|
|
const { data, error } = useSWR<AuthEndpointResponse>(
|
|
USER_ENDPOINT_PATH,
|
|
fetchAuthStatus,
|
|
swrConfig
|
|
);
|
|
|
|
const refetchAuth = useCallback(() => {
|
|
mutate(USER_ENDPOINT_PATH).catch(console.warn);
|
|
}, []);
|
|
|
|
return {
|
|
data,
|
|
refetchAuth,
|
|
loading: !error && !data,
|
|
error,
|
|
};
|
|
};
|
|
|
|
const fetchAuthStatus = (): Promise<AuthEndpointResponse> => {
|
|
return fetch(USER_ENDPOINT_PATH).then(res => res.json());
|
|
};
|
|
|
|
const swrConfig = {
|
|
revalidateIfStale: false,
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
refreshInterval: 15000,
|
|
};
|
|
|
|
export const USER_ENDPOINT_PATH = formatApiPath(`api/admin/user`);
|