Skip to content

fix: when the system disables registration, users can still register and log in through SSO #4722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 65 additions & 7 deletions web/src/pages/AuthCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { LoaderIcon } from "lucide-react";
import { ClientError } from "nice-grpc-web";
import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { Button } from "@usememos/mui";
import { authServiceClient } from "@/grpcweb";
import { absolutifyLink } from "@/helpers/utils";
import useNavigateTo from "@/hooks/useNavigateTo";
import { workspaceStore } from "@/store/v2";
import { initialUserStore } from "@/store/v2/user";

interface State {
loading: boolean;
errorMessage: string;
redirectCountdown?: number;
}

const AuthCallback = () => {
Expand All @@ -20,6 +23,7 @@ const AuthCallback = () => {
loading: true,
errorMessage: "",
});
const workspaceGeneralSetting = workspaceStore.state.generalSetting;

useEffect(() => {
const code = searchParams.get("code");
Expand Down Expand Up @@ -60,23 +64,77 @@ const AuthCallback = () => {
navigateTo("/");
} catch (error: any) {
console.error(error);
setState({
loading: false,
errorMessage: (error as ClientError).details,
});
const errorDetails = (error as ClientError).details;

// If user registration is disallowed and the error indicates that sign up is not allowed
if (workspaceGeneralSetting.disallowUserRegistration &&
(errorDetails.includes("registration is not allowed") ||
errorDetails.includes("sign up is not allowed"))) {
const redirectSeconds = 2;
setState({
loading: false,
errorMessage: errorDetails || "Sign up is not allowed.",
redirectCountdown: redirectSeconds,
});

// Set up countdown timer
const countdownInterval = setInterval(() => {
setState((prevState) => ({
...prevState,
redirectCountdown: prevState.redirectCountdown ? prevState.redirectCountdown - 1 : 0,
}));
}, 1000);

// Add a timer to redirect to the login page after the countdown
const redirectTimer = setTimeout(() => {
navigateTo("/auth");
}, redirectSeconds * 1000);

return () => {
clearTimeout(redirectTimer);
clearInterval(countdownInterval);
};
} else {
// Other errors
setState({
loading: false,
errorMessage: errorDetails || "Authentication failed",
});
}
}
})();
}, [searchParams]);
}, [searchParams, workspaceGeneralSetting.disallowUserRegistration]);

const handleReturnToLogin = () => {
navigateTo("/auth");
};

return (
<div className="p-4 py-24 w-full h-full flex justify-center items-center">
{state.loading ? (
<LoaderIcon className="animate-spin dark:text-gray-200" />
) : (
<div className="max-w-lg font-mono whitespace-pre-wrap opacity-80">{state.errorMessage}</div>
<div className="flex flex-col items-center">
<div className="max-w-lg font-mono whitespace-pre-wrap opacity-80 mb-4">{state.errorMessage}</div>
{state.redirectCountdown !== undefined && (
<div className="flex flex-col items-center gap-3">
<div className="flex items-center gap-2 text-sm text-gray-500">
<LoaderIcon className="w-4 h-4 animate-spin" />
<span>Redirecting to login page in {state.redirectCountdown} {state.redirectCountdown === 1 ? 'second' : 'seconds'}...</span>
</div>
<Button
color="primary"
size="sm"
onClick={handleReturnToLogin}
>
Return to Login Now
</Button>
</div>
)}
</div>
)}
</div>
);
};

export default AuthCallback;
export default AuthCallback;