|
@@ -1,79 +1,79 @@
|
|
|
-// 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 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((user, cb) => {
|
|
|
|
|
-// cb(null, user);
|
|
|
|
|
-// });
|
|
|
|
|
|
|
+passport.serializeUser((user, cb) => {
|
|
|
|
|
+ cb(null, user);
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
|
|
|
|
|
-// passport.deserializeUser((obj, cb) => {
|
|
|
|
|
-// cb(null, obj);
|
|
|
|
|
-// });
|
|
|
|
|
|
|
+passport.deserializeUser((obj, cb) => {
|
|
|
|
|
+ cb(null, obj);
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
|
|
|
|
|
-// // passport.use(new GoogleStrategy({
|
|
|
|
|
-// // clientID: "493465923713-qrbgtq8d53iguine1gf928483mn4odes.apps.googleusercontent.com",
|
|
|
|
|
-// // clientSecret: "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",
|
|
|
|
|
-// // callbackURL: "http://localhost:4000/api/auth/google/callback"
|
|
|
|
|
-// // },
|
|
|
|
|
|
|
+// 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) => {
|
|
|
|
|
|
|
+// async (accessToken, refreshToken, profile, done) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
-// // console.log(profile.emails[0].value)
|
|
|
|
|
|
|
+// console.log(profile.emails[0].value)
|
|
|
|
|
|
|
|
-// // return done(null, profile);
|
|
|
|
|
|
|
+// return done(null, profile);
|
|
|
|
|
|
|
|
-// // var email = profile.emails[0].value
|
|
|
|
|
-// // const user = await User.findOne({email: email})
|
|
|
|
|
|
|
+// 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{
|
|
|
|
|
|
|
+// 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('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);
|
|
|
|
|
|
|
|
|
|
|
|
|
-// // }
|
|
|
|
|
-// // ));
|
|
|
|
|
|
|
+// }
|
|
|
|
|
+// ));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -84,77 +84,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-// 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);
|
|
|
|
|
-// }
|
|
|
|
|
-// });
|
|
|
|
|
-// }
|
|
|
|
|
-// });
|
|
|
|
|
-// }
|
|
|
|
|
-// ));
|
|
|
|
|
|
|
+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'] }),
|
|
|
|
|
-// async () => {
|
|
|
|
|
-// console.log('je suis dans mon get root')
|
|
|
|
|
-// });
|
|
|
|
|
|
|
+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' }),
|
|
|
|
|
-// 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}`);
|
|
|
|
|
-// });
|
|
|
|
|
|
|
+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 ');
|
|
|
|
|
+})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -162,4 +162,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-// module.exports = router;
|
|
|
|
|
|
|
+module.exports = router;
|