mirror of
https://github.com/wassname/talk.git
synced 2026-08-09 12:30:42 +08:00
[next] Auth (#2257)
* feat: improved auth features + performance * fix: auth check logic * fix: tests
This commit is contained in:
@@ -52,10 +52,7 @@ class AuthCheckContainer extends React.Component<Props> {
|
||||
}
|
||||
|
||||
private hasAccess(props: Props = this.props) {
|
||||
const {
|
||||
viewer,
|
||||
settings: { auth },
|
||||
} = props.data!;
|
||||
const { viewer } = props.data!;
|
||||
if (viewer) {
|
||||
if (
|
||||
viewer.role === GQLUSER_ROLE.COMMENTER ||
|
||||
@@ -66,15 +63,6 @@ class AuthCheckContainer extends React.Component<Props> {
|
||||
!can(viewer, props.data.route.data))
|
||||
) {
|
||||
return false;
|
||||
} else if (
|
||||
!viewer.email ||
|
||||
!viewer.username ||
|
||||
(!viewer.profiles.some(p => p.__typename === "LocalProfile") &&
|
||||
auth.integrations.local.enabled &&
|
||||
(auth.integrations.local.targetFilter.admin ||
|
||||
auth.integrations.local.targetFilter.stream))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
import { Match, Router, withRouter } from "found";
|
||||
import React from "react";
|
||||
|
||||
import { RedirectLoginContainerQueryResponse } from "talk-admin/__generated__/RedirectLoginContainerQuery.graphql";
|
||||
import {
|
||||
SetRedirectPathMutation,
|
||||
withSetRedirectPathMutation,
|
||||
} from "talk-admin/mutations";
|
||||
import { graphql } from "talk-framework/lib/relay";
|
||||
import { withRouteConfig } from "talk-framework/lib/router";
|
||||
import { GQLUSER_ROLE } from "talk-framework/schema";
|
||||
|
||||
interface Props {
|
||||
match: Match;
|
||||
router: Router;
|
||||
setRedirectPath: SetRedirectPathMutation;
|
||||
data: RedirectLoginContainerQueryResponse | null;
|
||||
}
|
||||
|
||||
class RedirectLoginContainer extends React.Component<Props> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.redirectIfNotLoggedIn();
|
||||
}
|
||||
|
||||
public componentWillReceiveProps(nextProps: Props) {
|
||||
this.redirectIfNotLoggedIn(nextProps);
|
||||
}
|
||||
|
||||
private shouldRedirectTo(props: Props = this.props): string | null {
|
||||
if (!props.data) {
|
||||
return null;
|
||||
}
|
||||
const {
|
||||
viewer,
|
||||
settings: { auth },
|
||||
} = props.data!;
|
||||
if (viewer) {
|
||||
if (viewer.role === GQLUSER_ROLE.COMMENTER) {
|
||||
return "/admin/login";
|
||||
} else if (
|
||||
!viewer.email ||
|
||||
!viewer.username ||
|
||||
(!viewer.profiles.some(p => p.__typename === "LocalProfile") &&
|
||||
auth.integrations.local.enabled &&
|
||||
(auth.integrations.local.targetFilter.admin ||
|
||||
auth.integrations.local.targetFilter.stream))
|
||||
) {
|
||||
return "/admin/login";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
return "/admin/login";
|
||||
}
|
||||
|
||||
private redirectIfNotLoggedIn(props: Props = this.props) {
|
||||
const redirect = this.shouldRedirectTo(props);
|
||||
if (redirect) {
|
||||
const location = props.match.location;
|
||||
props.setRedirectPath({
|
||||
path: location.pathname + location.search + location.hash,
|
||||
});
|
||||
props.router.replace(redirect);
|
||||
}
|
||||
}
|
||||
|
||||
public render() {
|
||||
if (this.shouldRedirectTo()) {
|
||||
return null;
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
const enhanced = withRouteConfig({
|
||||
query: graphql`
|
||||
query RedirectLoginContainerQuery {
|
||||
viewer {
|
||||
username
|
||||
email
|
||||
profiles {
|
||||
__typename
|
||||
}
|
||||
role
|
||||
}
|
||||
settings {
|
||||
auth {
|
||||
integrations {
|
||||
local {
|
||||
enabled
|
||||
targetFilter {
|
||||
admin
|
||||
stream
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
})(withRouter(withSetRedirectPathMutation(RedirectLoginContainer)));
|
||||
|
||||
export default enhanced;
|
||||
@@ -59,21 +59,6 @@ it("show restricted screen for commenters and staff", async () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("show restricted screen when email is not set", async () => {
|
||||
const { testRenderer } = createTestRenderer({ email: "" });
|
||||
await waitForElement(() => within(testRenderer.root).getByTestID("authBox"));
|
||||
});
|
||||
|
||||
it("show restricted screen when username is not set", async () => {
|
||||
const { testRenderer } = createTestRenderer({ username: "" });
|
||||
await waitForElement(() => within(testRenderer.root).getByTestID("authBox"));
|
||||
});
|
||||
|
||||
it("show restricted screen local was not set (password)", async () => {
|
||||
const { testRenderer } = createTestRenderer({ profiles: [] });
|
||||
await waitForElement(() => within(testRenderer.root).getByTestID("authBox"));
|
||||
});
|
||||
|
||||
it("sign out when clicking on sign in as", async () => {
|
||||
const { context, testRenderer } = createTestRenderer({
|
||||
role: GQLUSER_ROLE.COMMENTER,
|
||||
|
||||
Reference in New Issue
Block a user