Skip to content

Commit 005f661

Browse files
committed
follow up on items
1 parent 9102628 commit 005f661

8 files changed

Lines changed: 42 additions & 38 deletions

File tree

nuxt.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ export default defineNuxtConfig({
5151

5252
devtools: { enabled: true },
5353

54+
devServer: {
55+
// Used with atproto oauth
56+
// https://atproto.com/specs/oauth#localhost-client-development
57+
host: '127.0.0.1',
58+
},
59+
5460
app: {
5561
head: {
5662
htmlAttrs: { lang: 'en-US' },

server/api/auth/atproto.get.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Agent } from '@atproto/api'
22
import { NodeOAuthClient } from '@atproto/oauth-client-node'
33
import { createError, getQuery, sendRedirect } from 'h3'
4-
import { OAuthSessionStore, OAuthStateStore } from '#server/utils/atproto/storage'
4+
import { useOAuthStorage } from '#server/utils/atproto/storage'
55
import { SLINGSHOT_ENDPOINT } from '#shared/utils/constants'
66

77
export default defineEventHandler(async event => {
@@ -15,8 +15,8 @@ export default defineEventHandler(async event => {
1515

1616
const query = getQuery(event)
1717
const clientMetadata = getOauthClientMetadata()
18-
const stateStore = new OAuthStateStore(event)
19-
const sessionStore = new OAuthSessionStore(event)
18+
const { stateStore, sessionStore } = useOAuthStorage(event)
19+
2020
const atclient = new NodeOAuthClient({
2121
stateStore,
2222
sessionStore,

server/api/auth/session.delete.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
2-
const config = useRuntimeConfig(event)
3-
if (!config.sessionPassword) {
4-
throw createError({
5-
status: 500,
6-
message: 'NUXT_SESSION_PASSWORD not set',
7-
})
8-
}
9-
10-
const session = await useSession(event, {
11-
password: config.sessionPassword,
12-
})
13-
1+
export default eventHandlerWithOAuthSession(async (event, oAuthSession, serverSession) => {
142
await oAuthSession?.signOut()
15-
await session.clear()
3+
await serverSession.clear()
164

175
return 'Session cleared'
186
})

server/api/auth/session.get.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
export default defineEventHandler(async event => {
2-
const config = useRuntimeConfig(event)
3-
if (!config.sessionPassword) {
4-
throw createError({
5-
status: 500,
6-
message: 'NUXT_SESSION_PASSWORD not set',
7-
})
8-
}
9-
10-
const session = await useSession(event, {
11-
password: config.sessionPassword,
12-
})
13-
14-
return session.data
1+
export default eventHandlerWithOAuthSession(async (event, oAuthSession, serverSession) => {
2+
return serverSession.data
153
})

server/utils/atproto/oauth.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { OAuthClientMetadataInput } from '@atproto/oauth-client-node'
22
import type { EventHandlerRequest, H3Event } from 'h3'
33
import type { OAuthSession } from '@atproto/oauth-client-node'
44
import { NodeOAuthClient } from '@atproto/oauth-client-node'
5-
import { OAuthSessionStore, OAuthStateStore } from '#server/utils/atproto/storage'
6-
5+
import { useOAuthStorage } from '#server/utils/atproto/storage'
6+
import { UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
7+
import type { SessionManager } from 'h3'
78
// TODO: limit scope as features gets added. atproto just allows login so no scary login screen till we have scopes
89
export const scope = 'atproto'
910

@@ -34,12 +35,12 @@ export function getOauthClientMetadata() {
3435
type EventHandlerWithOAuthSession<T extends EventHandlerRequest, D> = (
3536
event: H3Event<T>,
3637
session: OAuthSession | undefined,
38+
serverSession: SessionManager,
3739
) => Promise<D>
3840

3941
async function getOAuthSession(event: H3Event): Promise<OAuthSession | undefined> {
4042
const clientMetadata = getOauthClientMetadata()
41-
const stateStore = new OAuthStateStore(event)
42-
const sessionStore = new OAuthSessionStore(event)
43+
const { stateStore, sessionStore } = useOAuthStorage(event)
4344

4445
const client = new NodeOAuthClient({
4546
stateStore,
@@ -59,7 +60,20 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
5960
handler: EventHandlerWithOAuthSession<T, D>,
6061
) {
6162
return defineEventHandler(async event => {
63+
const config = useRuntimeConfig(event)
64+
65+
if (!config.sessionPassword) {
66+
throw createError({
67+
status: 500,
68+
message: UNSET_NUXT_SESSION_PASSWORD,
69+
})
70+
}
71+
72+
const serverSession = await useSession(event, {
73+
password: config.sessionPassword,
74+
})
75+
6276
const oAuthSession = await getOAuthSession(event)
63-
return await handler(event, oAuthSession)
77+
return await handler(event, oAuthSession, serverSession)
6478
})
6579
}

server/utils/atproto/storage.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class OAuthStateStore implements NodeSavedStateStore {
4545
export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session'
4646

4747
export class OAuthSessionStore implements NodeSavedSessionStore {
48-
//TODO not sure if we will support multi accounts, but if we do in the future will need to change this around
48+
// TODO: not sure if we will support multi accounts, but if we do in the future will need to change this around
4949
private readonly cookieKey = 'oauth:atproto:session'
5050
private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE)
5151

@@ -72,3 +72,10 @@ export class OAuthSessionStore implements NodeSavedSessionStore {
7272
deleteCookie(this.event, this.cookieKey)
7373
}
7474
}
75+
76+
export const useOAuthStorage = (event: H3Event) => {
77+
return {
78+
stateStore: new OAuthStateStore(event),
79+
sessionStore: new OAuthSessionStore(event),
80+
}
81+
}

shared/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const ERROR_CALC_INSTALL_SIZE_FAILED = 'Failed to calculate install size.
1616
export const NPM_MISSING_README_SENTINEL = 'ERROR: No README data found!'
1717
export const ERROR_JSR_FETCH_FAILED = 'Failed to fetch package from JSR registry.'
1818
export const ERROR_NPM_FETCH_FAILED = 'Failed to fetch package from npm registry.'
19+
export const UNSET_NUXT_SESSION_PASSWORD = 'NUXT_SESSION_PASSWORD not set'
1920
/** @public */
2021
export const ERROR_SUGGESTIONS_FETCH_FAILED = 'Failed to fetch suggestions.'
2122

shared/utils/fetch-cache-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const FETCH_CACHE_ALLOWED_DOMAINS = [
2626
'api.bitbucket.org', // Bitbucket API
2727
'codeberg.org', // Codeberg (Gitea-based)
2828
'gitee.com', // Gitee API
29-
//microcosm endpoints for atproto data
29+
// microcosm endpoints for atproto data
3030
CONSTELLATION_ENDPOINT,
3131
SLINGSHOT_ENDPOINT,
3232
] as const

0 commit comments

Comments
 (0)