chore: replaced hardcoded values with env variables in app.module.ts, main.ts and utils.ts
This commit is contained in:
@@ -8,6 +8,10 @@ import { AuthModule } from './auth/auth.module';
|
|||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
GraphQLModule.forRoot<ApolloDriverConfig>({
|
GraphQLModule.forRoot<ApolloDriverConfig>({
|
||||||
|
cors: process.env.PRODUCTION !== 'true' && {
|
||||||
|
origin: ['http://localhost:3170', 'http://localhost:3000'],
|
||||||
|
credentials: true,
|
||||||
|
},
|
||||||
playground: process.env.PRODUCTION !== 'true',
|
playground: process.env.PRODUCTION !== 'true',
|
||||||
debug: process.env.PRODUCTION !== 'true',
|
debug: process.env.PRODUCTION !== 'true',
|
||||||
autoSchemaFile: true,
|
autoSchemaFile: true,
|
||||||
|
|||||||
@@ -63,8 +63,6 @@ export class AuthController {
|
|||||||
async googleAuthRedirect(@Request() req, @Res() res) {
|
async googleAuthRedirect(@Request() req, @Res() res) {
|
||||||
const authTokens = await this.authService.generateAuthTokens(req.user.uid);
|
const authTokens = await this.authService.generateAuthTokens(req.user.uid);
|
||||||
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
if (E.isLeft(authTokens)) throwHTTPErr(authTokens.left);
|
||||||
console.log('google', authTokens);
|
|
||||||
|
|
||||||
authCookieHandler(res, authTokens.right, true);
|
authCookieHandler(res, authTokens.right, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|||||||
super({
|
super({
|
||||||
jwtFromRequest: ExtractJwt.fromExtractors([
|
jwtFromRequest: ExtractJwt.fromExtractors([
|
||||||
(request: Request) => {
|
(request: Request) => {
|
||||||
console.log('here1', request.cookies);
|
|
||||||
|
|
||||||
const ATCookie = request.cookies['access_token'];
|
const ATCookie = request.cookies['access_token'];
|
||||||
if (!ATCookie) {
|
if (!ATCookie) {
|
||||||
throw new ForbiddenException(COOKIES_NOT_FOUND);
|
throw new ForbiddenException(COOKIES_NOT_FOUND);
|
||||||
@@ -37,7 +35,6 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|||||||
|
|
||||||
async validate(payload: AccessTokenPayload) {
|
async validate(payload: AccessTokenPayload) {
|
||||||
if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN);
|
if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN);
|
||||||
console.log('here', payload);
|
|
||||||
|
|
||||||
const user = await this.usersService.findUserById(payload.sub);
|
const user = await this.usersService.findUserById(payload.sub);
|
||||||
if (O.isNone(user)) {
|
if (O.isNone(user)) {
|
||||||
|
|||||||
@@ -19,8 +19,10 @@ async function bootstrap() {
|
|||||||
|
|
||||||
if (process.env.PRODUCTION === 'false') {
|
if (process.env.PRODUCTION === 'false') {
|
||||||
console.log('Enabling CORS with development settings');
|
console.log('Enabling CORS with development settings');
|
||||||
|
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
origin: true,
|
origin: process.env.WHITELISTED_ORIGINS.split(','),
|
||||||
|
credentials: true,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log('Enabling CORS with production settings');
|
console.log('Enabling CORS with production settings');
|
||||||
|
|||||||
@@ -146,17 +146,31 @@ export const authCookieHandler = (
|
|||||||
authTokens: AuthTokens,
|
authTokens: AuthTokens,
|
||||||
redirect: boolean,
|
redirect: boolean,
|
||||||
) => {
|
) => {
|
||||||
|
const currentTime = DateTime.now();
|
||||||
|
const accessTokenValidity = currentTime
|
||||||
|
.plus({
|
||||||
|
milliseconds: parseInt(process.env.ACCESS_TOKEN_VALIDITY),
|
||||||
|
})
|
||||||
|
.toMillis();
|
||||||
|
const refreshTokenValidity = currentTime
|
||||||
|
.plus({
|
||||||
|
milliseconds: parseInt(process.env.REFRESH_TOKEN_VALIDITY),
|
||||||
|
})
|
||||||
|
.toMillis();
|
||||||
|
|
||||||
res.cookie('access_token', authTokens.access_token, {
|
res.cookie('access_token', authTokens.access_token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: true,
|
secure: true,
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
|
maxAge: accessTokenValidity,
|
||||||
});
|
});
|
||||||
res.cookie('refresh_token', authTokens.refresh_token, {
|
res.cookie('refresh_token', authTokens.refresh_token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: true,
|
secure: true,
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
|
maxAge: refreshTokenValidity,
|
||||||
});
|
});
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
res.status(HttpStatus.OK).redirect('http://localhost:3170/graphql');
|
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);
|
||||||
} else res.status(HttpStatus.OK).send();
|
} else res.status(HttpStatus.OK).send();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user