-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { describe, test, expect } from "vitest" | ||
import { getStateCookie, setStateCookie } from "../stateCookie" | ||
|
||
describe("setStateCookie", () => { | ||
test("throws errors on empty string", () => { | ||
expect(() => setStateCookie("")) | ||
.toThrow("state value is required") | ||
}) | ||
test("sets cookie with prefix app.txs", () => { | ||
const state = setStateCookie("code_verifier_secret") | ||
expect(document.cookie) | ||
.toMatch(`app.txs.${state}=code_verifier_secret`) | ||
}) | ||
test("sets cookie with latest value", () => { | ||
const state = setStateCookie("code_verifier_secret") | ||
expect(document.cookie) | ||
.toMatch(`app.txs.${state}=code_verifier_secret`) | ||
}) | ||
test("returns state", () => { | ||
const state = setStateCookie("code_verifier_secret") | ||
expect(state).toMatch(/[\w-]+/) | ||
}) | ||
}) | ||
|
||
describe("getStateCookie", () => { | ||
test("gets cookie", () => { | ||
const state = crypto.randomUUID() | ||
const random = Math.random().toString(36).substring(7) | ||
document.cookie = `app.txs.${state}=${random}` | ||
expect(getStateCookie(state)).toBe(random) | ||
}) | ||
|
||
test("gets none", () => { | ||
expect(getStateCookie('nothere')).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
import { saveCookie, getCookie } from './cookie' | ||
|
||
export const setStateCookie = (value: string) => { | ||
if (!value) throw new Error('state value is required') | ||
|
||
const state = crypto.randomUUID() | ||
saveCookie(`app.txs.${state}`, value, 30) | ||
|
||
return state | ||
} | ||
|
||
export const getStateCookie = (state: string) => { | ||
return getCookie(`app.txs.${state}`) | ||
} |