Search code examples
reactjsadmin-on-rest

Cannot create a custom login page with Admin-On-Rest


Been trying for a while to figure out how to set a custom login page in admin-on-rest v-1.0.0

Need to enable email based login. For this I changed the default admin-on-rest login page code slightly (below)

Replaced the UserName with email and changed the authClient to loopbackRestClient

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { propTypes, reduxForm, Field } from 'redux-form';
import { connect } from 'react-redux';
import compose from 'recompose/compose';

import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import { Card, CardActions } from 'material-ui/Card';
import Avatar from 'material-ui/Avatar';
import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
import CircularProgress from 'material-ui/CircularProgress';
import LockIcon from 'material-ui/svg-icons/action/lock-outline';
import { cyan500, pinkA200 } from 'material-ui/styles/colors';

import defaultTheme from 'admin-on-rest';
import { userLogin as userLoginAction } from 'admin-on-rest';

import Notification from 'admin-on-rest';

const styles = {
    main: {
        display: 'flex',
        flexDirection: 'column',
        minHeight: '100vh',
        alignItems: 'center',
        justifyContent: 'center',
    },
    card: {
        minWidth: 300,
    },
    avatar: {
        margin: '1em',
        textAlign: 'center ',
    },
    form: {
        padding: '0 1em 1em 1em',
    },
    input: {
        display: 'flex',
    },
};

function getColorsFromTheme(theme) {
    if (!theme) return { primary1Color: cyan500, accent1Color: pinkA200 };
    const {
        palette: {
            primary1Color,
            accent1Color,
        },
      } = theme;
    return { primary1Color, accent1Color };
}

// see http://redux-form.com/6.4.3/examples/material-ui/
const renderInput = ({ meta: { touched, error } = {}, input: { ...inputProps }, ...props }) =>
    <TextField
        errorText={touched && error}
        {...inputProps}
        {...props}
        fullWidth
    />;


class EmailLogin extends Component {

    login = (auth) => this.props.userLogin(auth, this.props.location.state ? this.props.location.state.nextPathname : '/');

    render() {
        const { handleSubmit, submitting, theme } = this.props;
        const muiTheme = getMuiTheme(theme);
        const { primary1Color, accent1Color } = getColorsFromTheme(muiTheme);
        console.log(styles.form)
        return (
            <MuiThemeProvider muiTheme={muiTheme}>
                <div style={{ ...styles.main, backgroundColor: primary1Color }}>
                    <Card style={styles.card}>
                        <div style={styles.avatar}>
                            <Avatar backgroundColor={accent1Color} icon={<LockIcon />} size={60} />
                        </div>
                        <form onSubmit={handleSubmit(this.login)}>
                            <div style={styles.form}>
                                <div style={styles.input} >
                                    <Field
                                        name="email"
                                        floatingLabelText={"email"}
                                        component={renderInput}
                                        disabled={submitting}
                                        placeholder={"email"}
                                    />
                                </div>
                                <div style={styles.input}>
                                    <Field
                                        name="password"
                                        component={renderInput}
                                        type="password"
                                        disabled={submitting}
                                    />
                                </div>
                            </div>
                            <CardActions>
                                <RaisedButton
                                    type="submit"
                                    primary
                                    disabled={submitting}
                                    icon={submitting && <CircularProgress size={25} thickness={2} />}
                                    fullWidth
                                />
                            </CardActions>
                        </form>
                    </Card>
                    <Notification />
                </div>
            </MuiThemeProvider>
        );
    }
}


EmailLogin.propTypes = {
    ...propTypes,
    authClient: PropTypes.func,
    previousRoute: PropTypes.string,
    theme: PropTypes.object,
    userLogin: PropTypes.func.isRequired,
};

EmailLogin.defaultProps = {
    theme: defaultTheme,
};

const enhance = compose(
    reduxForm({
        form: 'signIn',
        validate: (values, props) => {
            const errors = {};
            return errors;
        },
    }),
    connect(null, { userLogin: userLoginAction }),
);

export default enhance( EmailLogin );

I have added the above to the loginPage prop on Admin in my app.js

However admin-on-rest seems to be showing the default page.

I copied the BtcLoginPage from this question

Is there any examples of how to create a custom login page?

But the admin is still showing the default page (the one with UserName) only.

Please do advise. How can I create a custom page using Admin-On-Rest.

Thanks


Solution

  • There is a working example in the admin-on-rest-demo repository:

    The custom Login component:

    https://github.com/marmelab/admin-on-rest-demo/blob/master/src/Login.js

    It's integration into the Admin:

    https://github.com/marmelab/admin-on-rest-demo/blob/master/src/App.js#L45

    Which version of aor are you using ?