forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login.tsx
172 lines (161 loc) · 4.98 KB
/
Login.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, {
HtmlHTMLAttributes,
ComponentType,
createElement,
ReactNode,
useRef,
useEffect,
useMemo,
} from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Card, Avatar, Theme } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';
import LockIcon from '@material-ui/icons/Lock';
import { StaticContext } from 'react-router';
import { useHistory } from 'react-router-dom';
import { useCheckAuth, TitleComponent } from 'ra-core';
import defaultTheme from '../defaultTheme';
import { createMuiTheme } from '../layout';
import DefaultNotification from '../layout/Notification';
import DefaultLoginForm from './LoginForm';
export interface LoginProps
extends Omit<HtmlHTMLAttributes<HTMLDivElement>, 'title'> {
backgroundImage?: string;
children?: ReactNode;
classes?: object;
className?: string;
notification?: ComponentType;
staticContext?: StaticContext;
theme?: object;
title?: TitleComponent;
}
const useStyles = makeStyles(
(theme: Theme) => ({
main: {
display: 'flex',
flexDirection: 'column',
minHeight: '100vh',
height: '1px',
alignItems: 'center',
justifyContent: 'flex-start',
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
backgroundImage:
'radial-gradient(circle at 50% 14em, #313264 0%, #00023b 60%, #00023b 100%)',
},
card: {
minWidth: 300,
marginTop: '6em',
},
avatar: {
margin: '1em',
display: 'flex',
justifyContent: 'center',
},
icon: {
backgroundColor: theme.palette.secondary[500],
},
}),
{ name: 'RaLogin' }
);
/**
* A standalone login page, to serve as authentication gate to the admin
*
* Expects the user to enter a login and a password, which will be checked
* by the `authProvider.login()` method. Redirects to the root page (/)
* upon success, otherwise displays an authentication error message.
*
* Copy and adapt this component to implement your own login logic
* (e.g. to authenticate via email or facebook or anything else).
*
* @example
* import MyLoginPage from './MyLoginPage';
* const App = () => (
* <Admin loginPage={MyLoginPage} authProvider={authProvider}>
* ...
* </Admin>
* );
*/
const Login: React.FunctionComponent<LoginProps> = props => {
const {
theme,
title,
classes: classesOverride,
className,
children,
notification,
staticContext,
backgroundImage,
...rest
} = props;
const containerRef = useRef<HTMLDivElement>();
const classes = useStyles(props);
const muiTheme = useMemo(() => createMuiTheme(theme), [theme]);
let backgroundImageLoaded = false;
const checkAuth = useCheckAuth();
const history = useHistory();
useEffect(() => {
checkAuth({}, false)
.then(() => {
// already authenticated, redirect to the home page
history.push('/');
})
.catch(() => {
// not authenticated, stay on the login page
});
}, [checkAuth, history]);
const updateBackgroundImage = () => {
if (!backgroundImageLoaded && containerRef.current) {
containerRef.current.style.backgroundImage = `url(${backgroundImage})`;
backgroundImageLoaded = true;
}
};
// Load background image asynchronously to speed up time to interactive
const lazyLoadBackgroundImage = () => {
if (backgroundImage) {
const img = new Image();
img.onload = updateBackgroundImage;
img.src = backgroundImage;
}
};
useEffect(() => {
if (!backgroundImageLoaded) {
lazyLoadBackgroundImage();
}
});
return (
<ThemeProvider theme={muiTheme}>
<div
className={classnames(classes.main, className)}
{...rest}
ref={containerRef}
>
<Card className={classes.card}>
<div className={classes.avatar}>
<Avatar className={classes.icon}>
<LockIcon />
</Avatar>
</div>
{children}
</Card>
{notification ? createElement(notification) : null}
</div>
</ThemeProvider>
);
};
Login.propTypes = {
backgroundImage: PropTypes.string,
children: PropTypes.node,
classes: PropTypes.object,
className: PropTypes.string,
theme: PropTypes.object,
staticContext: PropTypes.object,
};
Login.defaultProps = {
theme: defaultTheme,
children: <DefaultLoginForm />,
notification: DefaultNotification,
};
export default Login;