Last active
March 16, 2021 18:59
-
-
Save k1000/b332775661394e86bb2d5fa8c5626e36 to your computer and use it in GitHub Desktop.
implementation Marvel API, mongodb, Auth0 (Github)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require("crypto"); | |
const fetch = require('node-fetch'); | |
const express = require('express'); | |
const lusca = require('lusca'); | |
const bodyParser = require('body-parser'); | |
const mongoose = require('mongoose'); | |
const session = require('express-session'); | |
const passport = require('passport'); | |
const GithubStrategy = require('passport-github').Strategy; | |
require('dotenv').config(); | |
const PORT = process.env.PORT || 5000 | |
passport.use(new GithubStrategy({ | |
clientID: process.env.GITHUB_CLIENT_ID, | |
clientSecret: process.env.GITHUB_CLIENT_SECRET, | |
callbackURL: "https://marvelouz.herokuapp.com/auth/github/callback" | |
}, | |
function(accessToken, refreshToken, profile, done) { | |
return done(null, profile); | |
} | |
)); | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(user, done) { | |
done(null, user); | |
}); | |
const mongo_uri = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@cluster0.u4tzh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`; | |
mongoose.connect(mongo_uri, {useNewUrlParser: true, useUnifiedTopology: true}); | |
const Character = mongoose.model('Character', { name: String, id: Number, thumbnail: String }); | |
const MARVEL_API = "https://gateway.marvel.com:443/v1/public"; | |
const ts = new Date().getTime(); | |
const hash = crypto.createHash('md5').update( | |
`${ts}${process.env.MARVEL_PRIV_KEY}${process.env.MARVEL_PUB_KEY}`).digest('hex'); | |
const api_url = `${MARVEL_API}/characters?ts=${ts}&apikey=${process.env.MARVEL_PUB_KEY}&hash=${hash}`; | |
fetch(api_url) | |
.then(res => res.json()) | |
.then(json => json.data.results) | |
.then(data => { | |
//console.log(data) | |
data.map(ch => { | |
const character = new Character( | |
{name: ch.name, id:ch.id, thumbnail:ch.thumbnail.path}) | |
character.save().then(() => console.log(ch.name)); | |
}) | |
}) | |
const app = express(); | |
app.use(session({secret: process.env.SECRET})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(lusca.xframe('SAMEORIGIN')); | |
app.use(lusca.xssProtection(true)); | |
app.disable('x-powered-by'); | |
app.get('/auth/github', passport.authenticate('github')); | |
app.get('/auth/github/callback', | |
passport.authenticate('github', { failureRedirect: '/' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
app.get('/', function (req, res) { | |
if (req.isAuthenticated()) { | |
Character.find({}) | |
.then(ch => res.send(JSON.stringify(ch))) | |
.catch(e => console.log(e)) | |
} else { | |
res.send("<ul>\ | |
<li><a href='/auth/github'>GitHub</a></li>\ | |
<li><a href='/logout'>logout</a></li>\ | |
</ul>"); | |
} | |
}); | |
app.get('/logout', function(req, res){ | |
console.log('logging out'); | |
req.logout(); | |
res.redirect('/'); | |
}); | |
function ensureAuthenticated(req, res, next) { | |
if (req.isAuthenticated()) { return next(); } | |
res.redirect('/') | |
} | |
app.get('/protected', ensureAuthenticated, function(req, res) { | |
res.send("acess granted"); | |
}); | |
app.listen(PORT, () => console.log(`Listening on ${ PORT }`)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "marvelouz", | |
"version": "0.0.1", | |
"description": "marvelouz app", | |
"engines": { | |
"node": "14.x" | |
}, | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "node test.js" | |
}, | |
"dependencies": { | |
"cors": "^2.8.5", | |
"dotenv": "^8.2.0", | |
"express": "^4.17.1", | |
"express-session": "^1.17.1", | |
"mongoose": "^5.11.15", | |
"node-fetch": "^2.6.1", | |
"passport": "^0.4.1", | |
"passport-github": "^1.1.0", | |
"lusca": "^1.7.0" | |
}, | |
"devDependencies": { | |
"got": "^11.3.0", | |
"tape": "^4.7.0" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https://gist.github.com/k1000/1bede1a4753ea1453ccbda11611d1d28" | |
}, | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment