2021-03-14 14:51:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
import {Button} from '../../elements/GPElements/Button';
|
|
|
|
import style from './AuthenticationPage.module.css';
|
2021-03-16 20:44:32 +01:00
|
|
|
import {addKeyHandler, removeKeyHandler} from '../../utils/ShortkeyHandler';
|
2021-03-22 19:07:32 +00:00
|
|
|
import {faTimes} from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
2021-09-19 23:20:37 +02:00
|
|
|
import {LoginContext, LoginState} from '../../utils/context/LoginContext';
|
|
|
|
import {APINode, callApiUnsafe} from '../../utils/Api';
|
|
|
|
import {cookie, Token} from '../../utils/context/Cookie';
|
2021-03-14 12:49:24 +00:00
|
|
|
|
|
|
|
interface state {
|
2021-03-14 14:51:53 +00:00
|
|
|
pwdText: string;
|
2021-03-22 19:07:32 +00:00
|
|
|
wrongPWDInfo: boolean;
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
interface Props {
|
2021-03-22 19:07:32 +00:00
|
|
|
onSuccessLogin: () => void;
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
class AuthenticationPage extends React.Component<Props, state> {
|
|
|
|
constructor(props: Props) {
|
2021-03-14 12:49:24 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2021-03-22 19:07:32 +00:00
|
|
|
pwdText: '',
|
|
|
|
wrongPWDInfo: false
|
2021-03-14 14:51:53 +00:00
|
|
|
};
|
2021-03-16 20:44:32 +01:00
|
|
|
|
|
|
|
this.keypress = this.keypress.bind(this);
|
2021-03-22 19:07:32 +00:00
|
|
|
this.authenticate = this.authenticate.bind(this);
|
2021-03-16 20:44:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(): void {
|
|
|
|
addKeyHandler(this.keypress);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount(): void {
|
|
|
|
removeKeyHandler(this.keypress);
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 23:20:37 +02:00
|
|
|
static contextType = LoginContext;
|
|
|
|
|
2021-03-14 12:49:24 +00:00
|
|
|
render(): JSX.Element {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className={style.openmediacenterlabel}>OpenMediaCenter</div>
|
|
|
|
<div className={style.main}>
|
|
|
|
<div className={style.loginText}>Login</div>
|
|
|
|
<div>
|
2021-03-14 14:51:53 +00:00
|
|
|
<input
|
|
|
|
className={style.input}
|
|
|
|
placeholder='Password'
|
|
|
|
type='password'
|
|
|
|
onChange={(ch): void => this.setState({pwdText: ch.target.value})}
|
|
|
|
value={this.state.pwdText}
|
|
|
|
/>
|
2021-03-22 19:07:32 +00:00
|
|
|
{this.state.wrongPWDInfo ? (
|
|
|
|
<div>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
style={{
|
|
|
|
color: 'red',
|
|
|
|
marginRight: '7px'
|
|
|
|
}}
|
|
|
|
icon={faTimes}
|
|
|
|
size='1x'
|
|
|
|
/>
|
|
|
|
wrong password!
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-03-14 12:49:24 +00:00
|
|
|
</div>
|
2021-03-22 19:07:32 +00:00
|
|
|
<div className={style.submitbtn}>
|
|
|
|
<Button title='Submit' onClick={this.authenticate} />
|
2021-03-14 12:49:24 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2021-03-16 20:44:32 +01:00
|
|
|
|
2021-03-22 19:07:32 +00:00
|
|
|
/**
|
|
|
|
* request a new token and check if pwd was valid
|
|
|
|
*/
|
|
|
|
authenticate(): void {
|
2021-09-19 23:20:37 +02:00
|
|
|
callApiUnsafe(
|
|
|
|
APINode.Login,
|
|
|
|
{action: 'login', Password: this.state.pwdText},
|
|
|
|
(r: Token) => {
|
|
|
|
cookie.Store(r);
|
2021-03-22 19:07:32 +00:00
|
|
|
|
2021-09-19 23:20:37 +02:00
|
|
|
this.context.setLoginState(LoginState.LoggedIn);
|
2021-03-22 19:07:32 +00:00
|
|
|
},
|
2021-09-19 23:20:37 +02:00
|
|
|
(e) => {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2021-03-22 19:07:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:44:32 +01:00
|
|
|
/**
|
|
|
|
* key event handling
|
|
|
|
* @param event keyevent
|
|
|
|
*/
|
|
|
|
keypress(event: KeyboardEvent): void {
|
|
|
|
// hide if escape is pressed
|
|
|
|
if (event.key === 'Enter') {
|
2021-03-22 19:07:32 +00:00
|
|
|
// call submit
|
|
|
|
this.authenticate();
|
2021-03-16 20:44:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 14:51:53 +00:00
|
|
|
export default AuthenticationPage;
|