-
Notifications
You must be signed in to change notification settings - Fork 18
/
Authorization.ts
73 lines (64 loc) · 2.05 KB
/
Authorization.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Axios, { AxiosResponse } from 'axios'
export interface AccessTokenResponse {
access_token: string
refresh_token: string
token_type: string
expires_in: number
}
export interface UserRecord {
userId: string
accessToken: string
refreshToken: string
accessTokenExpiry?: string
updatedAt?: string
email?: string
skillRegion?: string
isBlocked?: boolean
allowedDeviceCount?: number
plan?: string
stripeCustomerId?: string
deleteAtUnixTime?: number
}
export type PartialUserRecord = Pick<UserRecord, 'userId'> & Partial<UserRecord>
export async function fetchAccessAndRefreshToken(
event
): Promise<AccessTokenResponse> {
const data = `grant_type=authorization_code&code=${event.directive.payload.grant.code}&client_id=${process.env.ALEXA_CLIENT_ID}&client_secret=${process.env.ALEXA_CLIENT_SECRET}`
const url = 'https://api.amazon.com/auth/o2/token'
const response: AxiosResponse = await Axios.post(url, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
// console.log(':::fetchAccessAndRefreshToken:::')
// console.log(url)
// console.log(data)
// console.log(response.data)
// {
// "access_token":"Atza|IQEBLjAsAhRmHjNmHpi0U-Dme37rR6CuUpSR...",
// "token_type":"bearer",
// "expires_in":3600,
// "refresh_token":"Atzr|IQEBLzAtAhRxpMJxdwVz2Nn6f2y-tpJX3DeX..."
// }
return response.data
}
export async function fetchFreshAccessToken(
refreshToken: string
): Promise<AccessTokenResponse> {
const response: AxiosResponse = await Axios.post(
'https://api.amazon.com/auth/o2/token',
`grant_type=refresh_token&refresh_token=${refreshToken}&client_id=${process.env.ALEXA_CLIENT_ID}&client_secret=${process.env.ALEXA_CLIENT_SECRET}`,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
)
// {
// "access_token":"Atza|IQEBLjAsAhRmHjNmHpi0U-Dme37rR6CuUpSR...",
// "token_type":"bearer",
// "expires_in":3600,
// "refresh_token":"Atzr|IQEBLzAtAhRxpMJxdwVz2Nn6f2y-tpJX3DeX..."
// }
return response.data
}