-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeefree-token.ts
More file actions
50 lines (43 loc) · 1.77 KB
/
beefree-token.ts
File metadata and controls
50 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { IToken } from '../dist/index'
/**
* **************************************************************************** *
* !!!! WARNING !!!! *
* *
* This is done on the frontend to get the example working. *
* Set up a backend authentication server to avoid exposing your credentials! *
* *
* For a production-ready server implementation, see: *
* github.com/BeefreeSDK/beefree-sdk-examples/tree/main/secure-auth-example *
* *
* **************************************************************************** *
*/
const AUTH_URL = 'https://auth.getbee.io/loginV2'
export async function getBuilderToken(
clientId: string,
clientSecret: string,
userId = 'user',
): Promise<IToken> {
let response: Response
try {
response = await fetch(AUTH_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
uid: userId,
}),
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown network error'
throw new Error(`Network error: unable to reach authentication server (${message})`)
}
if (!response.ok) {
throw new Error(`Authentication failed: ${response.status} ${response.statusText}`)
}
const token = await response.json()
if (!token || !token.access_token) {
throw new Error('Invalid credentials: no access token returned')
}
return token
}