-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitter.ts
80 lines (69 loc) · 2.23 KB
/
twitter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* @poppinss/oauth-client
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Request, Response } from 'express'
import { twitterConfig } from './config.js'
import { Oauth1Client } from '../src/clients/oauth1/main.js'
export async function renderRedirect(_: Request, res: Response) {
try {
/**
* Instantiate the driver
*/
const driver = new Oauth1Client(twitterConfig)
const { token, secret } = await driver.getRequestToken()
/**
* Make the redirect URL. We also send the state in the URL query string,
* along with a github specific "allow_signup" option.
*/
const redirectUrl = driver.getRedirectUrl((request) => {
request.param('oauth_token', token)
})
/**
* Store state inside cookie for later verification
*/
res.cookie('twitter_oauth_token', token, { sameSite: false })
res.cookie('twitter_oauth_token_secret', secret, { sameSite: false })
res.type('html').send(`<a href="${redirectUrl}">Login with Twitter</a>`)
} catch (error) {
console.log(error)
res.send(error.response && error.response.body ? error.response.body : error.response || error)
}
}
export async function handleCallback(req: Request, res: Response) {
const oauthToken = req.query.oauth_token as string
const oldOauthToken = req.cookies.twitter_oauth_token
const oauthVerifier = req.query.oauth_verifier as string
const oauthSecret = req.cookies.twitter_oauth_token_secret
if (!oauthToken) {
res.status(400).send('Missing "oauth_token"')
return
}
if (!oauthVerifier) {
res.status(400).send('Missing "oauth_verifier"')
return
}
/**
* Instantiate the driver
*/
const driver = new Oauth1Client(twitterConfig)
try {
driver.verifyState(oldOauthToken, oauthToken)
const accessToken = await driver.getAccessToken(
{
token: oauthToken,
secret: oauthSecret,
},
(request) => {
request.oauth1Param('oauth_verifier', oauthVerifier)
}
)
res.type('json').send(accessToken)
} catch (error) {
res.send(error.response && error.response.body ? error.response.body : error.response || error)
}
}