-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from farcasterxyz/horsefacts/redis-sessions
feat: redis-backed sessions
- Loading branch information
Showing
12 changed files
with
70 additions
and
16 deletions.
There are no files selected for viewing
Binary file not shown.
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,20 @@ | ||
version: "3.9" | ||
|
||
services: | ||
redis: | ||
image: "redis:7.2-alpine" | ||
restart: unless-stopped | ||
command: --save 1 1 --loglevel warning --maxmemory-policy noeviction | ||
volumes: | ||
- redis-data:/data | ||
ports: | ||
- "6379:6379" | ||
healthcheck: | ||
test: ["CMD-SHELL", "redis-cli", "ping"] | ||
interval: 10s | ||
timeout: 10s | ||
retries: 3 | ||
start_period: 5s | ||
|
||
volumes: | ||
redis-data: |
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
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
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
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
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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { cookies } from 'next/headers'; | ||
import { clearSessionToken } from "@lib/redis/sessions"; | ||
import { cookies } from "next/headers"; | ||
|
||
import { getTokenFromCookieOrHeader } from './getTokenFromCookieOrHeader'; | ||
import { tokenKey, tokens } from './shared'; | ||
import { getTokenFromCookieOrHeader } from "./getTokenFromCookieOrHeader"; | ||
import { tokenKey } from "./shared"; | ||
|
||
export function clearCurrentUser() { | ||
export async function clearCurrentUser() { | ||
const token = getTokenFromCookieOrHeader(); | ||
|
||
cookies().delete(tokenKey); | ||
|
||
if (token) { | ||
delete tokens[token]; | ||
await clearSessionToken({ token }); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { setSessionToken } from '@lib/redis/sessions'; | ||
import { cookies } from 'next/headers'; | ||
|
||
import { tokenKey, tokens } from './shared'; | ||
import { tokenKey } from './shared'; | ||
|
||
export function setCurrentUser({ token, fid }: { token: string; fid: string }) { | ||
export async function setCurrentUser({ token, fid }: { token: string; fid: string }) { | ||
cookies().set(tokenKey, token, { secure: true }); | ||
tokens[token] = fid; | ||
return setSessionToken({ token, fid }); | ||
} |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export const tokenKey = 'token'; | ||
export const tokens: Record<string, string> = {}; | ||
export const tokenKey = "token"; |
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,3 @@ | ||
import Redis from 'ioredis'; | ||
|
||
export const redis = new Redis(process.env.REDIS_URL ?? "redis://localhost:6379"); |
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,28 @@ | ||
import { tokenKey } from "@lib/auth/shared"; | ||
|
||
import { redis } from "./client"; | ||
|
||
const ttl = 60 * 60 * 24 * 30; | ||
|
||
interface Session { | ||
fid?: string; | ||
} | ||
|
||
export async function setSessionToken({ | ||
token, | ||
fid, | ||
}: { | ||
token: string; | ||
fid: string; | ||
}) { | ||
return redis.set(`${tokenKey}:${token}`, JSON.stringify({ fid }), "EX", ttl); | ||
} | ||
|
||
export async function clearSessionToken({ token }: { token: string }) { | ||
return redis.del(`${tokenKey}:${token}`); | ||
} | ||
|
||
export async function getSessionByToken({ token }: { token: string }) { | ||
const session = (await redis.get(`${tokenKey}:${token}`)) ?? "{}"; | ||
return JSON.parse(session) as Session; | ||
} |