mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-07 07:55:16 +02:00
Merge pull request #9 from dograh-hq/fix/redirect-to-workflow
fix: redirect user to /workflow page if they have workflow
This commit is contained in:
commit
9cb75829cb
3 changed files with 110 additions and 9 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { redirect } from "next/navigation";
|
||||
|
||||
import { getServerAuthProvider, getServerUser } from "@/lib/auth/server";
|
||||
import { getWorkflowsApiV1WorkflowFetchGet } from "@/client/sdk.gen";
|
||||
import { getServerAccessToken,getServerAuthProvider, getServerUser } from "@/lib/auth/server";
|
||||
import logger from '@/lib/logger';
|
||||
import { getRedirectUrl } from "@/lib/utils";
|
||||
|
||||
|
|
@ -26,7 +27,40 @@ export default async function AfterSignInPage() {
|
|||
logger.debug('[AfterSignInPage] Redirecting to:', redirectUrl);
|
||||
redirect(redirectUrl);
|
||||
}
|
||||
// For local provider or if user is not available, redirect to create-workflow
|
||||
logger.debug('[AfterSignInPage] Fallback redirect to /create-workflow');
|
||||
|
||||
// For local provider or if user is not available, check for existing workflows
|
||||
logger.debug('[AfterSignInPage] Checking for existing workflows before fallback');
|
||||
|
||||
try {
|
||||
const accessToken = await getServerAccessToken();
|
||||
if (accessToken) {
|
||||
const workflowsResponse = await getWorkflowsApiV1WorkflowFetchGet({
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
const workflows = workflowsResponse.data ? (Array.isArray(workflowsResponse.data) ? workflowsResponse.data : [workflowsResponse.data]) : [];
|
||||
const activeWorkflows = workflows.filter(w => w.status === 'active');
|
||||
|
||||
logger.debug('[AfterSignInPage] Found workflows:', {
|
||||
total: workflows.length,
|
||||
active: activeWorkflows.length
|
||||
});
|
||||
|
||||
if (activeWorkflows.length > 0) {
|
||||
logger.debug('[AfterSignInPage] Redirecting to /workflow - user has workflows');
|
||||
redirect('/workflow');
|
||||
} else {
|
||||
logger.debug('[AfterSignInPage] Redirecting to /create-workflow - no workflows found');
|
||||
redirect('/create-workflow');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('[AfterSignInPage] Error checking workflows:', error);
|
||||
}
|
||||
|
||||
// Default fallback
|
||||
logger.debug('[AfterSignInPage] Final fallback redirect to /create-workflow');
|
||||
redirect('/create-workflow');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { redirect } from "next/navigation";
|
||||
import { isNextRouterError } from "next/dist/client/components/is-next-router-error";
|
||||
|
||||
import { getWorkflowsApiV1WorkflowFetchGet } from "@/client/sdk.gen";
|
||||
import SignInClient from "@/components/SignInClient";
|
||||
import { getServerAuthProvider,getServerUser } from "@/lib/auth/server";
|
||||
import { getServerAccessToken,getServerAuthProvider,getServerUser } from "@/lib/auth/server";
|
||||
import logger from '@/lib/logger';
|
||||
import { getRedirectUrl } from "@/lib/utils";
|
||||
|
||||
|
|
@ -12,10 +14,46 @@ export default async function Home() {
|
|||
const authProvider = getServerAuthProvider();
|
||||
logger.debug('[HomePage] Auth provider:', authProvider);
|
||||
|
||||
// For local/OSS provider, always redirect to workflow page
|
||||
// For local/OSS provider, check if user has workflows
|
||||
if (authProvider === 'local') {
|
||||
logger.debug('[HomePage] Redirecting to workflow page for local provider');
|
||||
redirect('/create-workflow');
|
||||
logger.debug('[HomePage] Local provider detected, checking for workflows');
|
||||
|
||||
try {
|
||||
const accessToken = await getServerAccessToken();
|
||||
if (accessToken) {
|
||||
const workflowsResponse = await getWorkflowsApiV1WorkflowFetchGet({
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
const workflows = workflowsResponse.data ? (Array.isArray(workflowsResponse.data) ? workflowsResponse.data : [workflowsResponse.data]) : [];
|
||||
const activeWorkflows = workflows.filter(w => w.status === 'active');
|
||||
|
||||
logger.debug('[HomePage] Found workflows for local provider:', {
|
||||
total: workflows.length,
|
||||
active: activeWorkflows.length
|
||||
});
|
||||
|
||||
if (activeWorkflows.length > 0) {
|
||||
logger.debug('[HomePage] Redirecting to /workflow - user has workflows');
|
||||
redirect('/workflow');
|
||||
} else {
|
||||
logger.debug('[HomePage] Redirecting to /create-workflow - no workflows found');
|
||||
redirect('/create-workflow');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Re-throw navigation errors (redirects, not found, etc.) - they're intentional
|
||||
if (isNextRouterError(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
logger.error('[HomePage] Error checking workflows for local provider:', error);
|
||||
// Default to create-workflow on actual errors
|
||||
logger.debug('[HomePage] Defaulting to /create-workflow due to error');
|
||||
redirect('/create-workflow');
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug('[HomePage] Getting server user...');
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { type ClassValue, clsx } from "clsx"
|
|||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
import { getAuthUserApiV1UserAuthUserGet } from "@/client/sdk.gen";
|
||||
import { getWorkflowsApiV1WorkflowFetchGet } from "@/client/sdk.gen";
|
||||
import { impersonateApiV1SuperuserImpersonatePost } from "@/client/sdk.gen";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
|
|
@ -60,8 +61,36 @@ export async function getRedirectUrl(token: string, permissions: { id: string }[
|
|||
return "/usage";
|
||||
}
|
||||
|
||||
console.log('[getRedirectUrl] Has admin permission, redirecting to /create-workflow');
|
||||
return "/create-workflow";
|
||||
// Check if user has any workflows
|
||||
try {
|
||||
console.log('[getRedirectUrl] Checking for existing workflows...');
|
||||
const workflowsResponse = await getWorkflowsApiV1WorkflowFetchGet({
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const workflows = workflowsResponse.data ? (Array.isArray(workflowsResponse.data) ? workflowsResponse.data : [workflowsResponse.data]) : [];
|
||||
const activeWorkflows = workflows.filter(w => w.status === 'active');
|
||||
|
||||
console.log('[getRedirectUrl] Found workflows:', {
|
||||
total: workflows.length,
|
||||
active: activeWorkflows.length
|
||||
});
|
||||
|
||||
if (activeWorkflows.length > 0) {
|
||||
console.log('[getRedirectUrl] User has workflows, redirecting to /workflow');
|
||||
return "/workflow";
|
||||
} else {
|
||||
console.log('[getRedirectUrl] No workflows found, redirecting to /create-workflow');
|
||||
return "/create-workflow";
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[getRedirectUrl] Error checking workflows:', error);
|
||||
// If we can't check workflows, default to create-workflow
|
||||
console.log('[getRedirectUrl] Defaulting to /create-workflow due to error');
|
||||
return "/create-workflow";
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[getRedirectUrl] Failed to fetch auth user:", error);
|
||||
// Re-throw the error so the caller can handle it
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue