Skip to content

Commit 7ca57f9

Browse files
psubram3jmorton
andcommitted
OPTIONAL COMMIT: make non-oidc auth routes' cookie setting use event instead of manually setting headers, like what is done in refresh
Co-authored-by: Pranav Subramanian <pranav.subramanian@nasa.gov> Co-authored-by: Jonathan Morton <jonathan.r.morton@nasa.gov>
1 parent 99d6ba1 commit 7ca57f9

3 files changed

Lines changed: 10 additions & 16 deletions

File tree

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { base } from '$app/paths';
21
import type { RequestHandler } from '@sveltejs/kit';
32
import { json } from '@sveltejs/kit';
43
import type { ChangeUserRoleRequestBody } from '../../../types/auth';
54

65
export const POST: RequestHandler = async event => {
76
const body: ChangeUserRoleRequestBody = await event.request.json();
87
const { role } = body;
9-
return json({ success: true }, { headers: { 'set-cookie': `activeRole=${role}; Path=${base}/` } });
8+
event.cookies.set('activeRole', role, { httpOnly: false, path: '/' });
9+
10+
return json({ success: true });
1011
};

src/routes/auth/login/+server.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { base } from '$app/paths';
21
import type { RequestHandler } from '@sveltejs/kit';
32
import { json } from '@sveltejs/kit';
43
import { jwtDecode } from 'jwt-decode';
@@ -21,10 +20,9 @@ export const POST: RequestHandler = async event => {
2120
const parsedUserToken: ParsedUserToken = jwtDecode(user.token);
2221
const defaultRole = parsedUserToken['https://hasura.io/jwt/claims']['x-hasura-default-role'];
2322

24-
return json(
25-
{ success: true, user },
26-
{ headers: { 'set-cookie': `activeRole=${defaultRole}; path=${base}/,user=${userCookie}; Path=${base}/` } },
27-
);
23+
event.cookies.set('activeRole', defaultRole, { httpOnly: false, path: '/' });
24+
event.cookies.set('user', userCookie, { httpOnly: false, path: '/' });
25+
return json({ success: true, user });
2826
} else {
2927
return json({ message, success: false });
3028
}

src/routes/auth/logout/+server.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { base } from '$app/paths';
2+
import { env } from '$env/dynamic/public';
23
import type { RequestHandler } from '@sveltejs/kit';
34
import { json } from '@sveltejs/kit';
45
import { reqGatewayForwardCookies } from '../../../utilities/requests';
5-
import { env } from '$env/dynamic/public';
66

77
export const POST: RequestHandler = async event => {
88
const invalidated =
99
env.PUBLIC_AUTH_SSO_ENABLED === 'true'
1010
? await reqGatewayForwardCookies<boolean>('/auth/logoutSSO', event.request.headers.get('cookie') ?? '', base)
1111
: true;
1212

13-
return json(
14-
{ message: 'Logout successful', success: invalidated },
15-
{
16-
headers: {
17-
'set-cookie': `activeRole=deleted; path=${base}/; expires=Thu, 01 Jan 1970 00:00:00 GMT,user=deleted; path=${base}/; expires=Thu, 01 Jan 1970 00:00:00 GMT`,
18-
},
19-
},
20-
);
13+
event.cookies.delete('activeRole', { path: '/' });
14+
event.cookies.delete('user', { path: '/' });
15+
return json({ message: 'Logout successful', success: invalidated });
2116
};

0 commit comments

Comments
 (0)