auth/github: Migrate to AuthMiddleware

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2020-04-13 12:55:55 +02:00
parent dd55c947cc
commit e4b2acf53e
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
3 changed files with 38 additions and 33 deletions

View file

@ -1,31 +0,0 @@
'use strict'
const Router = require('express').Router
const passport = require('passport')
const GithubStrategy = require('passport-github').Strategy
const config = require('../../../config')
const response = require('../../../response')
const { passportGeneralCallback } = require('../utils')
let githubAuth = module.exports = Router()
passport.use(new GithubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.serverURL + '/auth/github/callback'
}, passportGeneralCallback))
githubAuth.get('/auth/github', function (req, res, next) {
passport.authenticate('github')(req, res, next)
})
// github auth callback
githubAuth.get('/auth/github/callback',
passport.authenticate('github', {
successReturnToOrRedirect: config.serverURL + '/',
failureRedirect: config.serverURL + '/'
})
)
// github callback actions
githubAuth.get('/auth/github/callback/:noteId/:action', response.githubActions)

View file

@ -0,0 +1,36 @@
import { Router } from 'express'
import passport from 'passport'
import { Strategy as GithubStrategy } from 'passport-github'
import { config } from '../../../config'
import { response } from '../../../response'
import { AuthMiddleware } from '../interface'
import { passportGeneralCallback } from '../utils'
export const GithubMiddleware: AuthMiddleware = {
getMiddleware (): Router {
const githubAuth = Router()
passport.use(new GithubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.serverURL + '/auth/github/callback'
}, passportGeneralCallback))
githubAuth.get('/auth/github', function (req, res, next) {
passport.authenticate('github')(req, res, next)
})
// github auth callback
githubAuth.get('/auth/github/callback',
passport.authenticate('github', {
successReturnToOrRedirect: config.serverURL + '/',
failureRedirect: config.serverURL + '/'
})
)
// github callback actions
githubAuth.get('/auth/github/callback/:noteId/:action', response.githubActions)
return githubAuth
}
}

View file

@ -5,7 +5,7 @@ import { logger } from '../../logger'
import { User } from '../../models' import { User } from '../../models'
import { FacebookMiddleware } from './facebook' import { FacebookMiddleware } from './facebook'
import { TwitterMiddleware } from './twitter' import { TwitterMiddleware } from './twitter'
import github from './github' import { GithubMiddleware } from './github'
import gitlab from './gitlab' import gitlab from './gitlab'
import dropbox from './dropbox' import dropbox from './dropbox'
import google from './google' import google from './google'
@ -45,7 +45,7 @@ passport.deserializeUser(function (id: string, done) {
if (config.isFacebookEnable) AuthRouter.use(FacebookMiddleware.getMiddleware()) if (config.isFacebookEnable) AuthRouter.use(FacebookMiddleware.getMiddleware())
if (config.isTwitterEnable) AuthRouter.use(TwitterMiddleware.getMiddleware()) if (config.isTwitterEnable) AuthRouter.use(TwitterMiddleware.getMiddleware())
if (config.isGitHubEnable) AuthRouter.use(github) if (config.isGitHubEnable) AuthRouter.use(GithubMiddleware.getMiddleware())
if (config.isGitLabEnable) AuthRouter.use(gitlab) if (config.isGitLabEnable) AuthRouter.use(gitlab)
if (config.isDropboxEnable) AuthRouter.use(dropbox) if (config.isDropboxEnable) AuthRouter.use(dropbox)
if (config.isGoogleEnable) AuthRouter.use(google) if (config.isGoogleEnable) AuthRouter.use(google)