From 031eee651a099edb358980846fddd1b636bed10f Mon Sep 17 00:00:00 2001 From: Matthew Wright <1815200+matthewguywright@users.noreply.github.com> Date: Fri, 1 Sep 2023 09:38:53 -0500 Subject: [PATCH] MERL-1204: onLogout server action - app router (#1557) * added onLogout function to the server actions for App Router Experimental --- package-lock.json | 368 ++++++++++-------- .../experimental-app-router/src/index.tsx | 1 + .../src/server-actions/logoutAction.ts | 16 + .../tests/server-actions/onLogout.test.ts | 63 +++ 4 files changed, 292 insertions(+), 156 deletions(-) create mode 100644 packages/experimental-app-router/src/server-actions/logoutAction.ts create mode 100644 packages/experimental-app-router/tests/server-actions/onLogout.test.ts diff --git a/package-lock.json b/package-lock.json index e7fe536f1..ac0e02b05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,9 +50,9 @@ "dependencies": { "@apollo/client": "^3.8.0", "@apollo/experimental-nextjs-app-support": "^0.4.1", - "@faustwp/cli": "1.0.1", - "@faustwp/core": "1.1.0", - "@faustwp/experimental-app-router": "^0.0.1", + "@faustwp/cli": "1.1.1", + "@faustwp/core": "1.1.1", + "@faustwp/experimental-app-router": "^0.0.2", "graphql": "^16.7.1", "next": "^13.4.13", "react": "^18.2.0", @@ -286,8 +286,8 @@ "version": "0.1.0", "dependencies": { "@apollo/client": "^3.6.6", - "@faustwp/cli": "1.0.1", - "@faustwp/core": "1.1.0", + "@faustwp/cli": "1.1.1", + "@faustwp/core": "1.1.1", "@wordpress/base-styles": "^4.26.0", "@wordpress/block-library": "^7.19.0", "classnames": "^2.3.1", @@ -307,7 +307,7 @@ "version": "0.1.0", "dependencies": { "@faustjs/core": "^0.15.10", - "@faustjs/next": "^0.15.11", + "@faustjs/next": "^0.15.12", "next": "^12.2.4", "normalize.css": "^8.0.1", "react": "^17.0.2", @@ -15221,7 +15221,7 @@ }, "packages/block-editor-utils": { "name": "@faustwp/block-editor-utils", - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "dependencies": { "@wordpress/block-editor": "^12.8.0", @@ -18261,13 +18261,13 @@ }, "packages/experimental-app-router": { "name": "@faustwp/experimental-app-router", - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "devDependencies": { "@apollo/client": "^3.8.0", "@apollo/experimental-nextjs-app-support": "^0.4.1", - "@faustwp/cli": "^1.0.1", - "@faustwp/core": "^1.1.0", + "@faustwp/cli": "^1.1.1", + "@faustwp/core": "^1.1.1", "@testing-library/jest-dom": "^5.17.0", "@types/node": "^20.4.6", "concurrently": "^8.2.0", @@ -18288,8 +18288,8 @@ "peerDependencies": { "@apollo/client": ">=3.8.0", "@apollo/experimental-nextjs-app-support": ">=0.3.2", - "@faustwp/cli": ">=1.0.0", - "@faustwp/core": ">=1.0.0", + "@faustwp/cli": ">=1.1.1", + "@faustwp/core": ">=1.1.1", "next": ">=12.1.6", "react": ">=17.0.2", "react-dom": ">=17.0.2" @@ -18585,6 +18585,21 @@ "dev": true, "license": "MIT" }, + "packages/experimental-app-router/node_modules/@next/swc-darwin-arm64": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.13.tgz", + "integrity": "sha512-ZptVhHjzUuivnXMNCJ6lER33HN7lC+rZ01z+PM10Ows21NHFYMvGhi5iXkGtBDk6VmtzsbqnAjnx4Oz5um0FjA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, "packages/experimental-app-router/node_modules/@next/swc-darwin-x64": { "version": "13.4.13", "cpu": [ @@ -18600,6 +18615,111 @@ "node": ">= 10" } }, + "packages/experimental-app-router/node_modules/@next/swc-linux-arm64-gnu": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.13.tgz", + "integrity": "sha512-xEHUqC8eqR5DHe8SOmMnDU1K3ggrJ28uIKltrQAwqFSSSmzjnN/XMocZkcVhuncuxYrpbri0iMQstRyRVdQVWg==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-linux-arm64-musl": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.13.tgz", + "integrity": "sha512-sNf3MnLAm8rquSSAoeD9nVcdaDeRYOeey4stOWOyWIgbBDtP+C93amSgH/LPTDoUV7gNiU6f+ghepTjTjRgIUQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-linux-x64-gnu": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.13.tgz", + "integrity": "sha512-WhcRaJJSHyx9OWmKjjz+OWHumiPZWRqmM/09Bt7Up4UqUJFFhGExeztR4trtv3rflvULatu9IH/nTV8fUUgaMA==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-linux-x64-musl": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.13.tgz", + "integrity": "sha512-+Y4LLhOWWZQIDKVwr2R17lq2KSN0F1c30QVgGIWfnjjHpH8nrIWHEndhqYU+iFuW8It78CiJjQKTw4f51HD7jA==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-win32-arm64-msvc": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.13.tgz", + "integrity": "sha512-rWurdOR20uxjfqd1X9vDAgv0Jb26KjyL8akF9CBeFqX8rVaBAnW/Wf6A2gYEwyYY4Bai3T7p1kro6DFrsvBAAw==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-win32-ia32-msvc": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.13.tgz", + "integrity": "sha512-E8bSPwRuY5ibJ3CzLQmJEt8qaWrPYuUTwnrwygPUEWoLzD5YRx9SD37oXRdU81TgGwDzCxpl7z5Nqlfk50xAog==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-win32-x64-msvc": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.13.tgz", + "integrity": "sha512-4KlyC6jWRubPnppgfYsNTPeWfGCxtWLh5vaOAW/kdzAk9widqho8Qb5S4K2vHmal1tsURi7Onk2MMCV1phvyqA==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, "packages/experimental-app-router/node_modules/@sinonjs/commons": { "version": "3.0.0", "dev": true, @@ -20177,7 +20297,7 @@ }, "packages/faustwp-cli": { "name": "@faustwp/cli", - "version": "1.0.1", + "version": "1.1.1", "license": "MIT", "dependencies": { "chalk": "^4.1.2", @@ -20277,7 +20397,7 @@ }, "packages/faustwp-core": { "name": "@faustwp/core", - "version": "1.1.0", + "version": "1.1.1", "license": "MIT", "dependencies": { "@wordpress/hooks": "^3.14.0", @@ -20380,7 +20500,7 @@ }, "packages/next": { "name": "@faustjs/next", - "version": "0.15.11", + "version": "0.15.12", "license": "MIT", "dependencies": { "@faustjs/core": "^0.15.10", @@ -20455,126 +20575,6 @@ "plugins/faustwp": { "name": "@faustwp/wordpress-plugin", "version": "1.0.3" - }, - "packages/experimental-app-router/node_modules/@next/swc-darwin-arm64": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.13.tgz", - "integrity": "sha512-ZptVhHjzUuivnXMNCJ6lER33HN7lC+rZ01z+PM10Ows21NHFYMvGhi5iXkGtBDk6VmtzsbqnAjnx4Oz5um0FjA==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-linux-arm64-gnu": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.13.tgz", - "integrity": "sha512-xEHUqC8eqR5DHe8SOmMnDU1K3ggrJ28uIKltrQAwqFSSSmzjnN/XMocZkcVhuncuxYrpbri0iMQstRyRVdQVWg==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-linux-arm64-musl": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.13.tgz", - "integrity": "sha512-sNf3MnLAm8rquSSAoeD9nVcdaDeRYOeey4stOWOyWIgbBDtP+C93amSgH/LPTDoUV7gNiU6f+ghepTjTjRgIUQ==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-linux-x64-gnu": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.13.tgz", - "integrity": "sha512-WhcRaJJSHyx9OWmKjjz+OWHumiPZWRqmM/09Bt7Up4UqUJFFhGExeztR4trtv3rflvULatu9IH/nTV8fUUgaMA==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-linux-x64-musl": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.13.tgz", - "integrity": "sha512-+Y4LLhOWWZQIDKVwr2R17lq2KSN0F1c30QVgGIWfnjjHpH8nrIWHEndhqYU+iFuW8It78CiJjQKTw4f51HD7jA==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-win32-arm64-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.13.tgz", - "integrity": "sha512-rWurdOR20uxjfqd1X9vDAgv0Jb26KjyL8akF9CBeFqX8rVaBAnW/Wf6A2gYEwyYY4Bai3T7p1kro6DFrsvBAAw==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-win32-ia32-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.13.tgz", - "integrity": "sha512-E8bSPwRuY5ibJ3CzLQmJEt8qaWrPYuUTwnrwygPUEWoLzD5YRx9SD37oXRdU81TgGwDzCxpl7z5Nqlfk50xAog==", - "cpu": [ - "ia32" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } - }, - "packages/experimental-app-router/node_modules/@next/swc-win32-x64-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.13.tgz", - "integrity": "sha512-4KlyC6jWRubPnppgfYsNTPeWfGCxtWLh5vaOAW/kdzAk9widqho8Qb5S4K2vHmal1tsURi7Onk2MMCV1phvyqA==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } } }, "dependencies": { @@ -20664,7 +20664,7 @@ "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", "json5": "^2.2.2", - "semver": "~7.5.2" + "semver": "^6.3.0" } }, "@babel/generator": { @@ -20696,7 +20696,7 @@ "@babel/helper-validator-option": "^7.22.5", "browserslist": "^4.21.3", "lru-cache": "^5.1.1", - "semver": "~7.5.2" + "semver": "^6.3.0" }, "dependencies": { "lru-cache": { @@ -20971,7 +20971,7 @@ "outdent": "^0.5.0", "prettier": "^2.7.1", "resolve-from": "^5.0.0", - "semver": "~7.5.2" + "semver": "^7.5.3" } }, "@changesets/assemble-release-plan": { @@ -20983,7 +20983,7 @@ "@changesets/get-dependents-graph": "^1.3.6", "@changesets/types": "^5.2.1", "@manypkg/get-packages": "^1.1.3", - "semver": "~7.5.2" + "semver": "^7.5.3" } }, "@changesets/changelog-git": { @@ -21347,7 +21347,7 @@ "version": "file:examples/next/getting-started", "requires": { "@faustjs/core": "^0.15.10", - "@faustjs/next": "^0.15.11", + "@faustjs/next": "^0.15.12", "@gqty/cli": "^3.1.0", "@types/node": "^17.0.17", "@types/react": "^17.0.34", @@ -21401,9 +21401,9 @@ "requires": { "@apollo/client": "^3.8.0", "@apollo/experimental-nextjs-app-support": "^0.4.1", - "@faustwp/cli": "1.0.1", - "@faustwp/core": "1.1.0", - "@faustwp/experimental-app-router": "^0.0.1", + "@faustwp/cli": "1.1.1", + "@faustwp/core": "1.1.1", + "@faustwp/experimental-app-router": "^0.0.2", "graphql": "^16.7.1", "next": "^13.4.13", "react": "^18.2.0", @@ -23760,8 +23760,8 @@ "requires": { "@apollo/client": "^3.8.0", "@apollo/experimental-nextjs-app-support": "^0.4.1", - "@faustwp/cli": "^1.0.1", - "@faustwp/core": "^1.1.0", + "@faustwp/cli": "^1.1.1", + "@faustwp/core": "^1.1.1", "@testing-library/jest-dom": "^5.17.0", "@types/node": "^20.4.6", "concurrently": "^8.2.0", @@ -23977,11 +23977,67 @@ "version": "13.4.13", "dev": true }, + "@next/swc-darwin-arm64": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.13.tgz", + "integrity": "sha512-ZptVhHjzUuivnXMNCJ6lER33HN7lC+rZ01z+PM10Ows21NHFYMvGhi5iXkGtBDk6VmtzsbqnAjnx4Oz5um0FjA==", + "dev": true, + "optional": true + }, "@next/swc-darwin-x64": { "version": "13.4.13", "dev": true, "optional": true }, + "@next/swc-linux-arm64-gnu": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.13.tgz", + "integrity": "sha512-xEHUqC8eqR5DHe8SOmMnDU1K3ggrJ28uIKltrQAwqFSSSmzjnN/XMocZkcVhuncuxYrpbri0iMQstRyRVdQVWg==", + "dev": true, + "optional": true + }, + "@next/swc-linux-arm64-musl": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.13.tgz", + "integrity": "sha512-sNf3MnLAm8rquSSAoeD9nVcdaDeRYOeey4stOWOyWIgbBDtP+C93amSgH/LPTDoUV7gNiU6f+ghepTjTjRgIUQ==", + "dev": true, + "optional": true + }, + "@next/swc-linux-x64-gnu": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.13.tgz", + "integrity": "sha512-WhcRaJJSHyx9OWmKjjz+OWHumiPZWRqmM/09Bt7Up4UqUJFFhGExeztR4trtv3rflvULatu9IH/nTV8fUUgaMA==", + "dev": true, + "optional": true + }, + "@next/swc-linux-x64-musl": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.13.tgz", + "integrity": "sha512-+Y4LLhOWWZQIDKVwr2R17lq2KSN0F1c30QVgGIWfnjjHpH8nrIWHEndhqYU+iFuW8It78CiJjQKTw4f51HD7jA==", + "dev": true, + "optional": true + }, + "@next/swc-win32-arm64-msvc": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.13.tgz", + "integrity": "sha512-rWurdOR20uxjfqd1X9vDAgv0Jb26KjyL8akF9CBeFqX8rVaBAnW/Wf6A2gYEwyYY4Bai3T7p1kro6DFrsvBAAw==", + "dev": true, + "optional": true + }, + "@next/swc-win32-ia32-msvc": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.13.tgz", + "integrity": "sha512-E8bSPwRuY5ibJ3CzLQmJEt8qaWrPYuUTwnrwygPUEWoLzD5YRx9SD37oXRdU81TgGwDzCxpl7z5Nqlfk50xAog==", + "dev": true, + "optional": true + }, + "@next/swc-win32-x64-msvc": { + "version": "13.4.13", + "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.13.tgz", + "integrity": "sha512-4KlyC6jWRubPnppgfYsNTPeWfGCxtWLh5vaOAW/kdzAk9widqho8Qb5S4K2vHmal1tsURi7Onk2MMCV1phvyqA==", + "dev": true, + "optional": true + }, "@sinonjs/commons": { "version": "3.0.0", "dev": true, @@ -25026,8 +25082,8 @@ "version": "file:examples/next/faustwp-getting-started", "requires": { "@apollo/client": "^3.6.6", - "@faustwp/cli": "1.0.1", - "@faustwp/core": "1.1.0", + "@faustwp/cli": "1.1.1", + "@faustwp/core": "1.1.1", "@wordpress/base-styles": "^4.26.0", "@wordpress/block-library": "^7.19.0", "classnames": "^2.3.1", @@ -25478,7 +25534,7 @@ "jest-util": "^29.6.2", "natural-compare": "^1.4.0", "pretty-format": "^29.6.2", - "semver": "~7.5.2" + "semver": "^7.5.3" } }, "jest-util": { @@ -29112,7 +29168,7 @@ "esprima": "^4.0.1", "estraverse": "^5.2.0", "esutils": "^2.0.2", - "optionator": "0.9.3", + "optionator": "^0.8.1", "source-map": "~0.6.1" } }, @@ -30377,7 +30433,7 @@ "@babel/parser": "^7.14.7", "@istanbuljs/schema": "^0.1.2", "istanbul-lib-coverage": "^3.2.0", - "semver": "~7.5.2" + "semver": "^6.3.0" } }, "istanbul-lib-report": { @@ -31226,7 +31282,7 @@ "jest-util": "^27.5.1", "natural-compare": "^1.4.0", "pretty-format": "^27.5.1", - "semver": "~7.5.2" + "semver": "^7.3.2" }, "dependencies": { "ansi-styles": { @@ -31663,7 +31719,7 @@ "version": "3.1.0", "dev": true, "requires": { - "semver": "~7.5.2" + "semver": "^6.0.0" } }, "make-error": { @@ -31842,7 +31898,7 @@ "node-abi": { "version": "3.24.0", "requires": { - "semver": "~7.5.2" + "semver": "^7.3.5" } }, "node-addon-api": { @@ -32680,7 +32736,7 @@ "detect-libc": "^2.0.1", "node-addon-api": "^5.0.0", "prebuild-install": "^7.1.1", - "semver": "~7.5.2", + "semver": "^7.3.7", "simple-get": "^4.0.1", "tar-fs": "^2.1.1", "tunnel-agent": "^0.6.0" @@ -33382,7 +33438,7 @@ "json5": "2.x", "lodash.memoize": "4.x", "make-error": "1.x", - "semver": "~7.5.2", + "semver": "7.x", "yargs-parser": "20.x" }, "dependencies": { @@ -33399,7 +33455,7 @@ "chalk": "^4.1.0", "enhanced-resolve": "^5.0.0", "micromatch": "^4.0.0", - "semver": "~7.5.2" + "semver": "^7.3.4" }, "dependencies": { "ansi-styles": { diff --git a/packages/experimental-app-router/src/index.tsx b/packages/experimental-app-router/src/index.tsx index 364e3213a..6abdb31d3 100644 --- a/packages/experimental-app-router/src/index.tsx +++ b/packages/experimental-app-router/src/index.tsx @@ -1,3 +1,4 @@ export { getClient, getAuthClient } from './client.js'; export { faustRouteHandler } from './server/routeHandler/index.js'; export { fetchAccessToken } from './server/auth/fetchAccessToken.js'; +export { onLogout } from './server-actions/logoutAction.js'; diff --git a/packages/experimental-app-router/src/server-actions/logoutAction.ts b/packages/experimental-app-router/src/server-actions/logoutAction.ts new file mode 100644 index 000000000..f64b4bb7f --- /dev/null +++ b/packages/experimental-app-router/src/server-actions/logoutAction.ts @@ -0,0 +1,16 @@ +import { cookies } from 'next/headers.js'; +import { getWpUrl } from '@faustwp/core/dist/cjs/lib/getWpUrl.js'; + +export async function onLogout() { + 'use server'; + + const wpCookieName = `${getWpUrl()}-rt`; + const cookieStore = cookies(); + const wpCookie = cookieStore.get(wpCookieName); + + if (wpCookie?.name) { + cookieStore.delete(wpCookieName); + return true; + } + return false; +} diff --git a/packages/experimental-app-router/tests/server-actions/onLogout.test.ts b/packages/experimental-app-router/tests/server-actions/onLogout.test.ts new file mode 100644 index 000000000..edf23a5e5 --- /dev/null +++ b/packages/experimental-app-router/tests/server-actions/onLogout.test.ts @@ -0,0 +1,63 @@ +import 'isomorphic-fetch'; +import { onLogout } from '../../src/server-actions/logoutAction.js'; +import fetchMock from 'fetch-mock'; +import { cookies } from 'next/headers.js'; + +// // https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-893763840 +const nextHeaders = { cookies }; + +jest.mock('next/headers.js'); + +describe('onLogout', () => { + const envBackup = process.env; + + beforeEach(() => { + process.env = { ...envBackup }; + }); + + afterEach(() => { + jest.clearAllMocks(); + fetchMock.restore(); + }); + + afterAll(() => { + process.env = envBackup; + }); + + it('return true if cookie exists and gets deleted', async () => { + const cookiesSpy = jest.spyOn(nextHeaders, 'cookies'); + const deleteSpy = jest.fn(); + + // No refresh token + cookiesSpy.mockReturnValue({ + get() { + return { + value: 'values', + name: 'cookieName', + }; + }, + delete: deleteSpy, + } as any); + + const loggedOut = await onLogout(); + + expect(deleteSpy).toHaveBeenCalled(); + expect(loggedOut).toBeTruthy(); + }); + + it("return false if cookie doesn't exist", async () => { + const cookiesSpy = jest.spyOn(nextHeaders, 'cookies'); + + // No refresh token + cookiesSpy.mockReturnValue({ + get() { + return undefined; + }, + delete() {}, + } as any); + + const loggedOut = await onLogout(); + + expect(loggedOut).toBeFalsy(); + }); +});