|
@@ -1,17 +1,18 @@
|
|
|
-// 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');
|
|
|
|
|
+const config = require('../config/config.json');
|
|
|
|
|
+//require('dotenv').config();
|
|
|
|
|
|
|
|
const passport = require('passport');
|
|
const passport = require('passport');
|
|
|
const GoogleStrategy = require('passport-google-oauth20').Strategy;
|
|
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) => {
|
|
passport.serializeUser((user, cb) => {
|
|
@@ -84,31 +85,32 @@ passport.deserializeUser((obj, cb) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-// 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,
|
|
|
|
|
-// });
|
|
|
|
|
|
|
+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 {
|
|
|
|
|
+ console.log('acces token ',accessToken);
|
|
|
|
|
+
|
|
|
|
|
+ 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) => {
|
|
user.save((err) => {
|
|
|
if (err) {
|
|
if (err) {
|
|
@@ -135,26 +137,25 @@ async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-// 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 }, config.secret_key, { expiresIn: '10 h' })
|
|
|
|
|
+ // Return the JWT token as JSON data
|
|
|
|
|
+ res.json({ 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('/users', async (req, res) => {
|
|
|
|
|
|
|
|
-// res.status(200).send('welcome mr ');
|
|
|
|
|
-// })
|
|
|
|
|
|
|
+ res.json(req.user);
|
|
|
|
|
+})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|