Created
October 15, 2018 14:04
-
-
Save workfel/feb32d5745fe2e3b3bdee45c3007fea4 to your computer and use it in GitHub Desktop.
nest auth middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NestMiddleware } from '@nestjs/common'; | |
import { ExpressMiddleware } from '@nestjs/common/interfaces/middlewares/express-midleware.interface'; | |
import { NextFunction } from 'express'; | |
@Injectable() | |
export class SessionAuthMiddleware implements NestMiddleware { | |
constructor() { | |
} | |
async resolve(...args: any[]): Promise<ExpressMiddleware> { | |
return async (req: any, res: Response, next: NextFunction) => { | |
if (req.query.socketId) { | |
req.session.socketId = req.query.socketId; | |
} | |
next(); | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NestMiddleware } from '@nestjs/common'; | |
import { ExpressMiddleware } from '@nestjs/common/interfaces/middlewares/express-midleware.interface'; | |
import { NextFunction } from 'express'; | |
@Injectable() | |
export class StrategyCallbackMiddleware implements NestMiddleware { | |
constructor() { | |
} | |
async resolve(...args: any[]): Promise<ExpressMiddleware> { | |
return async (req: any, res: any, next: NextFunction) => { | |
// Get the io object stored into bootstrap on main.ts | |
const io = req.app.get('io'); | |
const user = { | |
name: req.user.displayName, | |
photo: req.user.photo, | |
}; | |
// get the socketId of client and send it the user connected. | |
io.in(req.session.socketId).emit(args[0].provider, user); | |
res.end(); | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NestMiddleware } from '@nestjs/common'; | |
import { ExpressMiddleware } from '@nestjs/common/interfaces/middlewares/express-midleware.interface'; | |
import * as passport from 'passport'; | |
@Injectable() | |
export class StrategyMiddleware implements NestMiddleware { | |
constructor() { | |
} | |
async resolve(...args: any[]): Promise<ExpressMiddleware> { | |
if (args.length === 0 || !args[0].provider) { | |
throw new Error('Missing provider for authenticate oauth'); | |
} | |
// 'google', 'facebook', 'github', 'twitter', .... | |
const provider = args[0].provider; | |
return passport.authenticate(provider, { scope: ['profile', 'email'] }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment