Last active
March 20, 2023 23:15
-
-
Save k-funk/0898be6c5ced8a0ebf830ad88bba0911 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
import fetch from 'node-fetch' | |
import express, { Request, Response } from 'express' | |
import * as dotenv from 'dotenv' | |
import bodyParser from 'body-parser' | |
dotenv.config() | |
const redirectUri = 'http://localhost:5010/oauth/linearCallback' | |
const AuthorizationHost = 'https://linear.app' | |
const TokenHost = 'https://api.linear.app' | |
const AuthorizePath = '/oauth/authorize' | |
const TokenPath = '/oauth/token' | |
const params = { | |
response_type: 'code', | |
client_id: process.env.LINEAR_CLIENT_API_KEY!, | |
actor: 'application', // https://developers.linear.app/docs/oauth/oauth-actor-authorization | |
redirect_uri: redirectUri, | |
scope: 'read,write,issues:create,comments:create', | |
} | |
const url = new URL(AuthorizationHost) | |
url.pathname = AuthorizePath | |
url.search = new URLSearchParams(params).toString() | |
const authorizationUri = url.href | |
const app = express() | |
app.set('port', (process.env.PORT || 5010)) | |
app.listen(app.get('port'), () => { | |
console.info('Node app is running on port', app.get('port')) | |
}) | |
app.use(bodyParser.json()) | |
app.get('/oauth/linear', (req, res) => { | |
res.redirect(authorizationUri) | |
}) | |
app.get('/oauth/linearCallback', async (req: Request, res: Response) => { | |
const { code } = req.query | |
const params = { | |
code: typeof code === 'string' ? code : '', | |
redirect_uri: redirectUri, | |
client_id: process.env.LINEAR_CLIENT_API_KEY!, | |
client_secret: process.env.LINEAR_CLIENT_SECRET!, | |
grant_type: 'authorization_code', | |
} | |
try { | |
const url = new URL(TokenHost) | |
url.pathname = TokenPath | |
const response = await fetch(url.href, { | |
method: 'POST', | |
body: new URLSearchParams(params).toString(), | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
}) | |
if (!response.ok) throw new Error(response.status.toString()) | |
const payload = await response.json() | |
res.json(payload) | |
} catch (error) { | |
console.error('Access Token Error', error) | |
res.status(500).json('Authentication failed') | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment