remove redudant code
This commit is contained in:
parent
e2b789cafe
commit
24b8dd1f74
|
@ -1,12 +0,0 @@
|
|||
import React from 'react'
|
||||
import Session from './session'
|
||||
|
||||
export default class extends React.Component {
|
||||
|
||||
// Expose session to all pages
|
||||
static async getInitialProps({req}) {
|
||||
const session = new Session({req})
|
||||
return {session: await session.getSession(true)}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
import React from 'react';
|
||||
import Layout from '../components/layout';
|
||||
|
||||
export default Page => class DefaultPage extends React.Component {
|
||||
static async getInitialProps (ctx) {
|
||||
let username = null;
|
||||
let role = null;
|
||||
if (process.browser) {
|
||||
username = 'admin';
|
||||
role = 'admin';
|
||||
} else {
|
||||
if (ctx.req && ctx.req.user) {
|
||||
username = ctx.req.user.username;
|
||||
if (ctx.req.user.UserRole) {
|
||||
role = ctx.req.user.UserRole.role;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const pageProps = Page.getInitialProps && Page.getInitialProps(ctx);
|
||||
return {
|
||||
...pageProps,
|
||||
Username: username,
|
||||
Role: role,
|
||||
currentUrl: ctx.pathname,
|
||||
isAuthenticated: !!username
|
||||
};
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<Layout { ...this.props }>
|
||||
<Page { ...this.props } />
|
||||
</Layout>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import defaultPage from './defaultPage'
|
||||
|
||||
const securePageHoc = Page => class SecurePage extends React.Component {
|
||||
static getInitialProps (ctx) {
|
||||
return Page.getInitialProps && Page.getInitialProps(ctx)
|
||||
}
|
||||
static propTypes = {
|
||||
isAuthenticated: PropTypes.bool.isRequired
|
||||
}
|
||||
render () {
|
||||
if (!this.props.isAuthenticated) {
|
||||
return (<p>Unauthroized</p>);
|
||||
}
|
||||
return (<Page {...this.props} />);
|
||||
}
|
||||
}
|
||||
|
||||
export default Page => defaultPage(securePageHoc(Page))
|
|
@ -7,7 +7,13 @@ import Start from '../components/start'
|
|||
import Layout from '../components/layout'
|
||||
import Session from '../components/session'
|
||||
|
||||
export default class extends Start {
|
||||
export default class extends React.Component {
|
||||
|
||||
static async getInitialProps({req}) {
|
||||
const session = new Session({req})
|
||||
return {session: await session.getSession(true)}
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Layout session={this.props.session}>
|
||||
|
@ -21,5 +27,4 @@ export default class extends Start {
|
|||
</Layout>
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue