-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthenticate.js
122 lines (117 loc) · 3.42 KB
/
authenticate.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var passport= require('passport');
var User= require('./models/users');
var GoogleStrategy=require('passport-google-oauth20');
var FacebookStrategy= require('passport-facebook').Strategy;
var GitHubStrategy=require('passport-github2').Strategy;
require('dotenv').config();
passport.serializeUser((user,done)=>{
done(null,user.id);
});
passport.deserializeUser((id,done)=>{
User.findById(id)
.then((user)=>{
done(null,user)
});
});
passport.use(
new GoogleStrategy({
callbackURL:`${process.env.DOMAIN_NAME}/auth/google/redirect`,
clientID:process.env.google_client_id,
clientSecret:process.env.google_client_secret
},(accessToken,refreshToken,profile,done)=>{
console.log("profile:",profile._json.email)
User.findOne({googleId:profile.id})
.then((user)=>{
if(user===null)
{
new User({
username:profile.displayName,
name:`${profile.name.givenName} ${profile.name.familyName}`,
googleId:profile.id,
email:profile._json.email,
}).save()
.then((user)=>{
// console.log("new user created",user)
done(null,user)
})
.catch((err)=>{
console.log(err);
})
}
else{
console.log("already present");
done(null,user)
}
})
.catch(err=>{
console.log(err)
})
}))
passport.use(new FacebookStrategy({
callbackURL:'/auth/facebook/redirect',
clientID:process.env.facebook_client_id,
clientSecret:process.env.facebook_client_secret,
profileFields: ['id', 'email', 'name','displayName']
},(accessToken,refreshToken,profile,done)=>{
console.log("facebook profile", profile);
User.findOne({facebookId:profile.id})
.then((user)=>{
if(user===null)
{
new User({
username:profile.displayName,
facebookId:profile.id,
name:`${profile.name.givenName} ${profile.name.familyName}`,
}).save()
.then((user)=>{
console.log("new user created",user);
done(null,user);
})
.catch((err)=>{
console.log(err)
})
}
else{
console.log("already present");
done(null,user)
}
})
.catch((err)=>{
console.log(err)
})
}))
passport.use(new GitHubStrategy({
clientID: process.env.github_client_id,
clientSecret: process.env.github_client_secret,
callbackURL: `${process.env.DOMAIN_NAME}/auth/github/callback`,
scope: ['user:email']
},
(accessToken,refreshToken,profile,done)=>{
console.log("github profile", profile.emails[0].value);
User.findOne({githubId:profile.id})
.then((user)=>{
if(user===null)
{
new User({
username:profile.username,
githubId:profile.id,
name:`${profile.username}`,
email:profile.emails[0].value,
}).save()
.then((user)=>{
console.log("new user created",user);
done(null,user);
})
.catch((err)=>{
console.log(err)
})
}
else{
console.log("already present");
done(null,user)
}
})
.catch((err)=>{
console.log(err)
})
}));