158 lines
5.5 KiB
JavaScript
158 lines
5.5 KiB
JavaScript
const express = require('express')
|
|
const next = require('next')
|
|
require('dotenv').config()
|
|
|
|
const axios = require('axios')
|
|
|
|
// const nextAuth = require('next-auth')
|
|
// const nextAuthConfig = require('./next-auth.config')
|
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
|
const app = next({ dev })
|
|
const handle = app.getRequestHandler()
|
|
|
|
const passport = require('passport')
|
|
const FacebookStrategy = require('passport-facebook').Strategy
|
|
const LinkedInStrategy = require('@sokratis/passport-linkedin-oauth2').Strategy
|
|
const bodyParser = require('body-parser');
|
|
|
|
passport.use(new FacebookStrategy({
|
|
clientID: process.env.FACEBOOK_ID,
|
|
clientSecret: process.env.FACEBOOK_SECRET,
|
|
callbackURL: "http://localhost:3000/auth/facebook/callback",
|
|
enableProof: true,
|
|
passReqToCallback: true
|
|
},
|
|
function (req, token, tokenSecret, profile, done) {
|
|
process.nextTick(function () {
|
|
console.log("fb verifyfunction")
|
|
// To keep the example simple, the user's Linkedin profile is returned to
|
|
// represent the logged-in user. In a typical application, you would want
|
|
// to associate the Linkedin account with a user record in your database,
|
|
// and return that user instead.
|
|
// console.log(token)
|
|
// console.log(profile)
|
|
|
|
// ! as we are in pure JWT strategie we pass token to authentify on our web services
|
|
return done(null, token);
|
|
});
|
|
}
|
|
));
|
|
|
|
passport.use(new LinkedInStrategy({
|
|
clientID: process.env.LINKEDIN_ID,
|
|
clientSecret: process.env.LINKEDIN_SECRET,
|
|
callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
|
|
scope: ['r_emailaddress', 'r_liteprofile'],
|
|
profileFields: ['id', 'first-name', 'last-name', 'location', 'email-address', 'picture-urls::(original)'],
|
|
passReqToCallback: true
|
|
},
|
|
function (req, token, tokenSecret, profile, done) {
|
|
console.log("linkedin verifyfunction")
|
|
process.nextTick(function () {
|
|
// To keep the example simple, the user's Linkedin profile is returned to
|
|
// represent the logged-in user. In a typical application, you would want
|
|
// to associate the Linkedin account with a user record in your database,
|
|
// and return that user instead.
|
|
//console.log(token)
|
|
// ! as we are in pure JWT strategie we pass token to authentify on our web services
|
|
return done(null, token);
|
|
});
|
|
|
|
}
|
|
));
|
|
|
|
app.prepare()
|
|
.then(() => {
|
|
var jsonParser = bodyParser.json()
|
|
// Get Express and instance of Express from NextAuth
|
|
const server = express()
|
|
server.use(passport.initialize());
|
|
|
|
server.get('/auth/facebook',
|
|
passport.authorize('facebook', { scope: ['email', 'public_profile'], session: false, failureRedirect: '/login'}));
|
|
|
|
server.get('/auth/facebook/callback',
|
|
passport.authorize('facebook', { failureRedirect: '/login', session: false }),
|
|
(req, res) => {
|
|
|
|
|
|
console.log('facebookcallback')
|
|
console.log(req.account)//don't understand why !
|
|
// console.log(req.accessToken)
|
|
// console.log(req)
|
|
|
|
axios.post("http://localhost/app_dev.php/api/login_check_oauth", { 'oauth-token': req.account, 'oauth-type': 'facebook' })
|
|
.then((resApi) => {
|
|
console.log('$$$$$$$$$$$retreive user from token validation$$$$$$$$$$$$$$')
|
|
console.log(resApi)
|
|
return res.redirect('/AuthRouter?JWT=' + resApi.data.token)
|
|
}
|
|
).catch(err => { console.log(err) })
|
|
})
|
|
|
|
server.get('/auth/linkedin',
|
|
passport.authorize('linkedin', { scope: ['r_basicprofile', 'r_emailaddress', 'r_liteprofile'], session: false , failureRedirect: '/login'}));
|
|
|
|
server.get('/auth/linkedin/callback',
|
|
passport.authorize('linkedin', { failureRedirect: '/login', session: false }),
|
|
function (error, req, res, next) {
|
|
if (error) {
|
|
console.log(error)
|
|
return res.redirect('/login?error='+error.message)
|
|
}
|
|
// Successful authentication, redirect home.
|
|
console.log('linkedincallback')
|
|
console.log(req.accessToken)
|
|
console.log(req)
|
|
|
|
axios.post("http://localhost/app_dev.php/api/login_check_oauth", { 'oauth-token': req.accessToken, 'oauth-type': 'linkedin' })
|
|
.then((resApi) => {
|
|
console.log('$$$$$$$$$$$retreive user from token validation$$$$$$$$$$$$$$')
|
|
console.log(resApi)
|
|
return res.redirect('/AuthRouter?JWT=' + resApi.data.token)
|
|
}
|
|
).catch(err => { console.log(err) })
|
|
|
|
});
|
|
|
|
server.get('/Offre/:id', (req, res) => {
|
|
const actualPage = '/Offre'
|
|
const queryParams = { id: req.params.id }
|
|
app.render(req, res, actualPage, queryParams)
|
|
})
|
|
|
|
server.post('/NewsletterSubscription', jsonParser, (req,res) => {
|
|
console.log(req.body.email)
|
|
let email = req.body.email;
|
|
|
|
if(!!!email)
|
|
{
|
|
console.log('email absent to NewsletterSubscription')
|
|
res.status(404).json({ errors: { global: "email absent to NewsletterSubscription" } });
|
|
}
|
|
|
|
var mailjet = require ('node-mailjet')
|
|
.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
|
|
|
|
mailjet.post('contact')
|
|
.request({Email: email})
|
|
.catch((err)=>console.log(err));
|
|
|
|
res.json({success:true});
|
|
})
|
|
|
|
server.get('*', (req, res) => {
|
|
return handle(req, res)
|
|
})
|
|
|
|
server.listen(process.env.PORT, (err) => {
|
|
if (err) throw err
|
|
console.log('> Ready on http://localhost:' + process.env.PORT)
|
|
})
|
|
|
|
})
|
|
.catch((ex) => {
|
|
console.error(ex.stack)
|
|
process.exit(1)
|
|
}) |