Skip to content

Commit

Permalink
applied cors settings to example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Taras authored and Nick Taras committed Sep 11, 2023
1 parent 3cd7c3c commit 3b4d227
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions socios-oauth2/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import express from "express";
import {Server} from "./../token-negotiator-server/dist/index.js";
import {fileURLToPath} from 'url';
import { Server } from "./../token-negotiator-server/dist/index.js";
import { fileURLToPath } from 'url';
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import cors from 'cors';
Expand All @@ -14,7 +14,7 @@ const port = 5000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static("public"));
Expand All @@ -32,30 +32,33 @@ const tokenNegotiatorServer = new Server({
}
});

app.get("/", function (request, response) {
response.sendFile(path.join(__dirname, "./public/index.html"));
// Same origin Cors
const corsOptions = { origin: 'http://localhost.5000' }

app.get("/", function (request, response) {
response.sendFile(path.join(__dirname, "./public/index.html"));
});

app.get("/user-login-callback", async (request, response) => {
app.get("/user-login-callback", async (request, response) => {

const accessTokenData = await tokenNegotiatorServer.socios.getAccessToken(request.query.code, response);
const accessTokenData = await tokenNegotiatorServer.socios.getAccessToken(request.query.code, response);

tokenNegotiatorServer.utils.setAccessTokenCookie(
response,
'socios',
accessTokenData
);
tokenNegotiatorServer.utils.setAccessTokenCookie(
response,
'socios',
accessTokenData
);

// navigate back to the application page including the wallet provider details.
response.redirect(`${process.env.SOCIOS_RETURN_TO_APP_URL}`);
// navigate back to the application page including the wallet provider details.
response.redirect(`${process.env.SOCIOS_RETURN_TO_APP_URL}`);
});

app.get("/user-balance", cors(), async (request, response) => {
app.get("/user-balance", cors(corsOptions), async (request, response) => {
const output = await tokenNegotiatorServer.socios.getFungibleTokenBalance(request.cookies['tn-oauth2-access-token-socios']);
response.json(output);
});

app.get("/user-nfts", cors(), async (request, response) =>{
app.get("/user-nfts", cors(corsOptions), async (request, response) => {
const output = await tokenNegotiatorServer.socios.getNonFungibleTokens(request.cookies['tn-oauth2-access-token-socios']);
response.json(output);
});
Expand Down

0 comments on commit 3b4d227

Please sign in to comment.