ForbiddenError:禁止发送SendStream.error(../../portal/node_modules/send/index.js:270:31)

时间:2018-04-10 12:18:15

标签: node.js express github oauth-2.0 passport.js

之前有效,但之后我将/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.log(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.log('server is listening on 4568')
app.listen(4568)

直接Hirarchy enter image description here

1 个答案:

答案 0 :(得分:0)

引用此链接link

  

该错误来自包含..(上行父目录)的路径,并且您没有提供root选项。尝试使用sendfile,如下所示:

res.sendfile(path, {'root': '/path/to/root/directory'});
  

root选项应该是您要从中提供文件的目录。它旨在防止路径包含诸如...之类的内容,因此用户可以让服务器提供该目录之外的文件。