added auth/twitter.ts

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Philip Molares 2020-04-13 12:37:32 +02:00 committed by David Mehren
parent e5961336b7
commit c075eb769f
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
3 changed files with 35 additions and 30 deletions

View file

@ -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)

View file

@ -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 + '/'
})
)

View file

@ -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
}
}