Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
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
3 changes: 2 additions & 1 deletion clients/ops/admin-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "next build && next export",
"start": "next start",
"lint": "eslint . --ext .ts,.tsx",
"lint:fix": "eslint . --fix --ext .ts,.tsx",
"format": "prettier --write src/ __tests__/",
"format:ci": "prettier --check src/ __tests__/",
"test": "jest --watch",
Expand Down Expand Up @@ -70,4 +71,4 @@
"msw": {
"workerDirectory": "public"
}
}
}
16 changes: 14 additions & 2 deletions clients/ops/admin-ui/src/features/auth/auth.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { BASE_URL, STORED_CREDENTIALS_KEY } from "../../constants";
import { addCommonHeaders } from "../common/CommonHeaders";
import { utf8ToB64 } from "../common/utils";
import { User } from "../user-management/types";
import { LoginRequest, LoginResponse } from "./types";
import {
LoginRequest,
LoginResponse,
LogoutRequest,
LogoutResponse,
} from "./types";

export interface AuthState {
user: User | null;
Expand Down Expand Up @@ -93,8 +98,15 @@ export const authApi = createApi({
}),
invalidatesTags: () => ["Auth"],
}),
logout: build.mutation<LogoutResponse, LogoutRequest>({
query: () => ({
url: "logout",
method: "POST",
}),
invalidatesTags: () => ["Auth"],
}),
}),
});

export const { useLoginMutation } = authApi;
export const { useLoginMutation, useLogoutMutation } = authApi;
export const { reducer } = authSlice;
3 changes: 3 additions & 0 deletions clients/ops/admin-ui/src/features/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export interface LoginResponse {
access_token: string;
};
}

export interface LogoutRequest {}
export interface LogoutResponse {}
20 changes: 12 additions & 8 deletions clients/ops/admin-ui/src/features/common/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ import React from "react";
import { useDispatch, useSelector } from "react-redux";

import { INDEX_ROUTE } from "../../constants";
import { logout, selectUser } from "../auth";
import { logout, selectUser, useLogoutMutation } from "../auth";
import { UserIcon } from "./Icon";
import Image from "./Image";

const useHeader = () => {
const dispatch = useDispatch();
const handleLogout = () => dispatch(logout());
const { username } = useSelector(selectUser) ?? { username: "" };
return {
handleLogout,
username,
};
return { username };
};

const Header: React.FC = () => {
const { handleLogout, username } = useHeader();
const { username } = useHeader();
const [logoutMutation] = useLogoutMutation();
const dispatch = useDispatch();

const handleLogout = async () => {
logoutMutation({})
.unwrap()
.then(() => dispatch(logout()));
};

return (
<header>
<Flex
Expand Down