1// my github https://github.com/restuwahyu13
2const { AuthSchema } = require('../models/model.auth')
3const passport = require('passport')
4const JwtStrategy = require('passport-jwt').Strategy
5const ExtractJwt = require('passport-jwt').ExtractJwt
6const LocalStrategy = require('passport-local').Strategy
7
8exports.passportSerialize = () => {
9 return passport.serializeUser(async (user, done) => {
10 if (user) {
11 const { _id } = user
12 const result = await AuthSchema.findById(_id).lean()
13 if (!result) return done(null, false)
14 return done(null, result._id)
15 }
16 return done(null, false)
17 })
18}
19
20exports.passportDeserialize = () => {
21 return passport.deserializeUser(async (id, done) => {
22 if (id) {
23 const user = await AuthSchema.findById(id).lean()
24 if (!user) return done(null, false)
25 return done(null, user)
26 }
27 return done(null, false)
28 })
29}
30
31// passport local
32exports.passportLocalStrategy = () => {
33 passport.use(
34 'local',
35 new LocalStrategy(async (username, password, done) => {
36 if (username && password) {
37 const user = await AuthSchema.find({ $or: [{ username }, { email: username }] }).lean()
38 const verify = AuthSchema.verifyPassword(password, user[0].password)
39
40 if (!verify) return done(null, false)
41 return done(null, user[0])
42 }
43 return done(null, false)
44 })
45 )
46}
47
48// passport jwt
49exports.passportJwtStrategy = () => {
50 passport.use(
51 'jwt',
52 new JwtStrategy(
53 {
54 secretOrKey: process.env.JWT_SECRET,
55 jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
56 },
57 async ({ _id }, done) => {
58 try {
59 const user = await AuthSchema.findById(_id).lean()
60 if (!user) done(null, false)
61 done(null, user)
62 } catch (err) {
63 done(err, false)
64 }
65 }
66 )
67 )
68}
1// my github https://github.com/restuwahyu13
2
3const { AuthSchema } = require('../models/model.auth')
4const GoogleStrategy = require('passport-google-oauth20').Strategy
5const FacebookStrategy = require('passport-facebook').Strategy
6const GithubStrategy = require('passport-github').Strategy
7
8// passport facebook
9exports.passportFacebook = () => {
10 return passport.use(
11 new FacebookStrategy(
12 {
13 clientID: process.env.FACEBOOK_ID,
14 clientSecret: process.env.FACEBOOK_SECRET,
15 callbackURL: "http://localhost:3000/auth/facebook/callback",
16 profileFields: ["id", "displayName", "gender", "email", "photos"],
17 enableProof: true,
18 },
19 (accessToken, refreshToken, profile, done) => {
20 authSocialSchema.findOne({idSocial: profile.id}, (err, user) => {
21 if (err) return done(err, false);
22 if (!user) {
23 authSocialSchema.findOrCreate(
24 {
25 idSocial: profile.id,
26 fullname: profile.displayName,
27 email: profile.email,
28 gender: profile.gende,
29 avatar: profile.photos[0]["value"],
30 provider: profile.provider,
31 created_at: Date.now(),
32 },
33 (err, user) => {
34 if (err) return done(err, false);
35 return done(null, user);
36 }
37 );
38 } else {
39 return done(null, user);
40 }
41 });
42 }
43 )
44 );
45}
46
47// passport google
48exports.passportGoogle = () => {
49 return passport.use(
50 new GoogleStrategy(
51 {
52 clientID: process.env.GOOGLE_ID,
53 clientSecret: process.env.GOOGLE_SECRET,
54 callbackURL: "http://localhost:3000/auth/google/callback",
55 },
56 (accessToken, refreshToken, profile, done) => {
57 authSocialSchema.findOne({idSocial: profile.id}, (err, user) => {
58 if (err) return done(err, false);
59 if (!user) {
60 authSocialSchema.findOrCreate(
61 {
62 idSocial: profile.id,
63 fullname: profile.displayName,
64 email: profile.emails[0]["value"],
65 avatar: profile.photos[0]["value"],
66 provider: profile.provider,
67 created_at: Date.now(),
68 },
69 (err, user) => {
70 if (err) return done(err, false);
71 return done(null, user);
72 }
73 );
74 } else {
75 return done(null, user);
76 }
77 });
78 }
79 )
80 );
81}
82
83// passport passport github
84exports.passportGithub = () => {
85 return passport.use(
86 new GithubStrategy(
87 {
88 clientID: process.env.GITHUB_ID,
89 clientSecret: process.env.GITHUB_SECRET,
90 callbackURL: "http://localhost:3000/auth/github/callback",
91 },
92 (accessToken, refreshToken, profile, done) => {
93 authSocialSchema.findOne({idSocial: profile.id}, (err, user) => {
94 if (err) return done(err, false);
95 if (!user) {
96 authSocialSchema.findOrCreate(
97 {
98 idSocial: profile.id,
99 username: profile.username,
100 fullname: profile.displayName,
101 avatar: profile.photos[0]["value"],
102 provider: profile.provider,
103 created_at: Date.now(),
104 },
105 (err, user) => {
106 if (err) return done(err, false);
107 return done(null, user);
108 }
109 );
110 } else {
111 return done(null, user);
112 }
113 });
114 }
115 )
116 );
117}
1var GoogleStrategy = require('passport-google-oauth20').Strategy;
2
3passport.use(new GoogleStrategy({
4 clientID: GOOGLE_CLIENT_ID,
5 clientSecret: GOOGLE_CLIENT_SECRET,
6 callbackURL: "http://www.example.com/auth/google/callback"
7 },
8 function(accessToken, refreshToken, profile, cb) {
9 User.findOrCreate({ googleId: profile.id }, function (err, user) {
10 return cb(err, user);
11 });
12 }
13));
14