mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
Extract device code into a separate view.
Convert more components from class components to functional. Fix invalid finish state when client was auto approved
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
import React from 'react';
|
import React, { FC } from 'react';
|
||||||
import { Helmet } from 'react-helmet-async';
|
import { Helmet } from 'react-helmet-async';
|
||||||
import { FormattedMessage as Message, MessageDescriptor } from 'react-intl';
|
import { FormattedMessage as Message, MessageDescriptor } from 'react-intl';
|
||||||
|
|
||||||
export default function AuthTitle({ title }: { title: MessageDescriptor }) {
|
interface Props {
|
||||||
|
title: MessageDescriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AuthTitle: FC<Props> = ({ title }) => {
|
||||||
return (
|
return (
|
||||||
<Message {...title}>
|
<Message {...title}>
|
||||||
{(msg) => (
|
{(msg) => (
|
||||||
@@ -13,4 +17,6 @@ export default function AuthTitle({ title }: { title: MessageDescriptor }) {
|
|||||||
)}
|
)}
|
||||||
</Message>
|
</Message>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export default AuthTitle;
|
||||||
|
|||||||
@@ -94,7 +94,8 @@ interface OwnProps {
|
|||||||
Title: ReactElement;
|
Title: ReactElement;
|
||||||
Body: ReactElement;
|
Body: ReactElement;
|
||||||
Footer: ReactElement;
|
Footer: ReactElement;
|
||||||
Links: ReactNode;
|
Links?: ReactNode;
|
||||||
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Props extends OwnProps {
|
interface Props extends OwnProps {
|
||||||
@@ -255,6 +256,7 @@ class PanelTransition extends React.PureComponent<Props, State> {
|
|||||||
onSubmit={this.onFormSubmit}
|
onSubmit={this.onFormSubmit}
|
||||||
onInvalid={this.onFormInvalid}
|
onInvalid={this.onFormInvalid}
|
||||||
isLoading={this.props.auth.isLoading}
|
isLoading={this.props.auth.isLoading}
|
||||||
|
className={this.props.className}
|
||||||
>
|
>
|
||||||
<Panel>
|
<Panel>
|
||||||
<PanelHeader>{panels.map((config) => this.getHeader(config))}</PanelHeader>
|
<PanelHeader>{panels.map((config) => this.getHeader(config))}</PanelHeader>
|
||||||
@@ -285,10 +287,7 @@ class PanelTransition extends React.PureComponent<Props, State> {
|
|||||||
|
|
||||||
onFormSubmit = (): void => {
|
onFormSubmit = (): void => {
|
||||||
this.props.clearErrors();
|
this.props.clearErrors();
|
||||||
|
this.body?.onFormSubmit();
|
||||||
if (this.body) {
|
|
||||||
this.body.onFormSubmit();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
onFormInvalid = (errors: Record<string, ValidationError>): void => this.props.setErrors(errors);
|
onFormInvalid = (errors: Record<string, ValidationError>): void => this.props.setErrors(errors);
|
||||||
@@ -377,7 +376,7 @@ class PanelTransition extends React.PureComponent<Props, State> {
|
|||||||
|
|
||||||
if (length === 1) {
|
if (length === 1) {
|
||||||
if (!this.wasAutoFocused) {
|
if (!this.wasAutoFocused) {
|
||||||
this.body.autoFocus();
|
this.body?.autoFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.wasAutoFocused = true;
|
this.wasAutoFocused = true;
|
||||||
|
|||||||
@@ -87,6 +87,9 @@ describe('components/auth/actions', () => {
|
|||||||
[setClient(resp.client)],
|
[setClient(resp.client)],
|
||||||
[
|
[
|
||||||
setOAuthRequest({
|
setOAuthRequest({
|
||||||
|
params: {
|
||||||
|
userCode: '',
|
||||||
|
},
|
||||||
prompt: 'none',
|
prompt: 'none',
|
||||||
loginHint: undefined,
|
loginHint: undefined,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -382,7 +382,12 @@ export function oAuthComplete(params: { accept?: boolean } = {}) {
|
|||||||
localStorage.removeItem('oauthData');
|
localStorage.removeItem('oauthData');
|
||||||
|
|
||||||
if (!resp.redirectUri) {
|
if (!resp.redirectUri) {
|
||||||
dispatch(setOAuthCode({ success: resp.success && params.accept }));
|
dispatch(
|
||||||
|
setOAuthCode({
|
||||||
|
// if accept is undefined, then it was auto approved
|
||||||
|
success: resp.success && (typeof params.accept === 'undefined' || params.accept),
|
||||||
|
}),
|
||||||
|
);
|
||||||
} else if (resp.redirectUri.startsWith('static_page')) {
|
} else if (resp.redirectUri.startsWith('static_page')) {
|
||||||
const displayCode = resp.redirectUri.includes('static_page_with_code');
|
const displayCode = resp.redirectUri.includes('static_page_with_code');
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,32 @@
|
|||||||
import React from 'react';
|
import React, { FC } from 'react';
|
||||||
|
import { RouteComponentProps } from 'react-router-dom';
|
||||||
import { FormattedMessage as Message, defineMessages } from 'react-intl';
|
import { FormattedMessage as Message, defineMessages } from 'react-intl';
|
||||||
|
|
||||||
import factory from '../factory';
|
import { Button } from 'app/components/ui/form';
|
||||||
import Body from './DeviceCodeBody';
|
import AuthTitle from 'app/components/auth/AuthTitle';
|
||||||
|
import PanelTransition from 'app/components/auth/PanelTransition';
|
||||||
|
|
||||||
|
import style from './deviceCode.scss';
|
||||||
|
import DeviceCodeBody from './DeviceCodeBody';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
deviceCodeTitle: 'Device code',
|
deviceCodeTitle: 'Device Code',
|
||||||
});
|
});
|
||||||
|
|
||||||
export default factory({
|
const DeviceCode: FC<RouteComponentProps> = (props) => {
|
||||||
title: messages.deviceCodeTitle,
|
return (
|
||||||
body: Body,
|
<PanelTransition
|
||||||
footer: {
|
key="deviceCode"
|
||||||
color: 'green',
|
className={style.form}
|
||||||
children: <Message key="continueButton" defaultMessage="Continue" />,
|
Title={<AuthTitle title={messages.deviceCodeTitle} />}
|
||||||
},
|
Body={<DeviceCodeBody {...props} />}
|
||||||
});
|
Footer={
|
||||||
|
<Button type="submit">
|
||||||
|
<Message id="continue" defaultMessage="Cotinute" />
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeviceCode;
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { defineMessages } from 'react-intl';
|
import { FormattedMessage as Message } from 'react-intl';
|
||||||
|
|
||||||
import { Input } from 'app/components/ui/form';
|
import { Input } from 'app/components/ui/form';
|
||||||
import BaseAuthBody from 'app/components/auth/BaseAuthBody';
|
import BaseAuthBody from 'app/components/auth/BaseAuthBody';
|
||||||
|
|
||||||
const messages = defineMessages({
|
|
||||||
deviceCode: 'Device code',
|
|
||||||
});
|
|
||||||
|
|
||||||
export default class DeviceCodeBody extends BaseAuthBody {
|
export default class DeviceCodeBody extends BaseAuthBody {
|
||||||
static displayName = 'DeviceCodeBody';
|
static displayName = 'DeviceCodeBody';
|
||||||
static panelId = 'deviceCode';
|
static panelId = 'deviceCode';
|
||||||
@@ -16,16 +12,22 @@ export default class DeviceCodeBody extends BaseAuthBody {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<>
|
||||||
{this.renderErrors()}
|
{this.renderErrors()}
|
||||||
|
|
||||||
<Input
|
<Message id="deviceCode" defaultMessage="Device Code">
|
||||||
{...this.bindField('user_code')}
|
{(nodes) => (
|
||||||
icon="key"
|
<Input
|
||||||
required
|
{...this.bindField('user_code')}
|
||||||
placeholder={messages.deviceCode}
|
icon="key"
|
||||||
/>
|
name="user_cide"
|
||||||
</div>
|
autoFocus
|
||||||
|
required
|
||||||
|
placeholder={nodes as string}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Message>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
packages/app/components/auth/deviceCode/deviceCode.scss
Normal file
5
packages/app/components/auth/deviceCode/deviceCode.scss
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
.form {
|
||||||
|
max-width: 340px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 55px 13px 0;
|
||||||
|
}
|
||||||
@@ -1,121 +1,91 @@
|
|||||||
import React from 'react';
|
import React, { FC, PropsWithChildren, useState, useCallback } from 'react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { omit } from 'app/functions';
|
|
||||||
|
|
||||||
import styles from './panel.scss';
|
import styles from './panel.scss';
|
||||||
import icons from './icons.scss';
|
import icons from './icons.scss';
|
||||||
|
|
||||||
export function Panel(props: { title?: string; icon?: string; children: React.ReactNode }) {
|
interface PanelProps extends PropsWithChildren<any> {
|
||||||
const { title: titleText, icon: iconType } = props;
|
title?: string;
|
||||||
let icon: React.ReactElement | undefined;
|
icon?: string;
|
||||||
let title: React.ReactElement | undefined;
|
}
|
||||||
|
|
||||||
if (iconType) {
|
|
||||||
icon = (
|
|
||||||
<button className={styles.headerControl}>
|
|
||||||
<span className={icons[iconType]} />
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (titleText) {
|
|
||||||
title = (
|
|
||||||
<PanelHeader>
|
|
||||||
{icon}
|
|
||||||
{titleText}
|
|
||||||
</PanelHeader>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export const Panel: FC<PanelProps> = ({ title, icon, children }) => {
|
||||||
return (
|
return (
|
||||||
<div className={styles.panel}>
|
<div className={styles.panel}>
|
||||||
{title}
|
{title && (
|
||||||
|
<PanelHeader>
|
||||||
|
{icon && (
|
||||||
|
<button className={styles.headerControl}>
|
||||||
|
<span className={icons[icon]} />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{title}
|
||||||
|
</PanelHeader>
|
||||||
|
)}
|
||||||
|
|
||||||
{props.children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export function PanelHeader(props: { children: React.ReactNode }) {
|
export const PanelHeader: FC<PropsWithChildren<any>> = ({ children }) => {
|
||||||
return (
|
return (
|
||||||
<div className={styles.header} {...props} data-testid="auth-header">
|
<div className={styles.header} data-testid="auth-header">
|
||||||
{props.children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export function PanelBody(props: { children: React.ReactNode }) {
|
export const PanelBody: FC<PropsWithChildren<any>> = ({ children }) => {
|
||||||
return (
|
return (
|
||||||
<div className={styles.body} {...props} data-testid="auth-body">
|
<div className={styles.body} data-testid="auth-body">
|
||||||
{props.children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export function PanelFooter(props: { children: React.ReactNode }) {
|
export const PanelFooter: FC<PropsWithChildren<any>> = ({ children }) => {
|
||||||
return (
|
return (
|
||||||
<div className={styles.footer} {...props} data-testid="auth-controls">
|
<div className={styles.footer} data-testid="auth-controls">
|
||||||
{props.children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
interface PanelBodyHeaderProps extends PropsWithChildren<any> {
|
||||||
|
type?: 'default' | 'error';
|
||||||
|
onClose?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PanelBodyHeader extends React.Component<
|
export const PanelBodyHeader: FC<PanelBodyHeaderProps> = ({ type = 'default', onClose, children }) => {
|
||||||
{
|
const [isClosed, setIsClosed] = useState<boolean>(false);
|
||||||
type?: 'default' | 'error';
|
const handleCloseClick = useCallback(() => {
|
||||||
onClose?: () => void;
|
setIsClosed(true);
|
||||||
children: React.ReactNode;
|
onClose?.();
|
||||||
},
|
}, [onClose]);
|
||||||
{
|
|
||||||
isClosed: boolean;
|
|
||||||
}
|
|
||||||
> {
|
|
||||||
state: {
|
|
||||||
isClosed: boolean;
|
|
||||||
} = {
|
|
||||||
isClosed: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
return (
|
||||||
const { type = 'default', children } = this.props;
|
<div
|
||||||
|
className={clsx({
|
||||||
|
[styles.defaultBodyHeader]: type === 'default',
|
||||||
|
[styles.errorBodyHeader]: type === 'error',
|
||||||
|
[styles.isClosed]: isClosed,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
{type === 'error' && <span className={styles.close} onClick={handleCloseClick} />}
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
let close;
|
interface PanelIconProps {
|
||||||
|
icon: string;
|
||||||
if (type === 'error') {
|
|
||||||
close = <span className={styles.close} onClick={this.onClose} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
const className = clsx(styles[`${type}BodyHeader`], {
|
|
||||||
[styles.isClosed]: this.state.isClosed,
|
|
||||||
});
|
|
||||||
|
|
||||||
const extraProps = omit(this.props, ['type', 'onClose']);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className={className} {...extraProps}>
|
|
||||||
{close}
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
onClose = (event: React.MouseEvent<HTMLElement>) => {
|
|
||||||
event.preventDefault();
|
|
||||||
|
|
||||||
const { onClose } = this.props;
|
|
||||||
|
|
||||||
this.setState({ isClosed: true });
|
|
||||||
|
|
||||||
if (onClose) {
|
|
||||||
onClose();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function PanelIcon({ icon }: { icon: string }) {
|
export const PanelIcon: FC<PanelIconProps> = ({ icon }) => {
|
||||||
return (
|
return (
|
||||||
<div className={styles.panelIcon}>
|
<div className={styles.panelIcon}>
|
||||||
<span className={icons[icon]} />
|
<span className={icons[icon]} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { ReactNode } from 'react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
|
||||||
import logger from 'app/services/logger';
|
import logger from 'app/services/logger';
|
||||||
@@ -10,7 +10,8 @@ interface BaseProps {
|
|||||||
id: string;
|
id: string;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
onInvalid: (errors: Record<string, string>) => void;
|
onInvalid: (errors: Record<string, string>) => void;
|
||||||
children: React.ReactNode;
|
className?: string;
|
||||||
|
children?: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PropsWithoutForm extends BaseProps {
|
interface PropsWithoutForm extends BaseProps {
|
||||||
@@ -24,10 +25,6 @@ interface PropsWithForm extends BaseProps {
|
|||||||
|
|
||||||
type Props = PropsWithoutForm | PropsWithForm;
|
type Props = PropsWithoutForm | PropsWithForm;
|
||||||
|
|
||||||
function hasForm(props: Props): props is PropsWithForm {
|
|
||||||
return 'form' in props;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
id: string; // just to track value for derived updates
|
id: string; // just to track value for derived updates
|
||||||
isTouched: boolean;
|
isTouched: boolean;
|
||||||
@@ -54,10 +51,7 @@ export default class Form extends React.Component<Props, State> {
|
|||||||
mounted = false;
|
mounted = false;
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
if (hasForm(this.props)) {
|
(this.props as PropsWithForm).form?.addLoadingListener(this.onLoading);
|
||||||
this.props.form.addLoadingListener(this.onLoading);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.mounted = true;
|
this.mounted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,8 +71,8 @@ export default class Form extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps: Props) {
|
componentDidUpdate(prevProps: Props) {
|
||||||
const nextForm = hasForm(this.props) ? this.props.form : undefined;
|
const nextForm = (this.props as PropsWithForm).form;
|
||||||
const prevForm = hasForm(prevProps) ? prevProps.form : undefined;
|
const prevForm = (prevProps as PropsWithForm).form;
|
||||||
|
|
||||||
if (nextForm !== prevForm) {
|
if (nextForm !== prevForm) {
|
||||||
if (prevForm) {
|
if (prevForm) {
|
||||||
@@ -92,10 +86,7 @@ export default class Form extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
if (hasForm(this.props)) {
|
(this.props as PropsWithForm).form?.removeLoadingListener(this.onLoading);
|
||||||
this.props.form.removeLoadingListener(this.onLoading);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.mounted = false;
|
this.mounted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +95,7 @@ export default class Form extends React.Component<Props, State> {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
className={clsx(styles.form, {
|
className={clsx(styles.form, this.props.className, {
|
||||||
[styles.isFormLoading]: isLoading,
|
[styles.isFormLoading]: isLoading,
|
||||||
[styles.formTouched]: this.state.isTouched,
|
[styles.formTouched]: this.state.isTouched,
|
||||||
})}
|
})}
|
||||||
@@ -134,12 +125,10 @@ export default class Form extends React.Component<Props, State> {
|
|||||||
this.clearErrors();
|
this.clearErrors();
|
||||||
let result: Promise<void> | void;
|
let result: Promise<void> | void;
|
||||||
|
|
||||||
if (hasForm(this.props)) {
|
if ((this.props as PropsWithForm).form) {
|
||||||
// @ts-ignore this prop has default value
|
result = (this.props as PropsWithForm).onSubmit!((this.props as PropsWithForm).form);
|
||||||
result = this.props.onSubmit(this.props.form);
|
|
||||||
} else {
|
} else {
|
||||||
// @ts-ignore this prop has default value
|
result = (this.props as PropsWithoutForm).onSubmit!(new FormData(form));
|
||||||
result = this.props.onSubmit(new FormData(form));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result && result.then) {
|
if (result && result.then) {
|
||||||
@@ -181,14 +170,11 @@ export default class Form extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setErrors(errors: { [key: string]: string }) {
|
setErrors(errors: { [key: string]: string }) {
|
||||||
if (hasForm(this.props)) {
|
(this.props as PropsWithForm).form?.setErrors(errors);
|
||||||
this.props.form.setErrors(errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.props.onInvalid(errors);
|
this.props.onInvalid(errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
clearErrors = () => hasForm(this.props) && this.props.form.clearErrors();
|
clearErrors = () => (this.props as PropsWithForm).form?.clearErrors();
|
||||||
|
|
||||||
onFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
onFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ $bodyTopBottomPadding: 15px;
|
|||||||
padding: 10px 20px;
|
padding: 10px 20px;
|
||||||
margin: (-$bodyTopBottomPadding) (-$bodyLeftRightPadding) 15px;
|
margin: (-$bodyTopBottomPadding) (-$bodyLeftRightPadding) 15px;
|
||||||
max-height: 200px;
|
max-height: 200px;
|
||||||
|
text-align: center;
|
||||||
|
|
||||||
transition: 0.4s ease;
|
transition: 0.4s ease;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
import React, { FC } from 'react';
|
import React, { FC, ComponentType } from 'react';
|
||||||
import { Route, RouteProps } from 'react-router-dom';
|
import { Route, RouteProps, RouteComponentProps } from 'react-router-dom';
|
||||||
|
|
||||||
import AuthFlowRouteContents from './AuthFlowRouteContents';
|
import AuthFlowRouteContents from './AuthFlowRouteContents';
|
||||||
|
|
||||||
// Make "component" prop required
|
// Make "component" prop required
|
||||||
type Props = Omit<RouteProps, 'component'> & Required<Pick<RouteProps, 'component'>>;
|
type Props = Omit<RouteProps, 'component'> & {
|
||||||
|
component: ComponentType<RouteComponentProps>;
|
||||||
|
};
|
||||||
|
|
||||||
const AuthFlowRoute: FC<Props> = ({ component: Component, ...props }) => {
|
const AuthFlowRoute: FC<Props> = ({ component: Component, ...props }) => {
|
||||||
return (
|
return (
|
||||||
<Route
|
<Route {...props} render={(routerProps) => <AuthFlowRouteContents component={Component} {...routerProps} />} />
|
||||||
{...props}
|
|
||||||
render={(routerProps) => <AuthFlowRouteContents routerProps={routerProps} component={Component} />}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ describe('AuthFlowRouteContents', () => {
|
|||||||
|
|
||||||
(authFlow.handleRequest as any).callsArg(2);
|
(authFlow.handleRequest as any).callsArg(2);
|
||||||
|
|
||||||
render(<AuthFlowRouteContents routerProps={routerProps} component={Component} />);
|
render(<AuthFlowRouteContents component={Component} {...routerProps} />);
|
||||||
|
|
||||||
const component = screen.getByTestId('test-component');
|
const component = screen.getByTestId('test-component');
|
||||||
|
|
||||||
|
|||||||
@@ -1,89 +1,34 @@
|
|||||||
import React from 'react';
|
import React, { FC, ReactElement, ComponentType, useEffect, useState } from 'react';
|
||||||
import { Redirect, RouteComponentProps } from 'react-router-dom';
|
import { RouteComponentProps } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { useIsMounted } from 'app/hooks';
|
||||||
import authFlow from 'app/services/authFlow';
|
import authFlow from 'app/services/authFlow';
|
||||||
|
|
||||||
interface Props {
|
interface Props extends RouteComponentProps {
|
||||||
component: React.ComponentType<RouteComponentProps> | React.ComponentType<any>;
|
component: ComponentType<RouteComponentProps>;
|
||||||
routerProps: RouteComponentProps;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
const AuthFlowRouteContents: FC<Props> = ({ component: WantedComponent, location, match, history }) => {
|
||||||
access: null | 'rejected' | 'allowed';
|
const isMounted = useIsMounted();
|
||||||
component: React.ReactElement | null;
|
const [component, setComponent] = useState<ReactElement | null>(null);
|
||||||
}
|
|
||||||
|
|
||||||
export default class AuthFlowRouteContents extends React.Component<Props, State> {
|
|
||||||
state: State = {
|
|
||||||
access: null,
|
|
||||||
component: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
mounted = false;
|
|
||||||
|
|
||||||
shouldComponentUpdate({ routerProps: nextRoute, component: nextComponent }: Props, state: State) {
|
|
||||||
const { component: prevComponent, routerProps: prevRoute } = this.props;
|
|
||||||
|
|
||||||
return (
|
|
||||||
prevRoute.location.pathname !== nextRoute.location.pathname ||
|
|
||||||
prevRoute.location.search !== nextRoute.location.search ||
|
|
||||||
prevComponent !== nextComponent ||
|
|
||||||
this.state.access !== state.access
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
this.mounted = true;
|
|
||||||
this.handleProps(this.props);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate() {
|
|
||||||
this.handleProps(this.props);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmount() {
|
|
||||||
this.mounted = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return this.state.component;
|
|
||||||
}
|
|
||||||
|
|
||||||
handleProps(props: Props) {
|
|
||||||
const { routerProps } = props;
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
authFlow.handleRequest(
|
authFlow.handleRequest(
|
||||||
{
|
{
|
||||||
path: routerProps.location.pathname,
|
path: location.pathname,
|
||||||
params: routerProps.match.params,
|
params: match.params,
|
||||||
query: new URLSearchParams(routerProps.location.search),
|
query: new URLSearchParams(location.search),
|
||||||
|
},
|
||||||
|
history.push,
|
||||||
|
() => {
|
||||||
|
if (isMounted()) {
|
||||||
|
setComponent(<WantedComponent history={history} location={location} match={match} />);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
this.onRedirect.bind(this),
|
|
||||||
this.onRouteAllowed.bind(this, props),
|
|
||||||
);
|
);
|
||||||
}
|
}, [location.pathname, location.search]);
|
||||||
|
|
||||||
onRedirect(path: string) {
|
return component;
|
||||||
if (!this.mounted) {
|
};
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
export default AuthFlowRouteContents;
|
||||||
access: 'rejected',
|
|
||||||
component: <Redirect to={path} />,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
onRouteAllowed(props: Props): void {
|
|
||||||
if (!this.mounted) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { component: Component } = props;
|
|
||||||
|
|
||||||
this.setState({
|
|
||||||
access: 'allowed',
|
|
||||||
component: <Component {...props.routerProps} />,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
1
packages/app/hooks/index.ts
Normal file
1
packages/app/hooks/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { default as useIsMounted } from './useIsMounted';
|
||||||
17
packages/app/hooks/useIsMounted.ts
Normal file
17
packages/app/hooks/useIsMounted.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { useCallback, useEffect, useRef } from 'react';
|
||||||
|
|
||||||
|
// This code is copied from the usehooks-ts: https://usehooks-ts.com/react-hook/use-is-mounted
|
||||||
|
// Replace it with the library when the Node.js version of the project will be updated at least to 16.
|
||||||
|
export default function useIsMounted(): () => boolean {
|
||||||
|
const isMounted = useRef(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
isMounted.current = true;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
isMounted.current = false;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return useCallback(() => isMounted.current, []);
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import React, { FC, useCallback, useState } from 'react';
|
import React, { FC, useCallback, useState } from 'react';
|
||||||
import { Route, Switch, Redirect, RouteComponentProps } from 'react-router-dom';
|
import { Route, Switch, Redirect, RouteComponentProps } from 'react-router-dom';
|
||||||
|
import clsx from 'clsx';
|
||||||
|
|
||||||
import AppInfo from 'app/components/auth/appInfo/AppInfo';
|
import AppInfo from 'app/components/auth/appInfo/AppInfo';
|
||||||
import PanelTransition from 'app/components/auth/PanelTransition';
|
import PanelTransition from 'app/components/auth/PanelTransition';
|
||||||
@@ -7,7 +8,6 @@ import Register from 'app/components/auth/register/Register';
|
|||||||
import Login from 'app/components/auth/login/Login';
|
import Login from 'app/components/auth/login/Login';
|
||||||
import Permissions from 'app/components/auth/permissions/Permissions';
|
import Permissions from 'app/components/auth/permissions/Permissions';
|
||||||
import ChooseAccount from 'app/components/auth/chooseAccount/ChooseAccount';
|
import ChooseAccount from 'app/components/auth/chooseAccount/ChooseAccount';
|
||||||
import DeviceCode from 'app/components/auth/deviceCode';
|
|
||||||
import Activation from 'app/components/auth/activation/Activation';
|
import Activation from 'app/components/auth/activation/Activation';
|
||||||
import ResendActivation from 'app/components/auth/resendActivation/ResendActivation';
|
import ResendActivation from 'app/components/auth/resendActivation/ResendActivation';
|
||||||
import Password from 'app/components/auth/password/Password';
|
import Password from 'app/components/auth/password/Password';
|
||||||
@@ -38,8 +38,8 @@ const AuthPage: FC = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<>
|
||||||
<div className={isSidebarHidden ? styles.hiddenSidebar : styles.sidebar}>
|
<div className={clsx(styles.sidebar, { [styles.hiddenSidebar]: isSidebarHidden })}>
|
||||||
<AppInfo {...client} onGoToAuth={goToAuth} />
|
<AppInfo {...client} onGoToAuth={goToAuth} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -55,14 +55,13 @@ const AuthPage: FC = () => {
|
|||||||
<Route path="/choose-account" render={renderPanelTransition(ChooseAccount)} />
|
<Route path="/choose-account" render={renderPanelTransition(ChooseAccount)} />
|
||||||
<Route path="/oauth/choose-account" render={renderPanelTransition(ChooseAccount)} />
|
<Route path="/oauth/choose-account" render={renderPanelTransition(ChooseAccount)} />
|
||||||
<Route path="/oauth/finish" component={Finish} />
|
<Route path="/oauth/finish" component={Finish} />
|
||||||
<Route path="/code" component={renderPanelTransition(DeviceCode)} />
|
|
||||||
<Route path="/accept-rules" render={renderPanelTransition(AcceptRules)} />
|
<Route path="/accept-rules" render={renderPanelTransition(AcceptRules)} />
|
||||||
<Route path="/forgot-password" render={renderPanelTransition(ForgotPassword)} />
|
<Route path="/forgot-password" render={renderPanelTransition(ForgotPassword)} />
|
||||||
<Route path="/recover-password/:key?" render={renderPanelTransition(RecoverPassword)} />
|
<Route path="/recover-password/:key?" render={renderPanelTransition(RecoverPassword)} />
|
||||||
<Redirect to="/404" />
|
<Redirect to="/404" />
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ $sidebar-width: 320px;
|
|||||||
}
|
}
|
||||||
|
|
||||||
.hiddenSidebar {
|
.hiddenSidebar {
|
||||||
composes: sidebar;
|
|
||||||
|
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import React from 'react';
|
import React, { FC, useCallback, useEffect } from 'react';
|
||||||
import { Route, Switch } from 'react-router-dom';
|
import { Route, Switch } from 'react-router-dom';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
|
||||||
import { connect } from 'app/functions';
|
import { useReduxSelector, useReduxDispatch } from 'app/functions';
|
||||||
import { resetAuth } from 'app/components/auth/actions';
|
import { resetAuth } from 'app/components/auth/actions';
|
||||||
import { ScrollIntoView } from 'app/components/ui/scroll';
|
import { ScrollIntoView } from 'app/components/ui/scroll';
|
||||||
import PrivateRoute from 'app/containers/PrivateRoute';
|
import PrivateRoute from 'app/containers/PrivateRoute';
|
||||||
@@ -10,14 +10,12 @@ import AuthFlowRoute from 'app/containers/AuthFlowRoute';
|
|||||||
import { PopupStack } from 'app/components/ui/popup';
|
import { PopupStack } from 'app/components/ui/popup';
|
||||||
import * as loader from 'app/services/loader';
|
import * as loader from 'app/services/loader';
|
||||||
import { getActiveAccount } from 'app/components/accounts/reducer';
|
import { getActiveAccount } from 'app/components/accounts/reducer';
|
||||||
import { User } from 'app/components/user';
|
|
||||||
import { Account } from 'app/components/accounts/reducer';
|
|
||||||
import { ComponentLoader } from 'app/components/ui/loader';
|
import { ComponentLoader } from 'app/components/ui/loader';
|
||||||
import Toolbar from './Toolbar';
|
import PageNotFound from 'app/pages/404/PageNotFound';
|
||||||
|
import DeviceCode from 'app/components/auth/deviceCode';
|
||||||
|
|
||||||
import styles from './root.scss';
|
import styles from './root.scss';
|
||||||
|
import Toolbar from './Toolbar';
|
||||||
import PageNotFound from 'app/pages/404/PageNotFound';
|
|
||||||
|
|
||||||
const ProfileController = React.lazy(
|
const ProfileController = React.lazy(
|
||||||
() => import(/* webpackChunkName: "page-profile-all" */ 'app/pages/profile/ProfileController'),
|
() => import(/* webpackChunkName: "page-profile-all" */ 'app/pages/profile/ProfileController'),
|
||||||
@@ -26,76 +24,56 @@ const RulesPage = React.lazy(() => import(/* webpackChunkName: "page-rules" */ '
|
|||||||
const DevPage = React.lazy(() => import(/* webpackChunkName: "page-dev-applications" */ 'app/pages/dev/DevPage'));
|
const DevPage = React.lazy(() => import(/* webpackChunkName: "page-dev-applications" */ 'app/pages/dev/DevPage'));
|
||||||
const AuthPage = React.lazy(() => import(/* webpackChunkName: "page-auth" */ 'app/pages/auth/AuthPage'));
|
const AuthPage = React.lazy(() => import(/* webpackChunkName: "page-auth" */ 'app/pages/auth/AuthPage'));
|
||||||
|
|
||||||
class RootPage extends React.PureComponent<{
|
const RootPage: FC = () => {
|
||||||
account: Account | null;
|
const dispatch = useReduxDispatch();
|
||||||
user: User;
|
const user = useReduxSelector((state) => state.user);
|
||||||
isPopupActive: boolean;
|
const account = useReduxSelector(getActiveAccount);
|
||||||
onLogoClick: (event: React.MouseEvent<HTMLAnchorElement>) => void;
|
const isPopupActive = useReduxSelector((state) => state.popup.popups.length > 0);
|
||||||
}> {
|
|
||||||
componentDidMount() {
|
|
||||||
this.onPageUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate() {
|
const onLogoClick = useCallback(() => {
|
||||||
this.onPageUpdate();
|
dispatch(resetAuth());
|
||||||
}
|
}, []);
|
||||||
|
|
||||||
onPageUpdate() {
|
useEffect(() => {
|
||||||
loader.hide();
|
loader.hide();
|
||||||
}
|
}); // No deps, effect must be called on every update
|
||||||
|
|
||||||
render() {
|
return (
|
||||||
const { user, account, isPopupActive, onLogoClick } = this.props;
|
<div className={styles.root}>
|
||||||
|
<ScrollIntoView top />
|
||||||
|
|
||||||
if (document && document.body) {
|
<div
|
||||||
document.body.style.overflow = isPopupActive ? 'hidden' : '';
|
id="view-port"
|
||||||
}
|
className={clsx(styles.viewPort, {
|
||||||
|
[styles.isPopupActive]: isPopupActive,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Toolbar account={account} onLogoClick={onLogoClick} />
|
||||||
|
<div className={styles.body}>
|
||||||
|
<React.Suspense fallback={<ComponentLoader />}>
|
||||||
|
<Switch>
|
||||||
|
<PrivateRoute path="/profile" component={ProfileController} />
|
||||||
|
<Route path="/404" component={PageNotFound} />
|
||||||
|
<Route path="/rules" component={RulesPage} />
|
||||||
|
<Route path="/dev" component={DevPage} />
|
||||||
|
|
||||||
return (
|
<AuthFlowRoute
|
||||||
<div className={styles.root}>
|
exact
|
||||||
<ScrollIntoView top />
|
path="/"
|
||||||
|
key="indexPage"
|
||||||
|
component={user.isGuest ? AuthPage : ProfileController}
|
||||||
|
/>
|
||||||
|
<AuthFlowRoute exact path="/code" component={DeviceCode} />
|
||||||
|
<AuthFlowRoute path="/" component={AuthPage} />
|
||||||
|
|
||||||
<div
|
<Route component={PageNotFound} />
|
||||||
id="view-port"
|
</Switch>
|
||||||
className={clsx(styles.viewPort, {
|
</React.Suspense>
|
||||||
[styles.isPopupActive]: isPopupActive,
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<Toolbar account={account} onLogoClick={onLogoClick} />
|
|
||||||
<div className={styles.body}>
|
|
||||||
<React.Suspense fallback={<ComponentLoader />}>
|
|
||||||
<Switch>
|
|
||||||
<PrivateRoute path="/profile" component={ProfileController} />
|
|
||||||
<Route path="/404" component={PageNotFound} />
|
|
||||||
<Route path="/rules" component={RulesPage} />
|
|
||||||
<Route path="/dev" component={DevPage} />
|
|
||||||
|
|
||||||
<AuthFlowRoute
|
|
||||||
exact
|
|
||||||
path="/"
|
|
||||||
key="indexPage"
|
|
||||||
component={user.isGuest ? AuthPage : ProfileController}
|
|
||||||
/>
|
|
||||||
<AuthFlowRoute path="/" component={AuthPage} />
|
|
||||||
|
|
||||||
<Route component={PageNotFound} />
|
|
||||||
</Switch>
|
|
||||||
</React.Suspense>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<PopupStack />
|
|
||||||
</div>
|
</div>
|
||||||
);
|
<PopupStack />
|
||||||
}
|
</div>
|
||||||
}
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default connect(
|
export default RootPage;
|
||||||
(state) => ({
|
|
||||||
user: state.user,
|
|
||||||
account: getActiveAccount(state),
|
|
||||||
isPopupActive: state.popup.popups.length > 0,
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
onLogoClick: resetAuth,
|
|
||||||
},
|
|
||||||
)(RootPage);
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ function hasPrompt(prompt: OAuthState['prompt'], needle: string): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default class CompleteState extends AbstractState {
|
export default class CompleteState extends AbstractState {
|
||||||
private readonly isPermissionsAccepted?: boolean;
|
isPermissionsAccepted?: boolean;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
options: {
|
options: {
|
||||||
@@ -54,8 +54,6 @@ export default class CompleteState extends AbstractState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
processOAuth(context: AuthContext): Promise<void> | void {
|
processOAuth(context: AuthContext): Promise<void> | void {
|
||||||
console.log('process oauth', this.isPermissionsAccepted);
|
|
||||||
|
|
||||||
const { auth, accounts, user } = context.getState();
|
const { auth, accounts, user } = context.getState();
|
||||||
|
|
||||||
let { isSwitcherEnabled } = auth;
|
let { isSwitcherEnabled } = auth;
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ const errorsMap: Record<string, (props?: Record<string, any>) => ReactElement> =
|
|||||||
'error.redirectUri_required': () => <Message key="redirectUriRequired" defaultMessage="Redirect URI is required" />,
|
'error.redirectUri_required': () => <Message key="redirectUriRequired" defaultMessage="Redirect URI is required" />,
|
||||||
'error.redirectUri_invalid': () => <Message key="redirectUriInvalid" defaultMessage="Redirect URI is invalid" />,
|
'error.redirectUri_invalid': () => <Message key="redirectUriInvalid" defaultMessage="Redirect URI is invalid" />,
|
||||||
|
|
||||||
'invalid_user_code': () => <Message key="invalidUserCode" defaultMessage="Invalid Device Code" />,
|
invalid_user_code: () => <Message key="invalidUserCode" defaultMessage="Invalid Device Code" />,
|
||||||
};
|
};
|
||||||
|
|
||||||
interface ErrorLiteral {
|
interface ErrorLiteral {
|
||||||
|
|||||||
Reference in New Issue
Block a user