Skip to content

Commit 8274fa1

Browse files
authored
chore: tidy up oauth exports (#491)
1 parent b0af236 commit 8274fa1

File tree

3 files changed

+81
-81
lines changed

3 files changed

+81
-81
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { NodeSavedSession, NodeSavedSessionStore } from '@atproto/oauth-client-node'
2+
import type { H3Event } from 'h3'
3+
4+
/**
5+
* Storage key prefix for oauth session storage.
6+
*/
7+
export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session'
8+
9+
export class OAuthSessionStore implements NodeSavedSessionStore {
10+
// TODO: not sure if we will support multi accounts, but if we do in the future will need to change this around
11+
private readonly cookieKey = 'oauth:atproto:session'
12+
private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE)
13+
14+
constructor(private event: H3Event) {}
15+
16+
async get(): Promise<NodeSavedSession | undefined> {
17+
const sessionKey = getCookie(this.event, this.cookieKey)
18+
if (!sessionKey) return
19+
const result = await this.storage.getItem<NodeSavedSession>(sessionKey)
20+
if (!result) return
21+
return result
22+
}
23+
24+
async set(key: string, val: NodeSavedSession) {
25+
setCookie(this.event, this.cookieKey, key, {
26+
httpOnly: true,
27+
secure: !import.meta.dev,
28+
sameSite: 'lax',
29+
})
30+
await this.storage.setItem<NodeSavedSession>(key, val)
31+
}
32+
33+
async del() {
34+
const sessionKey = getCookie(this.event, this.cookieKey)
35+
if (sessionKey) {
36+
await this.storage.del(sessionKey)
37+
}
38+
deleteCookie(this.event, this.cookieKey)
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { NodeSavedState, NodeSavedStateStore } from '@atproto/oauth-client-node'
2+
import type { H3Event } from 'h3'
3+
4+
/**
5+
* Storage key prefix for oauth state storage.
6+
*/
7+
export const OAUTH_STATE_CACHE_STORAGE_BASE = 'oauth-atproto-state'
8+
9+
export class OAuthStateStore implements NodeSavedStateStore {
10+
private readonly cookieKey = 'oauth:atproto:state'
11+
private readonly storage = useStorage(OAUTH_STATE_CACHE_STORAGE_BASE)
12+
13+
constructor(private event: H3Event) {}
14+
15+
async get(): Promise<NodeSavedState | undefined> {
16+
const stateKey = getCookie(this.event, this.cookieKey)
17+
if (!stateKey) return
18+
const result = await this.storage.getItem<NodeSavedState>(stateKey)
19+
if (!result) return
20+
return result
21+
}
22+
23+
async set(key: string, val: NodeSavedState) {
24+
setCookie(this.event, this.cookieKey, key, {
25+
httpOnly: true,
26+
secure: !import.meta.dev,
27+
sameSite: 'lax',
28+
})
29+
await this.storage.setItem<NodeSavedState>(key, val)
30+
}
31+
32+
async del() {
33+
const stateKey = getCookie(this.event, this.cookieKey)
34+
deleteCookie(this.event, this.cookieKey)
35+
if (stateKey) {
36+
await this.storage.del(stateKey)
37+
}
38+
}
39+
}

server/utils/atproto/storage.ts

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,6 @@
1-
import type {
2-
NodeSavedSession,
3-
NodeSavedSessionStore,
4-
NodeSavedState,
5-
NodeSavedStateStore,
6-
} from '@atproto/oauth-client-node'
71
import type { H3Event } from 'h3'
8-
9-
/**
10-
* Storage key prefix for oauth state storage.
11-
*/
12-
export const OAUTH_STATE_CACHE_STORAGE_BASE = 'oauth-atproto-state'
13-
14-
export class OAuthStateStore implements NodeSavedStateStore {
15-
private readonly cookieKey = 'oauth:atproto:state'
16-
private readonly storage = useStorage(OAUTH_STATE_CACHE_STORAGE_BASE)
17-
18-
constructor(private event: H3Event) {}
19-
20-
async get(): Promise<NodeSavedState | undefined> {
21-
const stateKey = getCookie(this.event, this.cookieKey)
22-
if (!stateKey) return
23-
const result = await this.storage.getItem<NodeSavedState>(stateKey)
24-
if (!result) return
25-
return result
26-
}
27-
28-
async set(key: string, val: NodeSavedState) {
29-
setCookie(this.event, this.cookieKey, key, {
30-
httpOnly: true,
31-
secure: !import.meta.dev,
32-
sameSite: 'lax',
33-
})
34-
await this.storage.setItem<NodeSavedState>(key, val)
35-
}
36-
37-
async del() {
38-
const stateKey = getCookie(this.event, this.cookieKey)
39-
deleteCookie(this.event, this.cookieKey)
40-
if (stateKey) {
41-
await this.storage.del(stateKey)
42-
}
43-
}
44-
}
45-
46-
/**
47-
* Storage key prefix for oauth session storage.
48-
*/
49-
export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session'
50-
51-
export class OAuthSessionStore implements NodeSavedSessionStore {
52-
// TODO: not sure if we will support multi accounts, but if we do in the future will need to change this around
53-
private readonly cookieKey = 'oauth:atproto:session'
54-
private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE)
55-
56-
constructor(private event: H3Event) {}
57-
58-
async get(): Promise<NodeSavedSession | undefined> {
59-
const sessionKey = getCookie(this.event, this.cookieKey)
60-
if (!sessionKey) return
61-
const result = await this.storage.getItem<NodeSavedSession>(sessionKey)
62-
if (!result) return
63-
return result
64-
}
65-
66-
async set(key: string, val: NodeSavedSession) {
67-
setCookie(this.event, this.cookieKey, key, {
68-
httpOnly: true,
69-
secure: !import.meta.dev,
70-
sameSite: 'lax',
71-
})
72-
await this.storage.setItem<NodeSavedSession>(key, val)
73-
}
74-
75-
async del() {
76-
const sessionKey = getCookie(this.event, this.cookieKey)
77-
if (sessionKey) {
78-
await this.storage.del(sessionKey)
79-
}
80-
deleteCookie(this.event, this.cookieKey)
81-
}
82-
}
2+
import { OAuthStateStore } from './oauth-state-store'
3+
import { OAuthSessionStore } from './oauth-session-store'
834

845
export const useOAuthStorage = (event: H3Event) => {
856
return {

0 commit comments

Comments
 (0)