File size: 1,300 Bytes
3b6afc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { Strategy: GitHubStrategy } = require('passport-github2');
const config = require('../../config/loader');
const domains = config.domains;

const User = require('../models/User');

// GitHub strategy
const githubLogin = async () =>
  new GitHubStrategy(
    {
      clientID: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
      callbackURL: `${domains.server}${process.env.GITHUB_CALLBACK_URL}`,
      proxy: false,
      scope: ['user:email'], // Request email scope
    },
    async (accessToken, refreshToken, profile, cb) => {
      try {
        let email;
        if (profile.emails && profile.emails.length > 0) {
          email = profile.emails[0].value;
        }

        const oldUser = await User.findOne({ email });
        if (oldUser) {
          return cb(null, oldUser);
        }

        const newUser = await new User({
          provider: 'github',
          githubId: profile.id,
          username: profile.username,
          email,
          emailVerified: profile.emails[0].verified,
          name: profile.displayName,
          avatar: profile.photos[0].value,
        }).save();

        cb(null, newUser);
      } catch (err) {
        console.error(err);
        cb(err);
      }
    },
  );

module.exports = githubLogin;