无法使用Admin-On-Rest创建自定义登录页面

时间:2017-04-28 12:08:05

标签: reactjs admin-on-rest

尝试了一段时间以了解如何在admin-on-rest v-1.0.0中设置自定义登录页面

需要启用基于电子邮件的登录。为此,我稍微更改了默认的admin-on-rest登录页面代码(下面)

用电子邮件替换UserName并将authClient更改为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 );

我已将上述内容添加到app.js

中Admin上的loginPage prop

但是,休息时管理似乎显示默认页面。

我从这个问题中复制了BtcLoginPage

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

但是管理员仍然只显示默认页面(具有UserName的页面)。

请提出建议。如何使用Admin-On-Rest创建自定义页面。

由于

2 个答案:

答案 0 :(得分:3)

admin-on-rest-demo存储库中有一个工作示例:

自定义登录组件:

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

它集成到Admin:

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

您使用的是哪个版本的aor?

答案 1 :(得分:0)

似乎我一直在做错事。

我的自定义loginPage(上面)有一个无提示错误。这条线

import Notification from 'admin-on-rest';

失败需要

import { Notification } from 'admin-on-rest';

然而。

Admin-on-rest隐含地将默认管理页面替换为我的自定义页面。我将错误归咎于我对Redux和React-Redux的有限知识。但解决方案更简单。