mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-12 11:19:08 +00:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import {
|
|
AuthenticateOptionsGoogle,
|
|
Profile,
|
|
Strategy,
|
|
StrategyOptions,
|
|
} from 'passport-google-oauth20';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { OauthSecrets } from 'src/utils/OauthSecrets';
|
|
import { Request } from 'express';
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
@Injectable()
|
|
export class GoogleLoginStrategy extends PassportStrategy(
|
|
Strategy,
|
|
'google-login',
|
|
) {
|
|
redirect_uri: string;
|
|
constructor(
|
|
@Inject('OAUTH_SECRETS')
|
|
private readonly oauthSecrets: OauthSecrets,
|
|
) {
|
|
const options: StrategyOptions = {
|
|
clientID: oauthSecrets['google-login'].client_id,
|
|
clientSecret: oauthSecrets['google-login'].client_secret,
|
|
callbackURL: oauthSecrets['google-login'].redirect_uri,
|
|
scope: ['email', 'profile', 'openid'],
|
|
};
|
|
const verify = (
|
|
accessToken: string,
|
|
refreshToken: string,
|
|
profile: Profile,
|
|
done,
|
|
) => {
|
|
return done(null, profile, { accessToken, refreshToken });
|
|
};
|
|
|
|
super(options, verify);
|
|
}
|
|
|
|
authenticate(req: Request, options: AuthenticateOptionsGoogle) {
|
|
const language = req.headers['dadosfera-lang'] || req.query.language;
|
|
options.state = jwt.sign({ language }, process.env.JWT_PRIVATE_KEY);
|
|
super.authenticate(req, options);
|
|
}
|
|
}
|