Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktaras authored Sep 18, 2023
1 parent 26e9f1f commit b025cfe
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions socios-oauth2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,92 @@

2. Once added, `run npm i` then `run npm start`

## example express application

import path from "path";
import express from "express";
import { Server } from "@tokenscript/token-negotiator-server";
import { fileURLToPath } from "url";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import cors from "cors";
import "dotenv/config";

const app = express();
const port = 5000;
const hostname = process.env.HOST;

// ES6 solution for __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

const tokenNegotiatorServer = new Server({
issuers: {
socios: {
collectionID: process.env.SOCIOS_COLLECTION_ID,
consumerKey: process.env.SOCIOS_CUSTOMER_KEY,
consumerSecret: process.env.SOCIOS_CUSTOMER_SECRET,
partnerTag: process.env.SOCIOS_PARTNER_TAG,
redirectURI: process.env.SOCIOS_REDIRECT_URI,
returnToApplicationURL: process.env.SOCIOS_RETURN_TO_APP_URL,
},
},
});

const corsOptions = { origin: process.env.APPLICATION_URL };

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

app.get("/user-login-callback", cors(), async (request, response) => {
const accessTokenData = await tokenNegotiatorServer.socios.getAccessToken(
request.query.code,
response
);

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}`);
});

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(corsOptions), async (request, response) => {
const output = await tokenNegotiatorServer.socios.getNonFungibleTokens(
request.cookies["tn-oauth2-access-token-socios"]
);
response.json(output);
});

app.post("/user-logout", cors(corsOptions), async (request, response) => {
const output = await tokenNegotiatorServer.socios.userLogout(
process.env.SOCIOS_AUTH_KEY,
request.cookies["tn-oauth2-access-token-socios"]
);
response.json(output);
});

app.listen(port, hostname, () => console.info(`App listening ${hostname ?? ''} on port ${port}`));







Expand Down

0 comments on commit b025cfe

Please sign in to comment.