fix: added scope for github strategy in auth module
This commit is contained in:
@@ -77,4 +77,23 @@ export class AuthController {
|
||||
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
||||
authCookieHandler(res, authTokens.right, true);
|
||||
}
|
||||
|
||||
@Get('microsoft')
|
||||
@UseGuards(AuthGuard('microsoft'))
|
||||
async microsoftAuth(@Request() req) {}
|
||||
|
||||
@Get('microsoft/callback')
|
||||
@UseGuards(AuthGuard('microsoft'))
|
||||
async microsoftAuthRedirect(@Request() req, @Res() res) {
|
||||
const authTokens = await this.authService.generateAuthTokens(req.user.id);
|
||||
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
||||
authCookieHandler(res, authTokens.right, true);
|
||||
}
|
||||
|
||||
@Get('logout')
|
||||
async logout(@Res() res: Response) {
|
||||
res.clearCookie('access_token');
|
||||
res.clearCookie('refresh_token');
|
||||
return res.redirect(process.env.REDIRECT_URL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import { JwtStrategy } from './strategies/jwt.strategy';
|
||||
import { RTJwtStrategy } from './strategies/rt-jwt.strategy';
|
||||
import { GoogleStrategy } from './strategies/google.strategy';
|
||||
import { GithubStrategy } from './strategies/github.strategy';
|
||||
import { MicrosoftStrategy } from './strategies/microsoft.strategy';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -27,6 +28,7 @@ import { GithubStrategy } from './strategies/github.strategy';
|
||||
RTJwtStrategy,
|
||||
GoogleStrategy,
|
||||
GithubStrategy,
|
||||
MicrosoftStrategy,
|
||||
],
|
||||
controllers: [AuthController],
|
||||
})
|
||||
|
||||
@@ -15,10 +15,12 @@ export class GithubStrategy extends PassportStrategy(Strategy) {
|
||||
clientID: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
callbackURL: process.env.GITHUB_CALLBACK_URL,
|
||||
scope: [process.env.GITHUB_SCOPE],
|
||||
});
|
||||
}
|
||||
|
||||
async validate(accessToken, refreshToken, profile, done): Promise<any> {
|
||||
async validate(accessToken, refreshToken, profile, done) {
|
||||
console.dir(profile);
|
||||
const user = await this.usersService.findUserByEmail(
|
||||
profile.emails[0].value,
|
||||
);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Strategy } from 'passport-microsoft';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthService } from '../auth.service';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import * as O from 'fp-ts/Option';
|
||||
|
||||
@Injectable()
|
||||
export class MicrosoftStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(
|
||||
private authService: AuthService,
|
||||
private usersService: UserService,
|
||||
) {
|
||||
super({
|
||||
clientID: process.env.MICROSOFT_CLIENT_ID,
|
||||
clientSecret: process.env.MICROSOFT_CLIENT_SECRET,
|
||||
callbackURL: process.env.MICROSOFT_CALLBACK_URL,
|
||||
scope: process.env.MICROSOFT_SCOPE,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(accessToken, refreshToken, profile, done) {
|
||||
const user = await this.usersService.findUserByEmail(
|
||||
profile.emails[0].value,
|
||||
);
|
||||
|
||||
if (O.isNone(user)) {
|
||||
const createdUser = await this.usersService.createUserSSO(
|
||||
accessToken,
|
||||
refreshToken,
|
||||
profile,
|
||||
);
|
||||
return createdUser;
|
||||
}
|
||||
|
||||
// Check to see if entry for microsoft is present in the Account table for this user
|
||||
const providerAccountExists =
|
||||
await this.authService.checkIfProviderAccountExists(user.value, profile);
|
||||
|
||||
if (O.isNone(providerAccountExists))
|
||||
await this.usersService.createProviderAccount(
|
||||
user.value,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
profile,
|
||||
);
|
||||
|
||||
return user.value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user