Skip to content

Commit f4b415f

Browse files
authored
Revert "Only request token endpoint initially, then use a cookie to d… (#1868)
* Revert "Only request token endpoint initially, then use a cookie to determine if there is an authenticated user (#1740)" This reverts commit 0759959. * Restore js-cookie
1 parent 352c00e commit f4b415f

File tree

9 files changed

+83
-265
lines changed

9 files changed

+83
-265
lines changed

.changeset/brave-cougars-lie.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

package-lock.json

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/faustwp-core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"@types/is-number": "^7.0.1",
1919
"@types/isomorphic-fetch": "^0.0.35",
2020
"@types/jest": "^27.0.2",
21-
"@types/js-cookie": "^3.0.6",
2221
"@types/lodash": "^4.14.176",
2322
"@types/node": "^17.0.17",
23+
"@types/js-cookie": "^3.0.6",
2424
"@types/testing-library__react": "10.2.0",
2525
"concurrently": "^7.6.0",
2626
"fetch-mock": "9.11.0",
@@ -39,9 +39,9 @@
3939
"deepmerge": "^4.2.2",
4040
"fast-xml-parser": "^4.2.5",
4141
"isomorphic-fetch": "^3.0.0",
42-
"js-cookie": "^3.0.5",
4342
"js-sha256": "^0.9.0",
44-
"lodash": "^4.17.21"
43+
"lodash": "^4.17.21",
44+
"js-cookie": "^3.0.5"
4545
},
4646
"scripts": {
4747
"dev": "concurrently \"npm:watch-*\" --prefix-colors \"auto\"",

packages/faustwp-core/src/components/Toolbar/Toolbar.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { gql, useQuery } from '@apollo/client';
2-
import cookies from 'js-cookie';
32
import React, { useEffect, useMemo, useState } from 'react';
43
import { getApolloAuthClient } from '../../client.js';
54
import { useAuth } from '../../hooks/useAuth.js';
6-
import { getWpUrl } from '../../lib/getWpUrl.js';
75
import { SeedNode } from '../../queries/seedQuery.js';
86
import { hooks } from '../../wpHooks/index.js';
9-
import { ToolbarNode } from './ToolbarNode.js';
107
import { Edit } from './nodes/Edit.js';
118
import { GraphiQL } from './nodes/GraphiQL.js';
129
import { MyAccount } from './nodes/MyAccount.js';
1310
import { SiteName } from './nodes/SiteName.js';
11+
import { ToolbarNode } from './ToolbarNode.js';
1412

1513
/**
1614
* The available menu locations that nodes can be added to.
@@ -217,16 +215,7 @@ export function ToolbarAwaitUser({ seedNode }: ToolbarProps) {
217215
* Renders a Toolbar that is based on WordPress' own toolbar.
218216
*/
219217
export function Toolbar({ seedNode }: ToolbarProps) {
220-
const hasAuthenticatedUser = cookies.get(`${getWpUrl()}-has-rt`);
221-
222-
const { isAuthenticated } = useAuth({
223-
strategy: 'redirect',
224-
/**
225-
* If the hasAuthenticatedUser cookie exists and it's "0", skip
226-
* running the useAuth hook.
227-
*/
228-
skip: hasAuthenticatedUser === '0',
229-
});
218+
const { isAuthenticated } = useAuth();
230219

231220
if (isAuthenticated !== true) {
232221
return null;

packages/faustwp-core/src/server/auth/cookie.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,6 @@ export interface CookieOptions {
99
isJson?: boolean;
1010
}
1111

12-
/**
13-
* Merge cookies from current Set-Cookie header with a new cookie string.
14-
*
15-
* @param setCookieHeader Current Set-Cookie header if exists.
16-
* @param newCookie The new cookie string to be applied.
17-
* @returns A cookie string or array of cookie strings.
18-
*/
19-
export function mergeCookies(
20-
setCookieHeader: string | string[] | number | undefined,
21-
newCookie: string,
22-
) {
23-
// If there is no setCookieHeader, return the newCookie early.
24-
if (!setCookieHeader) {
25-
return newCookie;
26-
}
27-
28-
/**
29-
* If there is already a Set-Cookie header, create an array and merge
30-
* the existing ones with the new cookie.
31-
*/
32-
let newCookies: string[] = [];
33-
if (Array.isArray(setCookieHeader)) {
34-
newCookies = [...setCookieHeader];
35-
} else {
36-
newCookies = [setCookieHeader as string];
37-
}
38-
39-
newCookies = [...newCookies, newCookie];
40-
41-
return newCookies;
42-
}
43-
4412
export class Cookies {
4513
private request: IncomingMessage;
4614

@@ -90,13 +58,20 @@ export class Cookies {
9058

9159
this.cookies[key] = cookieValue;
9260

93-
const existingCookieHeader = this.response?.getHeader('Set-Cookie');
94-
95-
const newCookies = mergeCookies(
96-
existingCookieHeader,
61+
this.response?.setHeader(
62+
'Set-Cookie',
9763
cookie.serialize(key, cookieValue, serializeOptions),
9864
);
65+
}
9966

100-
this.response?.setHeader('Set-Cookie', newCookies);
67+
public removeCookie(key: string): void {
68+
delete this.cookies[key];
69+
70+
this.response?.setHeader(
71+
'Set-Cookie',
72+
cookie.serialize(key, '', {
73+
expires: new Date(0),
74+
}),
75+
);
10176
}
10277
}

packages/faustwp-core/src/server/auth/middleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export async function authorizeHandler(
3232

3333
if (!refreshToken && !code) {
3434
res.statusCode = 401;
35-
oauth.setRefreshToken(undefined);
3635
res.setHeader('Content-Type', 'application/json');
3736
res.end(JSON.stringify({ error: 'Unauthorized' }));
3837

packages/faustwp-core/src/server/auth/token.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,29 @@ export class OAuth {
2323

2424
private tokenKey: string;
2525

26-
private hasTokenKey: string;
27-
2826
constructor(cookies: Cookies) {
2927
this.cookies = cookies;
3028
this.tokenKey = `${getWpUrl()}-rt`;
31-
this.hasTokenKey = `${getWpUrl()}-has-rt`;
3229
}
3330

3431
public getRefreshToken(): string | undefined {
3532
return this.cookies.getCookie(this.tokenKey);
3633
}
3734

3835
public setRefreshToken(token?: string, expires?: number): void {
39-
let maxAge: number | undefined = 2592000;
40-
let expiresIn: Date | undefined;
41-
4236
if (!isString(token) || token.length === 0) {
43-
this.cookies.setCookie(this.tokenKey, '', {
44-
path: '/',
45-
expires: new Date(0),
46-
secure: true,
47-
httpOnly: true,
48-
});
49-
50-
this.cookies.setCookie(this.hasTokenKey, '0', {
51-
path: '/',
52-
encoded: false,
53-
maxAge,
54-
expires: expiresIn,
55-
});
56-
57-
return;
37+
this.cookies.removeCookie(this.tokenKey);
5838
}
5939

40+
let maxAge: number | undefined = 2592000;
41+
let expiresIn: Date | undefined;
42+
6043
if (isNumber(expires)) {
6144
expiresIn = new Date(expires * 1000);
6245
maxAge = undefined;
6346
}
6447

65-
this.cookies.setCookie(this.hasTokenKey, '1', {
66-
path: '/',
67-
encoded: false,
68-
maxAge,
69-
expires: expiresIn,
70-
});
71-
72-
this.cookies.setCookie(this.tokenKey, token, {
48+
this.cookies.setCookie(this.tokenKey, token as string, {
7349
expires: expiresIn,
7450
maxAge,
7551
path: '/',

packages/faustwp-core/tests/server/auth/cookie.test.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)