MCPcopy
hub / github.com/21st-dev/1code / exchangeCode

Method exchangeCode

src/main/auth-manager.ts:45–73  ·  view source on GitHub ↗

* Exchange auth code for session tokens * Called after receiving code via deep link

(code: string)

Source from the content-addressed store, hash-verified

43 * Called after receiving code via deep link
44 */
45 async exchangeCode(code: string): Promise<AuthData> {
46 const response = await fetch(`${this.getApiUrl()}/api/auth/desktop/exchange`, {
47 method: "POST",
48 headers: { "Content-Type": "application/json" },
49 body: JSON.stringify({
50 code,
51 deviceInfo: this.getDeviceInfo(),
52 }),
53 })
54
55 if (!response.ok) {
56 const error = await response.json().catch(() => ({ error: "Unknown error" }))
57 throw new Error(error.error || `Exchange failed: ${response.status}`)
58 }
59
60 const data = await response.json()
61
62 const authData: AuthData = {
63 token: data.token,
64 refreshToken: data.refreshToken,
65 expiresAt: data.expiresAt,
66 user: data.user,
67 }
68
69 this.store.save(authData)
70 this.scheduleRefresh()
71
72 return authData
73 }
74
75 /**
76 * Get device info for session tracking

Callers 1

handleAuthCodeFunction · 0.80

Calls 4

getApiUrlMethod · 0.95
getDeviceInfoMethod · 0.95
scheduleRefreshMethod · 0.95
saveMethod · 0.45

Tested by

no test coverage detected