|
@@ -1,77 +1,79 @@
|
|
|
-const express = require('express');
|
|
|
|
|
-const router = express.Router();
|
|
|
|
|
-const { Email } = require('../models/email.model');
|
|
|
|
|
-const { User } = require('../models/user.model');
|
|
|
|
|
-const nodemailer = require('nodemailer');
|
|
|
|
|
|
|
+// const express = require('express');
|
|
|
|
|
+// const router = express.Router();
|
|
|
|
|
+// const jwt = require("jsonwebtoken");
|
|
|
|
|
+// const { Email } = require('../models/email.model');
|
|
|
|
|
+// const { User } = require('../models/user.model');
|
|
|
|
|
+// const nodemailer = require('nodemailer');
|
|
|
|
|
+// const role = require('../lib/role');
|
|
|
|
|
+// require('dotenv').config();
|
|
|
|
|
|
|
|
-const passport = require('passport');
|
|
|
|
|
-const GoogleStrategy = require('passport-google-oauth20').Strategy;
|
|
|
|
|
|
|
+// const passport = require('passport');
|
|
|
|
|
+// const GoogleStrategy = require('passport-google-oauth20').Strategy;
|
|
|
|
|
|
|
|
-router.use(passport.initialize());
|
|
|
|
|
-// router.use(passport.session());
|
|
|
|
|
|
|
+// router.use(passport.initialize());
|
|
|
|
|
+// //router.use(passport.session());
|
|
|
|
|
|
|
|
|
|
|
|
|
-passport.serializeUser(function (user, done) {
|
|
|
|
|
- done(null, user);
|
|
|
|
|
-});
|
|
|
|
|
|
|
+// passport.serializeUser((user, cb) => {
|
|
|
|
|
+// cb(null, user);
|
|
|
|
|
+// });
|
|
|
|
|
|
|
|
|
|
|
|
|
-passport.deserializeUser(function (user, done) {
|
|
|
|
|
- done(null, user);
|
|
|
|
|
-});
|
|
|
|
|
|
|
+// passport.deserializeUser((obj, cb) => {
|
|
|
|
|
+// cb(null, obj);
|
|
|
|
|
+// });
|
|
|
|
|
|
|
|
|
|
|
|
|
-// passport.use(new GoogleStrategy({
|
|
|
|
|
-// clientID: "931209872114-5glro42kjofspf239h7n7tq21f6ntgqq.apps.googleusercontent.com",
|
|
|
|
|
-// clientSecret: "sBiZm-fvgtCVVF3db7naVEwn",
|
|
|
|
|
-// callbackURL: "http://localhost:4000/api/auth/google/callback"
|
|
|
|
|
-// },
|
|
|
|
|
-
|
|
|
|
|
-// async (accessToken, refreshToken, profile, done) => {
|
|
|
|
|
|
|
+// // passport.use(new GoogleStrategy({
|
|
|
|
|
+// // clientID: "493465923713-qrbgtq8d53iguine1gf928483mn4odes.apps.googleusercontent.com",
|
|
|
|
|
+// // clientSecret: "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",
|
|
|
|
|
+// // callbackURL: "http://localhost:4000/api/auth/google/callback"
|
|
|
|
|
+// // },
|
|
|
|
|
|
|
|
|
|
+// // async (accessToken, refreshToken, profile, done) => {
|
|
|
|
|
|
|
|
-// console.log(profile.emails[0].value)
|
|
|
|
|
|
|
|
|
|
-// return done(null, profile);
|
|
|
|
|
|
|
+// // console.log(profile.emails[0].value)
|
|
|
|
|
|
|
|
-// var email = profile.emails[0].value
|
|
|
|
|
-// const user = await User.findOne({email: email})
|
|
|
|
|
|
|
+// // return done(null, profile);
|
|
|
|
|
|
|
|
|
|
+// // var email = profile.emails[0].value
|
|
|
|
|
+// // const user = await User.findOne({email: email})
|
|
|
|
|
|
|
|
-// if (user) {
|
|
|
|
|
-// console.log(user)
|
|
|
|
|
-// user.username = accessToken;
|
|
|
|
|
|
|
|
|
|
|
|
+// // if (user) {
|
|
|
|
|
+// // console.log(user)
|
|
|
|
|
+// // user.username = accessToken;
|
|
|
|
|
|
|
|
-// user.save().then(()=>{
|
|
|
|
|
-// return done(null, user);
|
|
|
|
|
-// })
|
|
|
|
|
-// }else{
|
|
|
|
|
|
|
|
|
|
-// console.log('creation of user')
|
|
|
|
|
-// make a new record
|
|
|
|
|
-// let user = await new User({
|
|
|
|
|
-// googleId: profile.id,
|
|
|
|
|
-// email: profile.emails[0].value,
|
|
|
|
|
-// accessToken : accessToken,
|
|
|
|
|
-// refreshToken : refreshToken
|
|
|
|
|
-// }).save()
|
|
|
|
|
-// done(null, user);
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+// // user.save().then(()=>{
|
|
|
|
|
+// // return done(null, user);
|
|
|
|
|
+// // })
|
|
|
|
|
+// // }else{
|
|
|
|
|
|
|
|
|
|
+// // console.log('creation of user')
|
|
|
|
|
+// // //make a new record
|
|
|
|
|
+// // let user = await new User({
|
|
|
|
|
+// // googleId: profile.id,
|
|
|
|
|
+// // email: profile.emails[0].value,
|
|
|
|
|
+// // accessToken : accessToken,
|
|
|
|
|
+// // refreshToken : refreshToken
|
|
|
|
|
+// // }).save()
|
|
|
|
|
+// // done(null, user);
|
|
|
|
|
+// // }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-// console.log('acces token ',accessToken);
|
|
|
|
|
-// console.log('refresh token ',refreshToken);
|
|
|
|
|
-// console.log('prfile ',profile.emails[0].value);
|
|
|
|
|
-// console.log('done ',done);
|
|
|
|
|
|
|
|
|
|
|
|
+// // console.log('acces token ',accessToken);
|
|
|
|
|
+// // console.log('refresh token ',refreshToken);
|
|
|
|
|
+// // console.log('prfile ',profile.emails[0].value);
|
|
|
|
|
+// // console.log('done ',done);
|
|
|
|
|
|
|
|
-// }
|
|
|
|
|
-// ));
|
|
|
|
|
|
|
|
|
|
|
|
+// // }
|
|
|
|
|
+// // ));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -81,92 +83,78 @@ passport.deserializeUser(function (user, done) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-passport.use(new GoogleStrategy({
|
|
|
|
|
- clientID: "931209872114-5glro42kjofspf239h7n7tq21f6ntgqq.apps.googleusercontent.com",
|
|
|
|
|
- clientSecret: "sBiZm-fvgtCVVF3db7naVEwn",
|
|
|
|
|
- callbackURL: "http://localhost:4000/api/auth/google/callback"
|
|
|
|
|
-},
|
|
|
|
|
- function (request, accessToken, refreshToken, profile, done) {
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- User.findOne({ email: profile.emails[0].value })
|
|
|
|
|
- .then(
|
|
|
|
|
- (user) => {
|
|
|
|
|
- if (user) {
|
|
|
|
|
- //if we already have a record with the given profile ID
|
|
|
|
|
- done(null, user);
|
|
|
|
|
- console.log('profile', user)
|
|
|
|
|
-
|
|
|
|
|
- } else {
|
|
|
|
|
- //if not, create a new user
|
|
|
|
|
- // new User({
|
|
|
|
|
- // googleId: profile.id,
|
|
|
|
|
- // }).save().then((newUser) => {
|
|
|
|
|
- // done(null, newUser)
|
|
|
|
|
- // }
|
|
|
|
|
- // )
|
|
|
|
|
- console.log('done')
|
|
|
|
|
- done(null, newUser)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
- .catch()
|
|
|
|
|
- // User.findOne({ email: profile.emails[0].value }, function(err, user) {
|
|
|
|
|
- // if(err) {
|
|
|
|
|
- // console.log(err); // handle errors!
|
|
|
|
|
- // }
|
|
|
|
|
- // if (!err && user !== null) {
|
|
|
|
|
- // console.log(user)
|
|
|
|
|
- // done(null, user);
|
|
|
|
|
- // } else {
|
|
|
|
|
- // console.log('fqsdffqef')
|
|
|
|
|
- // user = new User({
|
|
|
|
|
- // oauthID: profile.id,
|
|
|
|
|
- // name: profile.displayName,
|
|
|
|
|
- // created: Date.now()
|
|
|
|
|
- // });
|
|
|
|
|
- // user.save(function(err) {
|
|
|
|
|
- // if(err) {
|
|
|
|
|
- // console.log(err); // handle errors!
|
|
|
|
|
- // } else {
|
|
|
|
|
- // console.log("saving user ...");
|
|
|
|
|
- // done(null, user);
|
|
|
|
|
- // }
|
|
|
|
|
- // });
|
|
|
|
|
- // }
|
|
|
|
|
- // });
|
|
|
|
|
- }
|
|
|
|
|
-));
|
|
|
|
|
|
|
+// passport.use(new GoogleStrategy({
|
|
|
|
|
+// clientID: "493465923713-qrbgtq8d53iguine1gf928483mn4odes.apps.googleusercontent.com",
|
|
|
|
|
+// clientSecret: "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",
|
|
|
|
|
+// callbackURL: "http://localhost:4000/api/auth/google/callback"
|
|
|
|
|
+// },
|
|
|
|
|
+// async (req, accessToken, refreshToken, profile, cb) => {
|
|
|
|
|
+
|
|
|
|
|
+// User.findOne({ email: profile.emails[0].value }, (err, user) => {
|
|
|
|
|
+// if (err) {
|
|
|
|
|
+// cb(err); // handle errors!
|
|
|
|
|
+// }
|
|
|
|
|
+// if (!err && user !== null) {
|
|
|
|
|
+// cb(err, user);
|
|
|
|
|
+// }
|
|
|
|
|
+// else {
|
|
|
|
|
+
|
|
|
|
|
+// user = new User({
|
|
|
|
|
+// googleId: profile.id,
|
|
|
|
|
+// email: profile.emails[0].value,
|
|
|
|
|
+// firstname: profile.name.givenName,
|
|
|
|
|
+// lastname: profile.name.familyName,
|
|
|
|
|
+// role: role.Client,
|
|
|
|
|
+// isActive: true,
|
|
|
|
|
+// isGain: false,
|
|
|
|
|
+// });
|
|
|
|
|
+
|
|
|
|
|
+// user.save((err) => {
|
|
|
|
|
+// if (err) {
|
|
|
|
|
+// cb(err); // handle errors!
|
|
|
|
|
+// } else {
|
|
|
|
|
+// cb(null, user);
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// }
|
|
|
|
|
+// ));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-router.get('/', passport.authenticate('google', { session: false, scope: ['profile', 'email'] }));
|
|
|
|
|
|
|
+// router.get('/', passport.authenticate('google', { session: false, scope: ['profile', 'email'] }),
|
|
|
|
|
+// async () => {
|
|
|
|
|
+// console.log('je suis dans mon get root')
|
|
|
|
|
+// });
|
|
|
|
|
|
|
|
-// router.get('/callback', passport.authenticate('google') )
|
|
|
|
|
|
|
+// // router.get('/callback', passport.authenticate('google') )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-router.get('/callback', passport.authenticate('google', { failureRedirect: '/failed' }),
|
|
|
|
|
- function (req, res) {
|
|
|
|
|
- // res.redirect('/good')
|
|
|
|
|
- // console.log("success");
|
|
|
|
|
- // // Successful authentication, redirect home.
|
|
|
|
|
- res.status(200).json('ds');
|
|
|
|
|
- });
|
|
|
|
|
|
|
+// router.get('/callback', passport.authenticate('google', { failureRedirect: '/failed' }),
|
|
|
|
|
+// async (req, res) => {
|
|
|
|
|
+// console.log('je susi dans ma fonction callback ', req.user)
|
|
|
|
|
+// const token = jwt.sign({ userId: req.user._id, email: req.user.email, role: req.user.role }, process.env.SECRET_KEY, { expiresIn: '10 h' })
|
|
|
|
|
+// //res.status(200).json({ success: true, token, expireIn: `${new Date().getTime() + 120000}` })
|
|
|
|
|
+// console.log(token)
|
|
|
|
|
+// res.redirect(`${process.env.URL}/auth/?token=${token}`);
|
|
|
|
|
+// });
|
|
|
|
|
|
|
|
|
|
|
|
|
-//failed auth google
|
|
|
|
|
-router.get('/failed', async (req, res) => { res.status(404).send('erreur authentification') })
|
|
|
|
|
|
|
+// //failed auth google
|
|
|
|
|
+// router.get('/failed', async (req, res) => { res.status(404).send('erreur authentification') })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-//get all users
|
|
|
|
|
-router.get('/good', async (req, res) => {
|
|
|
|
|
|
|
+// //get all users
|
|
|
|
|
+// router.get('/good', async (req, res) => {
|
|
|
|
|
|
|
|
- res.status(200).send('welcome mr ');
|
|
|
|
|
-})
|
|
|
|
|
|
|
+// res.status(200).send('welcome mr ');
|
|
|
|
|
+// })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -174,4 +162,4 @@ router.get('/good', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-module.exports = router;
|
|
|
|
|
|
|
+// module.exports = router;
|