From c075eb769f3380b0cd105f1caf7dc95203ea351e Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 13 Apr 2020 12:37:32 +0200 Subject: [PATCH] added auth/twitter.ts Signed-off-by: Philip Molares Signed-off-by: David Mehren --- lib/web/auth/index.ts | 4 ++-- lib/web/auth/twitter/index.js | 28 ---------------------------- lib/web/auth/twitter/index.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 30 deletions(-) delete mode 100644 lib/web/auth/twitter/index.js create mode 100644 lib/web/auth/twitter/index.ts diff --git a/lib/web/auth/index.ts b/lib/web/auth/index.ts index 782f5a207..d53363615 100644 --- a/lib/web/auth/index.ts +++ b/lib/web/auth/index.ts @@ -4,7 +4,7 @@ import { config } from '../../config' import { logger } from '../../logger' import { User } from '../../models' import facebook from './facebook' -import twitter from './twitter' +import { TwitterMiddleware } from './twitter' import github from './github' import gitlab from './gitlab' import dropbox from './dropbox' @@ -44,7 +44,7 @@ passport.deserializeUser(function (id: string, done) { }) if (config.isFacebookEnable) AuthRouter.use(facebook) -if (config.isTwitterEnable) AuthRouter.use(twitter) +if (config.isTwitterEnable) AuthRouter.use(TwitterMiddleware.getMiddleware()) if (config.isGitHubEnable) AuthRouter.use(github) if (config.isGitLabEnable) AuthRouter.use(gitlab) if (config.isDropboxEnable) AuthRouter.use(dropbox) diff --git a/lib/web/auth/twitter/index.js b/lib/web/auth/twitter/index.js deleted file mode 100644 index 56389f842..000000000 --- a/lib/web/auth/twitter/index.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict' - -const Router = require('express').Router -const passport = require('passport') -const TwitterStrategy = require('passport-twitter').Strategy - -const config = require('../../../config') -const { passportGeneralCallback } = require('../utils') - -let twitterAuth = module.exports = Router() - -passport.use(new TwitterStrategy({ - consumerKey: config.twitter.consumerKey, - consumerSecret: config.twitter.consumerSecret, - callbackURL: config.serverURL + '/auth/twitter/callback' -}, passportGeneralCallback)) - -twitterAuth.get('/auth/twitter', function (req, res, next) { - passport.authenticate('twitter')(req, res, next) -}) - -// twitter auth callback -twitterAuth.get('/auth/twitter/callback', - passport.authenticate('twitter', { - successReturnToOrRedirect: config.serverURL + '/', - failureRedirect: config.serverURL + '/' - }) -) diff --git a/lib/web/auth/twitter/index.ts b/lib/web/auth/twitter/index.ts new file mode 100644 index 000000000..84a0bbfda --- /dev/null +++ b/lib/web/auth/twitter/index.ts @@ -0,0 +1,33 @@ +import { Router } from 'express' +import passport from 'passport' +import { Strategy as TwitterStrategy } from 'passport-twitter' + +import { config } from '../../../config' +import { passportGeneralCallback } from '../utils' +import { AuthMiddleware } from '../interface' + +export const TwitterMiddleware: AuthMiddleware = { + getMiddleware (): Router { + const TwitterAuth = Router() + + passport.use(new TwitterStrategy({ + consumerKey: config.twitter.consumerKey, + consumerSecret: config.twitter.consumerSecret, + callbackURL: config.serverURL + '/auth/twitter/callback' + }, passportGeneralCallback)) + + TwitterAuth.get('/auth/twitter', function (req, res, next) { + passport.authenticate('twitter')(req, res, next) + }) + + // twitter auth callback + TwitterAuth.get('/auth/twitter/callback', + passport.authenticate('twitter', { + successReturnToOrRedirect: config.serverURL + '/', + failureRedirect: config.serverURL + '/' + }) + ) + + return TwitterAuth + } +}