|
| 1 | +import { FullRequest, Ooth, StrategyValues } from 'ooth'; |
| 2 | + |
| 3 | +const request = require('request'); |
| 4 | +const qs = require('qs'); |
| 5 | + |
| 6 | +const { Strategy } = require('passport-custom'); |
| 7 | + |
| 8 | +type Config = { |
| 9 | + name?: string; |
| 10 | + ooth: Ooth; |
| 11 | + clientID: string; |
| 12 | + clientSecret: string; |
| 13 | + callbackUrl: string; |
| 14 | +}; |
| 15 | + |
| 16 | +export default function({ name = 'twitter', ooth, clientID, clientSecret, callbackUrl }: Config): void { |
| 17 | + ooth.registerUniqueField(name, 'email', 'email'); |
| 18 | + ooth.registerProfileFields(name, 'email'); |
| 19 | + ooth.registerStrategyUniqueField(name, 'id'); |
| 20 | + |
| 21 | + ooth.registerMethod( |
| 22 | + name, |
| 23 | + 'reverse', |
| 24 | + [], |
| 25 | + async () => |
| 26 | + new Promise((res, rej) => |
| 27 | + request.post( |
| 28 | + { |
| 29 | + url: 'https://api.twitter.com/oauth/request_token', |
| 30 | + oauth: { |
| 31 | + callback: callbackUrl, |
| 32 | + consumer_key: clientID, |
| 33 | + consumer_secret: clientSecret, |
| 34 | + }, |
| 35 | + headers: { |
| 36 | + Accept: 'application/json', |
| 37 | + }, |
| 38 | + }, |
| 39 | + (err: Error, _: any, body: string) => { |
| 40 | + if (err) { |
| 41 | + return rej(err); |
| 42 | + } |
| 43 | + |
| 44 | + if (body.indexOf('errors') > -1) { |
| 45 | + try { |
| 46 | + return rej(new Error(JSON.parse(body).errors[0].message)); |
| 47 | + } catch (e) { |
| 48 | + return rej(new Error(`Unexpected error: ${e.message}`)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + res(qs.parse(body)); |
| 53 | + }, |
| 54 | + ), |
| 55 | + ), |
| 56 | + ); |
| 57 | + |
| 58 | + ooth.registerPrimaryConnect( |
| 59 | + name, |
| 60 | + 'login', |
| 61 | + [], |
| 62 | + new Strategy(async (req: FullRequest, done: (e: Error | null, v: StrategyValues) => void) => { |
| 63 | + const { oauth_token, oauth_verifier } = req.body; |
| 64 | + request.post( |
| 65 | + { |
| 66 | + url: 'https://api.twitter.com/oauth/access_token', |
| 67 | + oauth: { |
| 68 | + consumer_key: clientID, |
| 69 | + consumer_secret: clientSecret, |
| 70 | + token: oauth_token, |
| 71 | + }, |
| 72 | + form: { |
| 73 | + oauth_verifier, |
| 74 | + }, |
| 75 | + }, |
| 76 | + (err: Error, _: any, body: string) => { |
| 77 | + if (err) { |
| 78 | + return done(err, {}); |
| 79 | + } |
| 80 | + |
| 81 | + if (body.indexOf('errors') > -1) { |
| 82 | + try { |
| 83 | + return done(new Error(JSON.parse(body).errors[0].message), {}); |
| 84 | + } catch (e) { |
| 85 | + return done(new Error(`Unexpected error: ${e.message}`), {}); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const { oauth_token, oauth_token_secret, user_id, screen_name } = qs.parse(body); |
| 90 | + |
| 91 | + request.get( |
| 92 | + { |
| 93 | + url: 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true', |
| 94 | + oauth: { |
| 95 | + consumer_key: clientID, |
| 96 | + consumer_secret: clientSecret, |
| 97 | + token: oauth_token, |
| 98 | + token_secret: oauth_token_secret, |
| 99 | + }, |
| 100 | + }, |
| 101 | + (err: Error, _: any, body: string) => { |
| 102 | + if (err) { |
| 103 | + return done(err, {}); |
| 104 | + } |
| 105 | + |
| 106 | + const data = JSON.parse(body); |
| 107 | + |
| 108 | + if (data.errors) { |
| 109 | + return done(new Error(data.errors[0].message), {}); |
| 110 | + } |
| 111 | + |
| 112 | + done(null, { |
| 113 | + token: oauth_token, |
| 114 | + tokenSecret: oauth_token_secret, |
| 115 | + id: user_id, |
| 116 | + username: screen_name, |
| 117 | + email: data.email, |
| 118 | + }); |
| 119 | + }, |
| 120 | + ); |
| 121 | + }, |
| 122 | + ); |
| 123 | + }), |
| 124 | + ); |
| 125 | +} |
0 commit comments