Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added forget method to remove the data from session #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions playwright/tests/sessions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ test('logs in a user after several mistakes', async ({ page }) => {
await page.locator('#email').fill('[email protected]')
await page.locator('#password').fill('correct')
await page.locator('#login-button').click()

// expect failed-login-attempts to not exist in the DOM
await expect(page.locator('#failed-login-attempts')).toBeHidden()
await expect(page.locator('#message')).toContainText('Login Successful')

await page.goto(runtimeCommand().server_url + '/')
Expand Down
6 changes: 6 additions & 0 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export class Session<T = any> {
this.cache._data[key as string] = entry
}

forget<K extends keyof T>(key: K) {
const entry = this.cache._data[key as string]
if (!entry) return
delete this.cache._data[key as string]
}

flash<K extends keyof T>(key: K, value: T[K]) {
const entry: SessionDataEntry<T> = {
value: value as T,
Expand Down
2 changes: 1 addition & 1 deletion test/bun/src/test_cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ session_routes.post('/login', async (c) => {
if (password === 'correct') {
c.set('session_key_rotation', true)
session.set('email', email)
session.set('failed-login-attempts', null)
session.forget('failed-login-attempts')
session.flash('message', 'Login Successful')
} else {
const failedLoginAttempts = (session.get('failed-login-attempts') || 0) as number
Expand Down
2 changes: 1 addition & 1 deletion test/cloudflare_pages/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ session_routes.post('/login', async (c) => {
if (password === 'correct') {
c.set('session_key_rotation', true)
session.set('email', email)
session.set('failed-login-attempts', null)
session.forget('failed-login-attempts')
session.flash('message', 'Login Successful')
} else {
const failedLoginAttempts = (session.get('failed-login-attempts') || 0) as number
Expand Down
2 changes: 1 addition & 1 deletion test/cloudflare_workers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ session_routes.post('/login', async (c) => {
if (password === 'correct') {
c.set('session_key_rotation', true)
session.set('email', email)
session.set('failed-login-attempts', null)
session.forget('failed-login-attempts')
session.flash('message', 'Login Successful')
} else {
const failedLoginAttempts = (session.get('failed-login-attempts') || 0) as number
Expand Down
2 changes: 1 addition & 1 deletion test/deno/server_deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ session_routes.post('/login', async (c) => {
if (password === 'correct') {
c.set('session_key_rotation', true)
session.set('email', email as string)
session.set('failed-login-attempts', null)
session.forget('failed-login-attempts')
session.flash('message', 'Login Successful')
} else {
const failedLoginAttempts = (session.get('failed-login-attempts') || 0) as number
Expand Down
2 changes: 1 addition & 1 deletion test/node/src/test_cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ session_routes.post('/login', async (c) => {
if (password === 'correct') {
c.set('session_key_rotation', true)
session.set('email', email)
session.set('failed-login-attempts', null)
session.forget('failed-login-attempts')
session.flash('message', 'Login Successful')
} else {
const failedLoginAttempts = (session.get('failed-login-attempts') || 0) as number
Expand Down