Skip to content

Instantly share code, notes, and snippets.

@superjojo140
Last active June 14, 2020 14:59
Show Gist options
  • Save superjojo140/d490726d4356d5dc445c647cd94e531d to your computer and use it in GitHub Desktop.
Save superjojo140/d490726d4356d5dc445c647cd94e531d to your computer and use it in GitHub Desktop.
CORS fetch-request with credentials

This sounds easy but... it isn't!

Client side

Use this fetch options:

fetch('superUnsecureCorsUrl',{
   credentials: 'include'
})

Server side (express backend)

const cors = require('cors');
const app = express();

//Allow CORS requests
var corsOptions = {
    origin: 'trustedClientUrl',
    credentials: true 
    };
app.use(cors(corsOptions))

Note that origin property can not be '*'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment