diff --git a/lib/web/auth/github/index.js b/lib/web/auth/github/index.js deleted file mode 100644 index 3a3a84c6e..000000000 --- a/lib/web/auth/github/index.js +++ /dev/null @@ -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) diff --git a/lib/web/auth/github/index.ts b/lib/web/auth/github/index.ts new file mode 100644 index 000000000..15d2b67aa --- /dev/null +++ b/lib/web/auth/github/index.ts @@ -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 + } +} diff --git a/lib/web/auth/index.ts b/lib/web/auth/index.ts index 6a5463248..bb4bf000e 100644 --- a/lib/web/auth/index.ts +++ b/lib/web/auth/index.ts @@ -5,7 +5,7 @@ import { logger } from '../../logger' import { User } from '../../models' import { FacebookMiddleware } from './facebook' import { TwitterMiddleware } from './twitter' -import github from './github' +import { GithubMiddleware } from './github' import gitlab from './gitlab' import dropbox from './dropbox' import google from './google' @@ -45,7 +45,7 @@ passport.deserializeUser(function (id: string, done) { if (config.isFacebookEnable) AuthRouter.use(FacebookMiddleware.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.isDropboxEnable) AuthRouter.use(dropbox) if (config.isGoogleEnable) AuthRouter.use(google)