Skip to content

Commit 73aa6cd

Browse files
committed
Refactor auth cookies
1 parent cb38688 commit 73aa6cd

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

packages/server/api/src/app/authentication/authentication.controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import { userService } from '../user/user-service';
1818
import { analyticsAuthenticationService } from './analytics-authentication-service';
1919
import { authenticationService } from './basic/authentication-service';
2020
import {
21-
removeAuthCookiesAndReply,
22-
setAuthCookiesAndReply,
21+
removeAuthCookies,
22+
setAuthCookies,
2323
} from './context/authentication-cookies';
2424

2525
const edition = system.getEdition();
@@ -55,7 +55,7 @@ export const authenticationController: FastifyPluginAsyncTypebox = async (
5555
},
5656
},
5757
async (request, reply) => {
58-
return removeAuthCookiesAndReply(reply);
58+
return removeAuthCookies(reply).send('Cookies removed');
5959
},
6060
);
6161

@@ -107,7 +107,7 @@ const signUpRoute = async (request: any, reply: any) => {
107107
provider: Provider.EMAIL,
108108
});
109109

110-
return setAuthCookiesAndReply(reply, signUpResponse);
110+
return setAuthCookies(reply, signUpResponse).send(signUpResponse);
111111
};
112112

113113
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -124,7 +124,7 @@ const signInRoute = async (request: any, reply: any) => {
124124
provider: Provider.EMAIL,
125125
});
126126

127-
return setAuthCookiesAndReply(reply, signInResponse);
127+
return setAuthCookies(reply, signInResponse).send(signInResponse);
128128
};
129129

130130
const rateLimitOptions: RateLimitOptions = {

packages/server/api/src/app/authentication/context/authentication-cookies.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import { FastifyReply } from 'fastify';
88
import { jwtDecode } from 'jwt-decode';
99
import { getSubDomain } from '../../helper/sub-domain';
1010

11-
export function setAuthCookiesAndReply(
11+
export function setAuthCookies(
1212
reply: FastifyReply,
1313
response: AuthenticationResponse,
14+
expireInSeconds?: number,
1415
): FastifyReply {
15-
const date = jwtDecode<{ exp: number }>(response.tablesRefreshToken);
16-
const cookieExpiryDate = new Date(date.exp * 1000);
16+
let cookieExpiryDate: Date;
17+
if (expireInSeconds) {
18+
cookieExpiryDate = new Date(expireInSeconds * 1000);
19+
} else {
20+
const date = jwtDecode<{ exp: number }>(response.tablesRefreshToken);
21+
cookieExpiryDate = new Date(date.exp * 1000);
22+
}
1723

1824
return reply
1925
.setCookie('jwt_token', response.tablesRefreshToken, {
@@ -37,11 +43,10 @@ export function setAuthCookiesAndReply(
3743
httpOnly: false,
3844
expires: cookieExpiryDate,
3945
sameSite: 'lax',
40-
})
41-
.send(response);
46+
});
4247
}
4348

44-
export function removeAuthCookiesAndReply(reply: FastifyReply): FastifyReply {
49+
export function removeAuthCookies(reply: FastifyReply): FastifyReply {
4550
return reply
4651
.clearCookie('jwt_token', {
4752
domain: getOpenOpsSubDomain(),
@@ -53,8 +58,7 @@ export function removeAuthCookiesAndReply(reply: FastifyReply): FastifyReply {
5358
.clearCookie('baserow_group_id', {
5459
domain: getOpenOpsSubDomain(),
5560
path: '/',
56-
})
57-
.send('Cookies removed');
61+
});
5862
}
5963

6064
function getOpenOpsSubDomain(): string {

0 commit comments

Comments
 (0)