Skip to content
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

fix: onboarding redirection loop and bug fixes #3250

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions web/components/auth-screens/not-authorized-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type Props = {

export const NotAuthorizedView: React.FC<Props> = ({ actionButton, type }) => {
const { user } = useUser();
const { asPath: currentPath } = useRouter();
const { query } = useRouter();
const { next_path } = query;

return (
<DefaultLayout>
Expand All @@ -37,15 +38,15 @@ export const NotAuthorizedView: React.FC<Props> = ({ actionButton, type }) => {
{user ? (
<p>
You have signed in as {user.email}. <br />
<Link href={`/?next=${currentPath}`}>
<Link href={`/?next_path=${next_path}`}>
<span className="font-medium text-custom-text-100">Sign in</span>
</Link>{" "}
with different account that has access to this page.
</p>
) : (
<p>
You need to{" "}
<Link href={`/?next=${currentPath}`}>
<Link href={`/?next_path=${next_path}`}>
<span className="font-medium text-custom-text-100">Sign in</span>
</Link>{" "}
with an account that has access to this page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ArchivedIssueQuickActions: React.FC<IQuickActionProps> = (props) =>
const { setToastAlert } = useToast();

const handleCopyIssueLink = () => {
copyUrlToClipboard(`/${workspaceSlug}/projects/${issue.project}/archived-issues/${issue.id}`).then(() =>
copyUrlToClipboard(`${workspaceSlug}/projects/${issue.project}/archived-issues/${issue.id}`).then(() =>
setToastAlert({
type: "success",
title: "Link copied",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const CycleIssueQuickActions: React.FC<IQuickActionProps> = (props) => {
const { setToastAlert } = useToast();

const handleCopyIssueLink = () => {
copyUrlToClipboard(`/${workspaceSlug}/projects/${issue.project}/issues/${issue.id}`).then(() =>
copyUrlToClipboard(`${workspaceSlug}/projects/${issue.project}/issues/${issue.id}`).then(() =>
setToastAlert({
type: "success",
title: "Link copied",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const ModuleIssueQuickActions: React.FC<IQuickActionProps> = (props) => {
const { setToastAlert } = useToast();

const handleCopyIssueLink = () => {
copyUrlToClipboard(`/${workspaceSlug}/projects/${issue.project}/issues/${issue.id}`).then(() =>
copyUrlToClipboard(`${workspaceSlug}/projects/${issue.project}/issues/${issue.id}`).then(() =>
setToastAlert({
type: "success",
title: "Link copied",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const ProjectIssueQuickActions: React.FC<IQuickActionProps> = (props) =>
const { setToastAlert } = useToast();

const handleCopyIssueLink = () => {
copyUrlToClipboard(`/${workspaceSlug}/projects/${issue.project}/issues/${issue.id}`).then(() =>
copyUrlToClipboard(`${workspaceSlug}/projects/${issue.project}/issues/${issue.id}`).then(() =>
setToastAlert({
type: "success",
title: "Link copied",
Expand Down
60 changes: 39 additions & 21 deletions web/hooks/use-sign-in-redirection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,54 @@
const [error, setError] = useState<any | null>(null);
// router
const router = useRouter();
const { next_url } = router.query;
const { next_path } = router.query;
// mobx store
const {
user: { fetchCurrentUser, fetchCurrentUserSettings },
} = useMobxStore();

const isValidURL = (url: string): boolean => {
const disallowedSchemes = /^(https?|ftp):\/\//i;
return !disallowedSchemes.test(url);
};

console.log("next_path", next_path);

const handleSignInRedirection = useCallback(
async (user: IUser) => {
// if the user is not onboarded, redirect them to the onboarding page
if (!user.is_onboarded) {
router.push("/onboarding");
return;
}
// if next_url is provided, redirect the user to that url
if (next_url) {
router.push(next_url.toString());
return;
}
try {
// if the user is not onboarded, redirect them to the onboarding page
if (!user.is_onboarded) {
router.push("/onboarding");
return;
}
// if next_path is provided, redirect the user to that url
if (next_path) {
if (isValidURL(next_path.toString())) {
router.push(next_path.toString());

Check warning

Code scanning / CodeQL

Client-side URL redirect Medium

Untrusted URL redirection depends on a
user-provided value
.

Check failure

Code scanning / CodeQL

Client-side cross-site scripting High

Cross-site scripting vulnerability due to
user-provided value
.
return;
} else {
router.push("/");
return;
}
}

// Fetch the current user settings
const userSettings: IUserSettings = await fetchCurrentUserSettings();

// if the user is onboarded, fetch their last workspace details
await fetchCurrentUserSettings()
.then((userSettings: IUserSettings) => {
const workspaceSlug =
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
if (workspaceSlug) router.push(`/${workspaceSlug}`);
else router.push("/profile");
})
.catch((err) => setError(err));
// Extract workspace details
const workspaceSlug =
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;

// Redirect based on workspace details or to profile if not available
if (workspaceSlug) router.push(`/${workspaceSlug}`);
else router.push("/profile");
} catch (error) {
console.error("Error in handleSignInRedirection:", error);
setError(error);
}
},
[fetchCurrentUserSettings, router, next_url]
[fetchCurrentUserSettings, router, next_path]
);

const updateUserInfo = useCallback(async () => {
Expand Down
20 changes: 16 additions & 4 deletions web/hooks/use-user-auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "admin") => {
const router = useRouter();
const { next_url } = router.query;
const { next_path } = router.query;

const [isRouteAccess, setIsRouteAccess] = useState(true);
const {
Expand All @@ -29,6 +29,11 @@
shouldRetryOnError: false,
});

const isValidURL = (url: string): boolean => {
const disallowedSchemes = /^(https?|ftp):\/\//i;
return !disallowedSchemes.test(url);
};

useEffect(() => {
const handleWorkSpaceRedirection = async () => {
workspaceService.userWorkspaces().then(async (userWorkspaces) => {
Expand Down Expand Up @@ -84,8 +89,15 @@
if (!isLoading) {
setIsRouteAccess(() => true);
if (user) {
if (next_url) router.push(next_url.toString());
else handleUserRouteAuthentication();
if (next_path) {
if (isValidURL(next_path.toString())) {
router.push(next_path.toString());

Check warning

Code scanning / CodeQL

Client-side URL redirect Medium

Untrusted URL redirection depends on a
user-provided value
.

Check failure

Code scanning / CodeQL

Client-side cross-site scripting High

Cross-site scripting vulnerability due to
user-provided value
.
return;
} else {
router.push("/");
return;
}
} else handleUserRouteAuthentication();
} else {
if (routeAuth === "sign-in") {
setIsRouteAccess(() => false);
Expand All @@ -97,7 +109,7 @@
}
}
}
}, [user, isLoading, routeAuth, router, next_url]);
}, [user, isLoading, routeAuth, router, next_path]);

return {
isLoading: isRouteAccess,
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/use-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function useUser({ redirectTo = "", redirectIfFound = false, opti
) {
router.push(redirectTo);
return;
// const nextLocation = router.asPath.split("?next=")[1];
// const nextLocation = router.asPath.split("?next_path=")[1];
// if (nextLocation) {
// router.push(nextLocation as string);
// return;
Expand Down
2 changes: 1 addition & 1 deletion web/layouts/auth-layout/user-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const UserAuthWrapper: FC<IUserAuthWrapper> = observer((props) => {

if (currentUserError) {
const redirectTo = router.asPath;
router.push(`/?next=${redirectTo}`);
router.push(`/?next_path=${redirectTo}`);
return null;
}

Expand Down
12 changes: 4 additions & 8 deletions web/pages/onboarding/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { Controller, useForm } from "react-hook-form";
import { useMobxStore } from "lib/mobx/store-provider";
// services
import { WorkspaceService } from "services/workspace.service";
// hooks
import useUserAuth from "hooks/use-user-auth";
// layouts
import DefaultLayout from "layouts/default-layout";
import { UserAuthWrapper } from "layouts/auth-layout";
Expand Down Expand Up @@ -45,8 +43,6 @@ const OnboardingPage: NextPageWithLayout = observer(() => {

const { setTheme } = useTheme();

const {} = useUserAuth("onboarding");

const { control, setValue } = useForm<{ full_name: string }>({
defaultValues: {
full_name: "",
Expand Down Expand Up @@ -158,8 +154,8 @@ const OnboardingPage: NextPageWithLayout = observer(() => {
currentUser?.first_name
? `${currentUser?.first_name} ${currentUser?.last_name ?? ""}`
: value.length > 0
? value
: currentUser?.email
? value
: currentUser?.email
}
src={currentUser?.avatar}
size={35}
Expand All @@ -174,8 +170,8 @@ const OnboardingPage: NextPageWithLayout = observer(() => {
{currentUser?.first_name
? `${currentUser?.first_name} ${currentUser?.last_name ?? ""}`
: value.length > 0
? value
: null}
? value
: null}
</p>
)}

Expand Down
Loading