-
Notifications
You must be signed in to change notification settings - Fork 13
/
withAuth.js
74 lines (67 loc) · 2.08 KB
/
withAuth.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, {Component} from 'react'
import Router from 'next/router'
import AuthService from './AuthService'
import { getCookie, setCookie } from '../utils/Cookies'
import cookie from "js-cookie";
export default function withAuth(AuthComponent) {
const Auth = new AuthService(process.env.API_DOMAIN_URL)
return class Authenticated extends Component {
static async getInitialProps(ctx) {
const isServer = !!ctx.req
// Ensures material-ui renders the correct css prefixes server-side
let userAgent
let seshToken
if (!isServer) {
userAgent = navigator.userAgent
seshToken = cookie.get('seshToken');
} else {
userAgent = ctx.req.headers['user-agent']
seshToken = getCookie('seshToken', ctx.req);
}
let isLoading = true
console.log(seshToken);
if (!seshToken) {
// ctx.res.writeHead(301, {
// Location: `http://localhost/oauth/authorize/?client_id=4&redirect_uri=http://localhost:3000/token&response_type=code`
// })
// ctx.res.end()
} else {
setCookie('seshToken', seshToken)
isLoading = false
}
// Check if Page has a `getInitialProps`; if so, call it.
const pageProps = AuthComponent.getInitialProps && await AuthComponent.getInitialProps(ctx);
// Return props.
return {
...pageProps,
userAgent,
isLoading,
seshToken
}
}
constructor(props) {
super(props)
this.state = {
isLoading: props.isLoading,
token: props.seshToken
};
}
componentDidMount () {
if (!this.state.token) {
Router.push('/')
}
this.setState({ isLoading: false })
}
render() {
return (
<div>
{this.state.isLoading ? (
<div>LOADING....</div>
) : (
<AuthComponent {...this.props} auth={Auth} token={this.state.token} />
)}
</div>
)
}
}
}