// eslint-disable-next-line import React, { useState, useCallback } from "react"; import { connect } from 'react-redux'; import { Redirect } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; import Paper from '@material-ui/core/Paper'; import Typography from '@material-ui/core/Typography'; import { AuthenticationService } from 'api'; import { RegistrationDialog, RequestPasswordResetDialog, ResetPasswordDialog } from 'dialogs'; import { LoginForm } from 'forms'; import { useReduxEffect } from 'hooks'; import { Images } from 'images'; import { HostDTO } from 'services'; import { RouteEnum, WebSocketConnectOptions, getHostPort } from 'types'; import { ServerSelectors, ServerTypes } from 'store'; import './Login.css'; const useStyles = makeStyles(theme => ({ root: { '& .login-content__header': { color: theme.palette.success.light }, '& .login-content__description': { backgroundColor: theme.palette.primary.main, color: theme.palette.primary.contrastText, }, '& .login-content__description-bar': { backgroundColor: theme.palette.primary.dark, }, '& .login-content__description-cards__card': { backgroundColor: theme.palette.background.paper, color: theme.palette.primary.main, }, [theme.breakpoints.up('lg')]: { '& .login-content': { maxWidth: '1000px', }, '& .login-content__form': { width: '50%', }, '& .login-content__description': { width: '50%', display: 'flex', }, }, }, })); const Login = ({ state, description }: LoginProps) => { const classes = useStyles(); const isConnected = AuthenticationService.isConnected(state); const [hostIdToRemember, setHostIdToRemember] = useState(null); const [dialogState, setDialogState] = useState({ passwordResetRequestDialog: false, resetPasswordDialog: false, registrationDialog: false, }); const [userToResetPassword, setUserToResetPassword] = useState(null); useReduxEffect(() => { closeRequestPasswordResetDialog(); openResetPasswordDialog(); }, ServerTypes.RESET_PASSWORD_REQUESTED, []); useReduxEffect(() => { closeResetPasswordDialog(); }, ServerTypes.RESET_PASSWORD_SUCCESS, []); useReduxEffect(({ options: { hashedPassword } }) => { if (hostIdToRemember) { HostDTO.get(hostIdToRemember).then(host => { host.hashedPassword = hashedPassword; host.save(); }); } }, ServerTypes.LOGIN_SUCCESSFUL, [hostIdToRemember]); const showDescription = () => { return !isConnected && description?.length; }; const onSubmitLogin = useCallback((loginForm) => { const { userName, password, selectedHost, selectedHost: { id: hostId, hashedPassword }, remember } = loginForm; updateHost(loginForm); if (remember) { setHostIdToRemember(hostId); } const options: WebSocketConnectOptions = { ...getHostPort(selectedHost), userName, password }; if (!password) { options.hashedPassword = hashedPassword; } AuthenticationService.login(options as WebSocketConnectOptions); }, []); const updateHost = ({ selectedHost, userName, hashedPassword, remember }) => { HostDTO.get(selectedHost.id).then(hostDTO => { hostDTO.remember = remember; hostDTO.userName = remember ? userName : null; hostDTO.hashedPassword = remember ? hashedPassword : null; hostDTO.save(); }); }; const handleRegistrationDialogSubmit = (form) => { const { userName, password, email, country, realName, selectedHost } = form; AuthenticationService.register({ ...getHostPort(selectedHost), userName, password, email, country, realName, }); }; const handleRequestPasswordResetDialogSubmit = (form) => { const { userName, email, selectedHost } = form; const { host, port } = getHostPort(selectedHost); if (email) { AuthenticationService.resetPasswordChallenge({ userName, email, host, port } as any); } else { setUserToResetPassword(userName); AuthenticationService.resetPasswordRequest({ userName, host, port } as any); } }; const handleResetPasswordDialogSubmit = ({ userName, token, newPassword, selectedHost }) => { const { host, port } = getHostPort(selectedHost); AuthenticationService.resetPassword({ userName, token, newPassword, host, port } as any); }; const skipTokenRequest = (userName) => { setUserToResetPassword(userName); setDialogState(s => ({ ...s, passwordResetRequestDialog: false, resetPasswordDialog: true, })); }; const closeRequestPasswordResetDialog = () => { setDialogState(s => ({ ...s, passwordResetRequestDialog: false })); } const openRequestPasswordResetDialog = () => { setDialogState(s => ({ ...s, passwordResetRequestDialog: true })); } const closeResetPasswordDialog = () => { setDialogState(s => ({ ...s, resetPasswordDialog: false })); } const openResetPasswordDialog = () => { setDialogState(s => ({ ...s, resetPasswordDialog: true })); } const closeRegistrationDialog = () => { setDialogState(s => ({ ...s, registrationDialog: false })); } const openRegistrationDialog = () => { setDialogState(s => ({ ...s, registrationDialog: true })); } return (
Play multiplayer card games online.
Cross-platform virtual tabletop for multiplayer card games. Forever free.