Repository navigation
Expand file tree
/
Copy pathwithAuth.tsx
More file actions
66 lines (52 loc) · 1.5 KB
/
Copy pathwithAuth.tsx
File metadata and controls
66 lines (52 loc) · 1.5 KB
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
import Router from 'next/router';
import React from 'react';
import { Store } from './store';
import withStore from './withStore';
export default function withAuth(
BaseComponent,
{ loginRequired = true, logoutRequired = false } = {},
) {
class App extends React.Component<{ store: Store }> {
public static async getInitialProps(ctx) {
const props: any = {};
if (BaseComponent.getInitialProps) {
Object.assign(props, (await BaseComponent.getInitialProps(ctx)) || {});
}
return props;
}
public componentDidMount() {
const { store } = this.props;
const user = store.currentUser;
if (loginRequired && !logoutRequired && !user) {
Router.push('/login');
return;
}
let redirectUrl = '/login';
let asUrl = '/login';
if (user) {
if (!user.defaultTeamSlug) {
redirectUrl = '/create-team';
asUrl = '/create-team';
} else {
redirectUrl = `/discussion?teamSlug=${user.defaultTeamSlug}`;
asUrl = `/team/${user.defaultTeamSlug}/d`;
}
}
if (logoutRequired && user) {
Router.push(redirectUrl, asUrl);
}
}
public render() {
const { store } = this.props;
const user = store.currentUser;
if (loginRequired && !logoutRequired && !user) {
return null;
}
if (logoutRequired && user) {
return null;
}
return <BaseComponent {...this.props} />;
}
}
return withStore(App);
}