← Public packages
@noah/mega-radio
MEGA Radio (the WAKE album) for members of the MEGA.dev Discord: sign in with Kody, verify with Discord, listen with repeat.
Files
/**
* Who is visiting, from the platform (never from cookies or headers the browser controls).
* Access tickets can lag a fresh sign-in, so retry briefly.
*/
import { kody } from 'kody:runtime'
import { APP_LEAF } from './config.ts'
export type Visitor = { userId: string; username: string }
/**
* Resolve the signed-in Kody visitor.
*
* @param retries - extra attempts for a sign-in that just completed
* @returns the visitor, or null when signed out
*/
export async function currentVisitor(retries = 0): Promise<Visitor | null> {
for (let attempt = 0; ; attempt++) {
try {
const me = (await kody.metaGetCurrentUser({})) as { user_id?: string; username?: string }
if (me?.user_id) return { userId: me.user_id, username: me.username ?? '' }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
if (!message.includes('Authenticated MCP user is required')) throw error
}
if (attempt >= retries) return null
await new Promise((r) => setTimeout(r, 600))
}
}
/**
* Sign in with Kody URL (platform route at the host root).
*
* @param returnPath - absolute path to come back to, e.g. `/mega-radio/kody/return` (old address) or `/kody/return`
* @param needs - extra consent scopes, e.g. `need:discord`
* @returns the login URL
*/
export function loginUrl(returnPath: string, needs: string[] = []): string {
const scope = needs.length ? `&scope=${encodeURIComponent(needs.join(' '))}` : ''
return `/-/auth/login?app=${APP_LEAF}&return_to=${encodeURIComponent(returnPath)}${scope}`
}
export const logoutUrl = () => `/-/auth/logout?app=${APP_LEAF}`