chore: made the required changes in auth and user modules to accommodate changes made in user schema

This commit is contained in:
Balu Babu
2023-01-19 05:53:23 +05:30
parent 8fdcc5dd50
commit c38ad89cd7
6 changed files with 23 additions and 177 deletions

View File

@@ -10,6 +10,7 @@ import * as E from 'fp-ts/Either';
import { AuthErrorHandler } from './types/AuthErrorHandler';
import { AuthTokens } from './types/AuthTokens';
import { Response } from 'express';
import { DateTime } from 'luxon';
/**
* A workaround to throw an exception in an expression.
@@ -134,7 +135,6 @@ export const validateEmail = (email: string) => {
).test(email);
};
//TODO: set expiresOn to cookies
/**
* Sets and returns the cookies in the response object on successful authentication
* @param res Express Response Object
@@ -146,15 +146,30 @@ export const authCookieHandler = (
authTokens: AuthTokens,
redirect: boolean,
) => {
const currentTime = DateTime.now();
const accessTokenValidity = currentTime.plus({
millisecond: parseInt(process.env.ACCESS_TOKEN_VALIDITY),
});
const refreshTokenValidity = currentTime.plus({
millisecond: parseInt(process.env.REFRESH_TOKEN_VALIDITY),
});
console.log(process.env.ACCESS_TOKEN_VALIDITY, accessTokenValidity);
console.log(process.env.REFRESH_TOKEN_VALIDITY, refreshTokenValidity);
console.log(process.env.REDIRECT_URL);
res.cookie('access_token', authTokens.access_token, {
httpOnly: true,
secure: true,
sameSite: 'lax',
maxAge: accessTokenValidity.toMillis(),
expires: accessTokenValidity.toJSDate(),
});
res.cookie('refresh_token', authTokens.refresh_token, {
httpOnly: true,
secure: true,
sameSite: 'lax',
maxAge: refreshTokenValidity.toMillis(),
expires: refreshTokenValidity.toJSDate(),
});
if (redirect) {
res.status(HttpStatus.OK).redirect('http://localhost:3170/graphql');