Раньше это работало, но потом я переместил /server.js на /server/controllers/oauth.controller.js
Раньше я мог войти на сайт с помощью Github OAuth.
Ошибка при посещении 127.0.0.1:4568
>node oauth.controller.js ✗ authentication (origin/authentication)
server is listening on 4568
ForbiddenError: Forbidden
at SendStream.error (/Users/abhimanyuaryan/portal/node_modules/send/index.js:270:31)
at SendStream.pipe (/Users/abhimanyuaryan/portal/node_modules/send/index.js:554:12)
at sendfile (/Users/abhimanyuaryan/portal/node_modules/express/lib/response.js:1099:8)
at ServerResponse.sendFile (/Users/abhimanyuaryan/portal/node_modules/express/lib/response.js:429:3)
at app.get (/Users/abhimanyuaryan/portal/server/controllers/oauth.controller.js:72:7)
at Layer.handle [as handle_request] (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/abhimanyuaryan/portal/node_modules/express/lib/router/layer.js:95:5)
at /Users/abhimanyuaryan/portal/node_modules/express/lib/router/index.js:281:22
/server/controllers/oauth.controllers.js
let Express = require('express')
let bodyParser = require('body-parser')
let session = require('express-session')
let passport = require('passport')
let GithubStrategy = require('passport-github2').Strategy
let GITHUB_CLIENT_ID = "xxxxxxxxxxxxxxxxx"
let GITHUB_CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
// flow #4
passport.serializeUser((user, done) => {
done(null, user);
})
// flow #6
passport.deserializeUser((user, done) => {
done(null, user);
})
passport.use(new GithubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:4568/auth/github/callback"
},
(accessToken, refreshToken, profile, done) => {
// console.info(profile)
/*
flow #3
Profile is the json result from github, it contains helpful information like id, username, email etc.
You can decide to use profile.id as your internal userId too.
Here you can call your database and check if the user already exist and create a new record if it doesn't
exists. We are not going to include this logic here to keep things simple but you can manage
the profile data if whatever way you want
*/
// for simplicity we are only going to return the whole profile
return done(null, profile)
}
))
let app = Express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(Express.static(__dirname + '../../client', {dotfiles:'allow'}))
app.use(session({
secret: 'top secret key',
resave: false,
saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())
// Explorting modules to external file
var exports = module.exports = {};
// function that will check if the user is authenticated
exports.isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()){
return next()
}
res.redirect('/login')
}
// you can put 'isAuthenticated function in any get/post call, here is an example'
app.get('/', exports.isAuthenticated,
(req, res) => {
res.sendFile(__dirname + '../../client/secret.html')
}
)
app.get('/login',
(req, res) => {
res.sendFile(__dirname + '../../client/login.html')
}
)
app.get('/logout',
(req, res) => {
req.logout()
res.sendFile(__dirname + '../../client/login.html')
}
)
// 'Sign in with Github' link click will arrive here and from here we call Github API with passport. authenticate
app.get('/auth/github',
//flow #1
passport.authenticate('github', {scope: [ 'user:email']}),
(req, res) => {
}
)
//github responses will arrive here and if its failure we will to /login
// if its successful we will redirect to ('/')
app.get('/auth/github/callback',
// flow #2
passport.authenticate('github', {failureRedirect: '/login'}),
(req, res) => {
// flow #5
res.redirect('/')
}
)
console.info('server is listening on 4568')
app.listen(4568)





Цитата из этого ссылка на сайт.
That error comes from the path containing .. (up parent directory) and you didn't supply the root option. Try using sendfile like so:
res.sendfile(path, {'root': '/path/to/root/directory'});
The root option should be the directory you want to serve the files from. It is intended to prevent the path from containing things like .. so a user may get the server to serve a file outside that directory.