Skip to content

Commit

Permalink
Merge pull request #146 from bl4drnnr/web-interface-dev
Browse files Browse the repository at this point in the history
cors callback
  • Loading branch information
bl4drnnr authored Apr 5, 2024
2 parents e6e4f77 + 1f457df commit be11843
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
33 changes: 28 additions & 5 deletions mnemosyne-web-interface/mnemosyne-api-proxy/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,42 @@ import * as fs from 'fs';
import * as yaml from 'yaml';
import * as bodyParser from 'body-parser';
import { urlencoded, json } from 'express';
import { ImATeapotException } from '@nestjs/common';

(async () => {
const app = await NestFactory.create(AppModule);
const whitelist = ['http://localhost:4200', 'https://mnemosyne.io'];

const app = await NestFactory.create(AppModule, {
cors: {
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
credentials: true,
origin: function (origin, callback) {
console.log('origin API Proxy: ', origin);
if (!origin) {
callback(null, true);
return;
}
if (whitelist.includes(origin) || !!origin.match(/mnemosyne\.io$/)) {
console.log('Allowed CORS (API Proxy) for: ', origin);
callback(null, true);
} else {
console.log('Blocked CORS (API Proxy) for: ', origin);
callback(new ImATeapotException('Not allowed by CORS'), false);
}
}
}
});
const port = process.env.PROXY_PORT;

app.setGlobalPrefix('/api');
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ extended: true, limit: '50mb' }));

app.enableCors({
origin: 'https://mnemosyne.io',
credentials: true
});
// app.enableCors({
// allowedHeaders: ['content-type'],
// origin: 'https://mnemosyne.io',
// credentials: true
// });

// Working in the development mode
// app.enableCors({
Expand Down
32 changes: 27 additions & 5 deletions mnemosyne-web-interface/mnemosyne-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,41 @@ import * as bodyParser from 'body-parser';
import * as fs from 'fs';
import * as yaml from 'yaml';
import { urlencoded, json } from 'express';
import { ImATeapotException } from '@nestjs/common';

(async () => {
const app = await NestFactory.create(AppModule);
const whitelist = ['http://localhost:4201', 'https://proxy.mnemosyne.io'];

const app = await NestFactory.create(AppModule, {
cors: {
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
credentials: true,
origin: function (origin, callback) {
console.log('origin API: ', origin);
if (!origin) {
callback(null, true);
return;
}
if (whitelist.includes(origin) || !!origin.match(/mnemosyne\.io$/)) {
console.log('Allowed CORS (API) for: ', origin);
callback(null, true);
} else {
console.log('Blocked CORS (API) for: ', origin);
callback(new ImATeapotException('Not allowed by CORS'), false);
}
}
}
});
const port = process.env.API_PORT || 3000;

app.setGlobalPrefix('/api');
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ extended: true, limit: '50mb' }));

app.enableCors({
origin: 'https://proxy.mnemosyne.io',
credentials: true
});
// app.enableCors({
// origin: 'https://proxy.mnemosyne.io',
// credentials: true
// });

// Working in the development mode
// app.enableCors({
Expand Down

0 comments on commit be11843

Please sign in to comment.