Skip to content
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
6 changes: 5 additions & 1 deletion apps/dashboard/app/auth/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ export async function completeOrgSelection(
): Promise<NavigationResponse | AuthErrorResponse> {
const tempSession = cookies().get(PENDING_SESSION_COOKIE);
if (!tempSession) {
throw new Error("No pending session");
return {
success: false,
code: AuthErrorCode.PENDING_SESSION_EXPIRED,
message: errorMessages[AuthErrorCode.PENDING_SESSION_EXPIRED]
};
}

// Call auth provider with token and orgId
Expand Down
14 changes: 11 additions & 3 deletions apps/dashboard/app/auth/sign-in/[[...sign-in]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ import { OAuthSignIn } from "../oauth-signin";
import { OrgSelector } from "../org-selector";

function SignInContent() {
const { isVerifying, accountNotFound, error, email, hasPendingAuth, orgs, handleSignInViaEmail } =
useSignIn();
const {
isVerifying,
accountNotFound,
error,
email,
hasPendingAuth,
orgs,
handleSignInViaEmail,
setError,
} = useSignIn();
const searchParams = useSearchParams();
const verifyParam = searchParams?.get("verify");
const invitationToken = searchParams?.get("invitation_token");
Expand Down Expand Up @@ -86,7 +94,7 @@ function SignInContent() {

return (
<div className="flex flex-col gap-10">
{hasPendingAuth && <OrgSelector organizations={orgs} />}
{hasPendingAuth && <OrgSelector organizations={orgs} onError={setError} />}

{accountNotFound && (
<WarnBanner>
Expand Down
23 changes: 20 additions & 3 deletions apps/dashboard/app/auth/sign-in/org-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import { completeOrgSelection } from "../actions";

interface OrgSelectorProps {
organizations: Organization[];
onError: (errorMessage: string) => void;
}

export const OrgSelector: React.FC<OrgSelectorProps> = ({ organizations }) => {
export const OrgSelector: React.FC<OrgSelectorProps> = ({ organizations, onError }) => {
const [selected, setSelected] = useState<string>();
const [isOpen, setIsOpen] = useState(false);
const [clientReady, setClientReady] = useState(false);
Expand All @@ -40,8 +41,24 @@ export const OrgSelector: React.FC<OrgSelectorProps> = ({ organizations }) => {
if (!selected) {
return;
}
await completeOrgSelection(selected);
setIsOpen(false);
try {
const result = await completeOrgSelection(selected);

if (!result.success) {
onError(result.message);
}

return;
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: "Failed to complete organization selection. Please re-authenticate or contact support@unkey.dev";

onError(errorMessage);
} finally {
setIsOpen(false);
}
};

return (
Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/lib/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export enum AuthErrorCode {
ACCOUNT_NOT_FOUND = "ACCOUNT_NOT_FOUND",
ORGANIZATION_SELECTION_REQUIRED = "ORGANIZATION_SELECTION_REQUIRED",
EMAIL_VERIFICATION_REQUIRED = "EMAIL_VERIFICATION_REQUIRED",
PENDING_SESSION_EXPIRED = "PENDING_SESSION_EXPIRED",
}

export const errorMessages: Record<AuthErrorCode, string> = {
Expand All @@ -188,6 +189,8 @@ export const errorMessages: Record<AuthErrorCode, string> = {
"Please choose a workspace to continue authentication.",
[AuthErrorCode.EMAIL_VERIFICATION_REQUIRED]:
"Email address not verified. Please check your email for a verification code.",
[AuthErrorCode.PENDING_SESSION_EXPIRED]:
"Pending Authentication has expired. Please sign-in again.",
};

export interface MiddlewareConfig {
Expand Down
Loading