]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/oauth.js
OAuth/User models refractoring -> use mongoose api
[github/Chocobozzz/PeerTube.git] / server / middlewares / oauth.js
CommitLineData
9457bf88
C
1'use strict'
2
3const OAuthServer = require('express-oauth-server')
4
0c1cbbfe
C
5const logger = require('../helpers/logger')
6
7const oAuthServer = new OAuthServer({
69b0a27c 8 model: require('../lib/oauth-model')
9457bf88
C
9})
10
69b0a27c 11const oAuth = {
0c1cbbfe
C
12 authenticate: authenticate,
13 token: token
14}
15
16function authenticate (req, res, next) {
17 oAuthServer.authenticate()(req, res, function (err) {
18 if (err) {
19 logger.error('Cannot authenticate.', { error: err })
20 return res.sendStatus(500)
21 }
22
23 if (res.statusCode === 401 || res.statusCode === 400) return res.end()
24
25 return next()
26 })
27}
28
29function token (req, res, next) {
30 return oAuthServer.token()(req, res, next)
31}
32
9457bf88
C
33// ---------------------------------------------------------------------------
34
69b0a27c 35module.exports = oAuth