-
Notifications
You must be signed in to change notification settings - Fork 355
/
AuthCheckRoute.tsx
172 lines (151 loc) · 4.63 KB
/
AuthCheckRoute.tsx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Match, Router, withRouter } from "found";
import React from "react";
import { graphql } from "react-relay";
import { SetRedirectPathMutation } from "coral-admin/mutations";
import { AbilityType, can } from "coral-admin/permissions";
import { roleIsAtLeast } from "coral-framework/helpers";
import { CoralContext, withContext } from "coral-framework/lib/bootstrap";
import { MutationProp, withMutation } from "coral-framework/lib/relay";
import { withRouteConfig } from "coral-framework/lib/router";
import { GQLUSER_ROLE } from "coral-framework/schema";
import { AuthCheckRouteQueryResponse } from "coral-admin/__generated__/AuthCheckRouteQuery.graphql";
import NetworkError from "./NetworkError";
import RestrictedContainer from "./RestrictedContainer";
interface Props {
reporter: CoralContext["reporter"];
match: Match;
router: Router;
setRedirectPath: MutationProp<typeof SetRedirectPathMutation>;
data: AuthCheckRouteQueryResponse;
error: Error | null;
}
type CheckParams =
| {
role: GQLUSER_ROLE;
ability?: AbilityType;
}
| {
role?: GQLUSER_ROLE;
ability: AbilityType;
};
function createAuthCheckRoute(check: CheckParams) {
class AuthCheckRoute extends React.Component<Props> {
private wasLoggedIn = false;
constructor(props: Props) {
super(props);
void this.redirectIfNotLoggedIn();
}
public UNSAFE_componentWillReceiveProps(nextProps: Props) {
if (nextProps.data && nextProps.data.viewer) {
this.wasLoggedIn = true;
}
void this.redirectIfNotLoggedIn(nextProps, this.props);
if (nextProps.data && !nextProps.data.viewer) {
this.wasLoggedIn = false;
}
}
private shouldRedirectTo(props: Props = this.props) {
if (!props.data || (props.data.viewer && props.data.viewer.email)) {
return false;
}
return true;
}
private hasAccess(props: Props = this.props) {
const { viewer } = props.data;
if (viewer) {
if (
(check.role && !roleIsAtLeast(viewer.role, check.role)) ||
(check.ability && !can(viewer, check.ability))
) {
return false;
}
return true;
}
return false;
}
private async redirectIfNotLoggedIn(
props: Props = this.props,
prevProps: Props | null = null
) {
if (!this.shouldRedirectTo(props)) {
return;
}
// If I was previously logged in then logged out, we don't need to set the redirect path.
if (!this.wasLoggedIn) {
const location = props.match.location;
await props.setRedirectPath({
path: location.pathname + location.search + location.hash,
});
}
props.router.replace("/admin/login");
}
public componentDidUpdate(prevProps: Props) {
// Whenever the viewer changes on this component, update the user on the
// reporter.
if (!this.props.reporter) {
return;
}
// Pull the viewer out of the next props (`this.props`) and the current
// props (`prevProps`) to compare them.
const next = this.props.data?.viewer || null;
const curr = prevProps.data?.viewer || null;
// If the next is different than current, then...
if (next !== curr) {
// If they are both provided then if the id of the viewer didn't change,
// then the user hasn't changed (at least, it hasn't changed enough to
// require another update to the reporter).
if (next && curr && next.id === curr.id) {
return;
}
this.props.reporter.setUser(next);
}
}
public render() {
if (this.props.error) {
return <NetworkError />;
}
if (!this.props.data || this.shouldRedirectTo()) {
return null;
}
if (this.hasAccess()) {
return this.props.children;
}
return <RestrictedContainer viewer={this.props.data.viewer} />;
}
}
const enhanced = withRouteConfig<Props>({
query: graphql`
query AuthCheckRouteQuery {
viewer {
...RestrictedContainer_viewer
id
username
email
profiles {
__typename
}
role
}
settings {
auth {
integrations {
local {
enabled
targetFilter {
admin
stream
}
}
}
}
}
}
`,
})(
withContext(({ reporter }) => ({ reporter }))(
withRouter(withMutation(SetRedirectPathMutation)(AuthCheckRoute))
)
);
return enhanced;
}
export default createAuthCheckRoute;