mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Get request token from pocket
This commit is contained in:
parent
ae2f695df8
commit
59621768c8
2 changed files with 56 additions and 0 deletions
54
packages/api/src/routers/integration_router.ts
Normal file
54
packages/api/src/routers/integration_router.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import cors from 'cors'
|
||||
import express from 'express'
|
||||
import { corsConfig } from '../utils/corsConfig'
|
||||
import { env } from '../env'
|
||||
import axios from 'axios'
|
||||
import { buildLogger } from '../utils/logger'
|
||||
import { getClaimsByToken } from '../utils/auth'
|
||||
|
||||
const logger = buildLogger('app.dispatch')
|
||||
|
||||
export function integrationRouter() {
|
||||
const router = express.Router()
|
||||
// request token from pocket
|
||||
router.post(
|
||||
'/pocket/request-token',
|
||||
cors<express.Request>(corsConfig),
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
const token = (req.cookies.auth as string) || req.headers.authorization
|
||||
const claims = await getClaimsByToken(token)
|
||||
if (!claims) {
|
||||
return res.status(401).send('UNAUTHORIZED')
|
||||
}
|
||||
|
||||
const consumerKey = env.pocket.consumerKey
|
||||
const redirectUri = `${env.client.url}/settings/integrations:pocketAuthorizationFinished`
|
||||
try {
|
||||
// make a POST request to Pocket to get a request token
|
||||
const response = await axios.post<{ code: string }>(
|
||||
'https://getpocket.com/v3/oauth/request',
|
||||
{
|
||||
consumer_key: consumerKey,
|
||||
redirect_uri: redirectUri,
|
||||
}
|
||||
)
|
||||
const { code } = response.data
|
||||
// store the request token in a cookie
|
||||
res.cookie('pocketRequestToken', code, {
|
||||
maxAge: 1000 * 60 * 60,
|
||||
})
|
||||
// redirect the user to Pocket to authorize the request token
|
||||
res.redirect(
|
||||
`https://getpocket.com/auth/authorize?request_token=${code}&redirect_uri=${redirectUri}`
|
||||
)
|
||||
} catch (e) {
|
||||
logger.info('pocket/request-token exception:', e)
|
||||
res.redirect(
|
||||
`${env.client.url}/settings/integrations?errorCodes=UNKNOWN`
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
return router
|
||||
}
|
||||
|
|
@ -49,6 +49,7 @@ import { textToSpeechRouter } from './routers/text_to_speech'
|
|||
import * as httpContext from 'express-http-context'
|
||||
import { notificationRouter } from './routers/notification_router'
|
||||
import { userRouter } from './routers/user_router'
|
||||
import { integrationRouter } from './routers/integration_router'
|
||||
|
||||
const PORT = process.env.PORT || 4000
|
||||
|
||||
|
|
@ -135,6 +136,7 @@ export const createApp = (): {
|
|||
app.use('/api/mobile-auth', mobileAuthRouter())
|
||||
app.use('/api/text-to-speech', textToSpeechRouter())
|
||||
app.use('/api/notification', notificationRouter())
|
||||
app.use('/api/integration', integrationRouter())
|
||||
app.use('/svc/pubsub/content', contentServiceRouter())
|
||||
app.use('/svc/pubsub/links', linkServiceRouter())
|
||||
app.use('/svc/pubsub/newsletters', newsletterServiceRouter())
|
||||
|
|
|
|||
Loading…
Reference in a new issue