import React, { useState } from 'react';
import '../common/styles.css';
import '../../styles/base.scss';
import {
Box,
Button,
Flashbar,
Grid,
HelpPanel,
SpaceBetween
} from "@cloudscape-design/components";
import { Navigation } from "../common/navigation";
import { CustomAppLayout } from "../common/app-layout";
import Input from "@cloudscape-design/components/input";
import { storeToken } from "../../redux/actions";
import { Auth } from "aws-amplify";
import { useHistory } from "react-router-dom";
import { useDispatch } from "react-redux";
import { v4 as uuid4 } from "uuid";
import jwt_decode from 'jwt-decode'
export default class LoginView extends React.Component {
render() {
return (
}
navigationOpen={false}
content={}
contentType="default"
tools={}
toolsHide={false}
// labels={appLayoutNavigationLabels}
/>
);
}
}
export const ToolsContent = () => (
###APP_TITLE###}
footer={
<>
>
}
>
This solution demonstrates ###APP_TITLE###.
);
// The content in the main content area of the App layout
export function LoginContent() {
const history = useHistory();
const dispatch = useDispatch();
const [user, setUser] = React.useState("");
const [password, setPassword] = React.useState("");
const [notifications, setNotifications] = useState([]);
const addNotification = (message: string) => {
const list = []
for (let notification of notifications) {
list.push(notification)
}
list.push({
type: 'error',
content: message,
statusIconAriaLabel: 'error',
dismissLabel: 'Dismiss all messages',
dismissible: true,
onDismiss: () => setNotifications([]),
id: uuid4(),
});
setNotifications(list);
};
const login = () => {
try {
Auth.signIn(user, password).then(
(result) => {
Auth.currentAuthenticatedUser()
.then((data) => {
let decodedToken = jwt_decode(data.signInUserSession.idToken["jwtToken"]);
//console.log("Token : " + JSON.stringify(data.signInUserSession.idToken["jwtToken"]))
if ((decodedToken["cognito:groups"] != undefined) && (decodedToken["cognito:groups"].length > 0) && (decodedToken["cognito:groups"][0].includes("admin"))) {
dispatch(storeToken(data.signInUserSession.idToken["jwtToken"]))
history.push("/");
}
else {
addNotification("User not member of admin group.")
}
});
}).catch(error => {
console.log("Got error in login function");
console.log(error);
addNotification("Incorrect username or password.")
});
} catch (error) {
console.log("Got error in login function");
console.log(error);
}
}
return (
###APP_TITLE###
This solution demonstrates ###APP_TITLE###.
);
}