Last active
December 12, 2023 08:39
-
-
Save dam1r89/62f530b52bff1e9c3090aa5b6ec9ac4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const querystring = require("querystring"); | |
const session = require("express-session"); | |
const app = express(); | |
const port = 8080; | |
const url = `http://localhost:${port}`; | |
const oauthServer = "https://app.startinfinity.com"; | |
const appId = "<paste-app-id>"; | |
const appSecret = "<paste-app-scret>"; | |
const callbackUrl = `${url}/callback`; | |
app.use(session({ secret: "keyboard cat", cookie: { maxAge: 60000 } })); | |
app.get("/redirect", (req, res) => { | |
const state = "<generate-random-string-token>"; | |
req.session.state = state; | |
const query = querystring.stringify({ | |
client_id: appId, | |
redirect_uri: callbackUrl, | |
response_type: "code", | |
scope: "", | |
state: state, | |
}); | |
res.redirect(`${oauthServer}/oauth/authorize?${query}`); | |
}); | |
app.get("/callback", async (req, res) => { | |
if (req.session.state !== req.query.state) { | |
throw Error("States do not match"); | |
} | |
const data = { | |
grant_type: "authorization_code", | |
client_id: appId, | |
client_secret: appSecret, | |
redirect_uri: callbackUrl, | |
code: req.query.code, | |
}; | |
const response = await fetch(`${oauthServer}/oauth/token`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
redirect: "follow", | |
body: JSON.stringify(data), | |
}); | |
const token = await response.json(); | |
res.json(token); | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening at ${url}`); | |
}); |
raftx24
commented
May 16, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment