Skip to content

Commit

Permalink
fix: Solve cross-domain issues (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
HashCookie authored Sep 7, 2024
1 parent df445dd commit 44d3948
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
10 changes: 8 additions & 2 deletions app/components/ModelEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { newModel } from 'casbin';
import { setError } from '@/app/utils/errorManager';

export const ModelEditor = ({ initialValue = '' }: { initialValue: string }) => {
const [modelText, setModelText] = useState(initialValue);
const [modelText, setModelText] = useState('');

useEffect(() => {
if (initialValue) {
setModelText(initialValue);
}
}, [initialValue]);

const validateModel = useCallback(async (text: string) => {
try {
Expand Down Expand Up @@ -74,4 +80,4 @@ export const ModelEditor = ({ initialValue = '' }: { initialValue: string }) =>
</div>
</div>
);
};
};
9 changes: 9 additions & 0 deletions app/model-editor/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export default function ModelEditorLayout({
children,
}: {
children: React.ReactNode
}) {
return <>{children}</>;
}
18 changes: 12 additions & 6 deletions app/model-editor/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
'use client';

import React from 'react';
import React, { useEffect, useState } from 'react';
import { ModelEditor } from '../components/ModelEditor';

const ModelEditorPage = ({ searchParams }: { searchParams: { model?: string } }) => {
const initialValue = searchParams.model ? decodeURIComponent(searchParams.model) : '';
export default function ModelEditorPage() {
const [initialValue, setInitialValue] = useState('');

useEffect(() => {
const params = new URLSearchParams(window.location.search);
const modelParam = params.get('model');
if (modelParam) {
setInitialValue(decodeURIComponent(modelParam));
}
}, []);

return (
<div style={{ width: '100%', height: '100vh' }}>
<ModelEditor initialValue={initialValue} />
</div>
);
};

export default ModelEditorPage;
}
18 changes: 18 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export function middleware(request: NextRequest) {
const response = NextResponse.next();
const origin = request.headers.get('origin') || '*';

response.headers.set('Access-Control-Allow-Origin', origin);
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
response.headers.set('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
response.headers.set('Access-Control-Allow-Credentials', 'true');

return response;
}

export const config = {
matcher: '/model-editor/:path*',
};

0 comments on commit 44d3948

Please sign in to comment.