|
| 1 | +import { Strategy as GitHubStrategy } from 'passport-github2' |
| 2 | +import jwt from 'jsonwebtoken' |
| 3 | +import requestPromise from 'request-promise' |
| 4 | +import { github, oauthCallbacks } from '../../config/secrets' |
| 5 | +import { userExists, userBuilds, userUpdate } from '../../modules/users' |
| 6 | +import { mailChimpConnect } from '../mail/mailchimp' |
| 7 | + |
| 8 | +interface UserData { |
| 9 | + id?: number |
| 10 | + provider?: string |
| 11 | + provider_username?: string |
| 12 | + provider_id?: string |
| 13 | + provider_email?: string |
| 14 | + name?: string |
| 15 | + username?: string |
| 16 | + picture_url?: string |
| 17 | + website?: string |
| 18 | + profile_url?: string |
| 19 | + repos?: number |
| 20 | + email?: string |
| 21 | + login_strategy?: string |
| 22 | + token?: string |
| 23 | +} |
| 24 | + |
| 25 | +export const createGitHubStrategy = () => { |
| 26 | + if (!github.id || !github.secret) { |
| 27 | + return null |
| 28 | + } |
| 29 | + |
| 30 | + return new GitHubStrategy( |
| 31 | + { |
| 32 | + clientID: github.id, |
| 33 | + clientSecret: github.secret, |
| 34 | + callbackURL: oauthCallbacks.githubCallbackUrl, |
| 35 | + passReqToCallback: true, |
| 36 | + scope: ['user:email', 'read:org'] |
| 37 | + }, |
| 38 | + async (req: any, accessToken: string, accessTokenSecret: string, profile: any, done: any) => { |
| 39 | + try { |
| 40 | + const githubEmail = profile.emails ? profile.emails[0].value : profile._json.email |
| 41 | + const userEmail = req.query.state |
| 42 | + const email = userEmail || githubEmail |
| 43 | + |
| 44 | + const data: UserData = { |
| 45 | + provider: profile.provider, |
| 46 | + provider_username: profile.username, |
| 47 | + provider_id: profile.id, |
| 48 | + provider_email: githubEmail, |
| 49 | + name: profile.displayName, |
| 50 | + username: profile.username, |
| 51 | + picture_url: profile.photos[0].value, |
| 52 | + website: profile._json.blog, |
| 53 | + profile_url: profile.profileUrl, |
| 54 | + repos: 0, |
| 55 | + email: email |
| 56 | + } |
| 57 | + |
| 58 | + if (userEmail) { |
| 59 | + data.login_strategy = 'local' |
| 60 | + } else { |
| 61 | + data.login_strategy = 'github' |
| 62 | + } |
| 63 | + |
| 64 | + if (!email) { |
| 65 | + return done(null) |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + const response = await requestPromise({ |
| 70 | + uri: `https://api.github.com/users/${profile.username}/repos`, |
| 71 | + headers: { |
| 72 | + 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0', |
| 73 | + authorization: `token ${accessToken}` |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + data.repos = JSON.parse(response).length |
| 78 | + const user = await userExists(data) |
| 79 | + |
| 80 | + if (user) { |
| 81 | + await userUpdate({ ...data, id: user.id }) |
| 82 | + const token = jwt.sign( |
| 83 | + { id: data.id, email: data.email }, |
| 84 | + process.env.SECRET_PHRASE as string |
| 85 | + ) |
| 86 | + data.token = token |
| 87 | + return done(null, data) |
| 88 | + } else { |
| 89 | + await userBuilds(data) |
| 90 | + const token = jwt.sign( |
| 91 | + { id: data.id, email: data.email }, |
| 92 | + process.env.SECRET_PHRASE as string |
| 93 | + ) |
| 94 | + data.token = token |
| 95 | + mailChimpConnect(data.email as string) |
| 96 | + return done(null, data) |
| 97 | + } |
| 98 | + } catch (e) { |
| 99 | + // eslint-disable-next-line no-console |
| 100 | + console.log('Error fetching GitHub repositories') |
| 101 | + // eslint-disable-next-line no-console |
| 102 | + console.log(e) |
| 103 | + return done(null) |
| 104 | + } |
| 105 | + } catch (error) { |
| 106 | + // eslint-disable-next-line no-console |
| 107 | + console.log('Error in github-strategy.ts configuration file - search users') |
| 108 | + // eslint-disable-next-line no-console |
| 109 | + console.log(error) |
| 110 | + return done(null) |
| 111 | + } |
| 112 | + } |
| 113 | + ) |
| 114 | +} |
0 commit comments