Skip to content

Commit 87122b0

Browse files
author
shausing
committed
AMBARI-26425: signout feature and showing logged in user
1 parent 4119417 commit 87122b0

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { faUser } from "@fortawesome/free-solid-svg-icons";
2+
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
3+
import { useEffect, useState } from "react";
4+
import {
5+
Container,
6+
Navbar,
7+
Nav,
8+
Dropdown,
9+
DropdownDivider,
10+
} from "react-bootstrap";
11+
import {
12+
decryptData,
13+
getFromLocalStorage,
14+
parseJSONData
15+
} from "./api/Utility.ts";
16+
import { get } from "lodash";
17+
import AmbariAboutModal from "./AmbariAboutModal";
18+
19+
type NavBarProps = {
20+
subPath: string;
21+
clusterName: string;
22+
};
23+
24+
export default function NavBar({ subPath, clusterName }: NavBarProps) {
25+
const [showAmbariAboutModal, setShowAmbariAboutModal] = useState(false);
26+
const [loginUserName, setLoginUserName] = useState("");
27+
const [ambariLsVal, setAmbariLsVal] = useState(null);
28+
29+
useEffect(() => {
30+
let ambariKey = getFromLocalStorage('ambari');
31+
if (ambariKey) {
32+
setAmbariLsVal(parseJSONData(decryptData(ambariKey)));
33+
}
34+
}, []);
35+
36+
useEffect(() => {
37+
if (ambariLsVal) {
38+
const loginName = get(ambariLsVal, 'app.loginName');
39+
if (loginName) {
40+
setLoginUserName(loginName);
41+
}
42+
}
43+
}, [ambariLsVal]);
44+
45+
return (
46+
<div>
47+
{showAmbariAboutModal ? (
48+
<AmbariAboutModal
49+
isOpen={showAmbariAboutModal}
50+
onClose={() => setShowAmbariAboutModal(false)}
51+
/>
52+
) : null}
53+
<Navbar collapseOnSelect expand="lg" className="bg-white">
54+
<Container className="d-flex justify-content-between">
55+
<Navbar.Brand
56+
className="text-black m-0 breadcrumb d-flex align-items-center"
57+
style={{ fontSize: 24 }}
58+
>
59+
{" "}
60+
Admin /
61+
<div className="navbar-text ms-1" style={{ fontSize: 24 }}>
62+
{subPath}
63+
</div>
64+
</Navbar.Brand>
65+
<div className="d-flex align-items-center ">
66+
<Nav.Link className="navbar-text navbar-size">
67+
{clusterName}
68+
</Nav.Link>
69+
<Dropdown>
70+
<Dropdown.Toggle
71+
variant="transparent"
72+
className="d-flex align-items-center border-0 ms-2"
73+
>
74+
<FontAwesomeIcon
75+
icon={faUser}
76+
className="me-1 navbar-text navbar-size"
77+
/>
78+
<div className="navbar-text navbar-size">{loginUserName}</div>
79+
</Dropdown.Toggle>
80+
81+
<Dropdown.Menu className="rounded-0">
82+
<Dropdown.Item
83+
onClick={() => {
84+
setShowAmbariAboutModal(true);
85+
}}
86+
>
87+
About
88+
</Dropdown.Item>
89+
<DropdownDivider />
90+
<Dropdown.Item>Signout</Dropdown.Item>
91+
</Dropdown.Menu>
92+
</Dropdown>
93+
</div>
94+
</Container>
95+
</Navbar>
96+
</div>
97+
);
98+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { encryptData, decryptData, getFromLocalStorage, parseJSONData, setInLocalStorage } from "./Utility.ts";
2+
import { adminApi } from "./configs/axiosConfig.ts";
3+
import { AxiosError } from 'axios';
4+
5+
const signOut = async () => {
6+
// var data = JSON.parse(decryptData(localStorage.getItem("ambari")));
7+
let ambariKey = getFromLocalStorage('ambari');
8+
let data;
9+
if (ambariKey) {
10+
data = parseJSONData(decryptData(ambariKey));
11+
}
12+
delete data.app.authenticated;
13+
delete data.app.loginName;
14+
delete data.app.user;
15+
16+
//with encrypting set data in LS
17+
setInLocalStorage('ambari', encryptData(JSON.stringify(data)));
18+
19+
const headers = {
20+
'Authorization': 'Basic ' + 'invalid_username:password'
21+
};
22+
23+
try {
24+
const url = "/logout"
25+
await adminApi.request({
26+
url: url,
27+
method: 'GET',
28+
headers: headers
29+
});
30+
} catch (error) {
31+
const axiosError = error as AxiosError;
32+
throw new Error(`Logout failed with status: ${axiosError.response?.status}`);
33+
}
34+
}
35+
export default signOut;

0 commit comments

Comments
 (0)