forked from coderplex-org/coderplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic-page.js
51 lines (48 loc) · 1.29 KB
/
public-page.js
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
import React from 'react';
import redirect from '../utils/redirect';
import { authenticate } from '../utils/authenticate';
import Header from '../components/header';
import Footer from '../components/footer';
export default Page => {
return class PublicPage extends React.Component {
static async getInitialProps(ctx) {
let authData;
let initialProps = {};
const pathName = ctx.req ? ctx.req.url : ctx.pathname;
if (ctx.req && ctx.req.user) {
authData = ctx.req.user;
} else {
try {
authData = await authenticate(ctx);
} catch (err) {
console.error(err);
if (Page.getInitialProps) {
initialProps = await Page.getInitialProps(ctx);
}
return { ...initialProps };
}
}
if (authData) {
if (pathName === '/login') {
return redirect(ctx, '/profile');
}
}
if (Page.getInitialProps) {
initialProps = await Page.getInitialProps(ctx);
}
if (!authData) {
return { ...initialProps };
}
return { ...authData, ...initialProps };
}
render() {
return (
<div>
<Header {...this.props} />
<Page {...this.props} />
<Footer />
</div>
);
}
};
};