@react-native-kakao/user 1.6.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,204 @@
1
+ import { camelCaseObject, filterNonNullishKeys, is } from '@mj-studio/js-util';
2
+ import {
3
+ kCreateWebError,
4
+ kFetch,
5
+ kFetchFormUrlEncoded,
6
+ kGlobalStorage,
7
+ } from '@react-native-kakao/core';
8
+ import * as querystring from 'querystring';
9
+
10
+ import {
11
+ type KakaoLoginToken,
12
+ type KakaoScopeInfo,
13
+ type KakaoServiceTerms,
14
+ type KakaoShippingAddressResult,
15
+ type KakaoUser,
16
+ type KakaoUserAPI,
17
+ } from './index';
18
+
19
+ declare const Kakao: {
20
+ Auth: {
21
+ authorize: Function;
22
+ logout: Function;
23
+ setAccessToken: Function;
24
+ };
25
+ API: { request: Function };
26
+ };
27
+
28
+ function isoToUnix(value?: string | number) {
29
+ if (!is.string(value)) {
30
+ return value;
31
+ }
32
+
33
+ return Date.parse(value) / 1000;
34
+ }
35
+
36
+ const KakaoUser: KakaoUserAPI = {
37
+ getAccessToken: async () => {
38
+ try {
39
+ const { id, expires_in, app_id } = (
40
+ await kFetch<{
41
+ id: number;
42
+ expires_in: number;
43
+ app_id: number;
44
+ }>('https://kapi.kakao.com/v1/user/access_token_info', {
45
+ headers: {
46
+ ...kGlobalStorage.headers,
47
+ },
48
+ })
49
+ ).body;
50
+
51
+ return {
52
+ id,
53
+ expiresIn: expires_in,
54
+ appId: app_id,
55
+ };
56
+ } catch (e: any) {
57
+ throw kCreateWebError(e);
58
+ }
59
+ },
60
+ issueAccessTokenWithCodeWeb: async ({ clientSecret, code, redirectUri }) => {
61
+ try {
62
+ const res = await kFetchFormUrlEncoded<{
63
+ token_type: string;
64
+ access_token: string;
65
+ id_token?: string;
66
+ expires_in: number;
67
+ refresh_token: string;
68
+ refresh_token_expires_in: number;
69
+ scope?: string;
70
+ }>('https://kauth.kakao.com/oauth/token', {
71
+ method: 'POST',
72
+ body: filterNonNullishKeys({
73
+ grant_type: 'authorization_code',
74
+ client_id: kGlobalStorage.restApiKey,
75
+ redirect_uri: encodeURIComponent(redirectUri),
76
+ code,
77
+ client_secret: clientSecret,
78
+ }),
79
+ }).then((r) => r.body);
80
+
81
+ return camelCaseObject(res) as any;
82
+ } catch (e: any) {
83
+ throw kCreateWebError(e);
84
+ }
85
+ },
86
+ setAccessTokenWeb: (token: string) => {
87
+ kGlobalStorage.accessToken = token;
88
+
89
+ return new Promise((r) => {
90
+ Kakao.Auth.setAccessToken(token);
91
+ r(42);
92
+ });
93
+ },
94
+ login: async (params): Promise<KakaoLoginToken> => {
95
+ const { loginHint, nonce, prompt, redirectUri, scope, state, throughTalk, serviceTerms } =
96
+ params?.web ?? {};
97
+
98
+ return Kakao.Auth.authorize(
99
+ filterNonNullishKeys({
100
+ loginHint,
101
+ nonce,
102
+ prompt: prompt?.join(','),
103
+ redirectUri,
104
+ scope: scope?.join(','),
105
+ serviceTerms: serviceTerms?.join(','),
106
+ state,
107
+ throughTalk,
108
+ }),
109
+ );
110
+ },
111
+ logout: () => Kakao.Auth.logout().catch(kCreateWebError),
112
+ isLogined: async (): Promise<boolean> => {
113
+ try {
114
+ const { appId } = await KakaoUser.getAccessToken();
115
+
116
+ return !!appId;
117
+ } catch (e) {
118
+ return false;
119
+ }
120
+ },
121
+ isKakaoTalkLoginAvailable: async (): Promise<boolean> => false,
122
+ unlink: (): Promise<void> => Kakao.API.request({ url: '/v1/user/unlink' }).catch(kCreateWebError),
123
+ scopes: async (scopes?: string[]): Promise<KakaoScopeInfo[]> => {
124
+ try {
125
+ const response = await Kakao.API.request({
126
+ url: `/v2/user/scopes${scopes ? '?' + querystring.stringify({ scopes }) : ''}`,
127
+ });
128
+
129
+ return camelCaseObject(response.scopes) as KakaoScopeInfo[];
130
+ } catch (e: any) {
131
+ throw kCreateWebError(e);
132
+ }
133
+ },
134
+ revokeScopes: async (scopes: string[]): Promise<void> => {
135
+ try {
136
+ await Kakao.API.request({
137
+ url: '/v2/user/revoke/scopes',
138
+ data: {
139
+ scopes,
140
+ },
141
+ });
142
+ } catch (e: any) {
143
+ throw kCreateWebError(e);
144
+ }
145
+ },
146
+ serviceTerms: async (): Promise<KakaoServiceTerms[]> => {
147
+ try {
148
+ return camelCaseObject(
149
+ (
150
+ await Kakao.API.request({
151
+ url: '/v2/user/service_terms',
152
+ })
153
+ ).service_terms.map((v) => ({
154
+ ...v,
155
+ agreed_at: v.agreed_at ? isoToUnix(v.agreed_at) : null,
156
+ })),
157
+ ) as KakaoServiceTerms[];
158
+ } catch (e: any) {
159
+ throw kCreateWebError(e);
160
+ }
161
+ },
162
+ shippingAddresses: async (): Promise<KakaoShippingAddressResult> => {
163
+ try {
164
+ return camelCaseObject(
165
+ await Kakao.API.request({
166
+ url: '/v1/user/shipping_address',
167
+ }),
168
+ ) as KakaoShippingAddressResult;
169
+ } catch (e: any) {
170
+ throw kCreateWebError(e);
171
+ }
172
+ },
173
+ me: async (): Promise<KakaoUser> => {
174
+ try {
175
+ const ret = camelCaseObject(await Kakao.API.request({ url: '/v2/user/me' })) as KakaoUser;
176
+
177
+ return {
178
+ ...ret,
179
+ connectedAt: isoToUnix(ret.connectedAt),
180
+ synchedAt: isoToUnix(ret.synchedAt),
181
+ };
182
+ } catch (e: any) {
183
+ throw kCreateWebError(e);
184
+ }
185
+ },
186
+ };
187
+
188
+ export const {
189
+ isKakaoTalkLoginAvailable,
190
+ isLogined,
191
+ login,
192
+ logout,
193
+ me,
194
+ revokeScopes,
195
+ scopes,
196
+ serviceTerms,
197
+ shippingAddresses,
198
+ unlink,
199
+ setAccessTokenWeb,
200
+ issueAccessTokenWithCodeWeb,
201
+ getAccessToken,
202
+ } = KakaoUser;
203
+
204
+ export default KakaoUser;
@@ -2,6 +2,7 @@ import type { TurboModule } from 'react-native';
2
2
  import { TurboModuleRegistry } from 'react-native';
3
3
 
4
4
  import type {
5
+ KakaoAccessTokenInfo,
5
6
  KakaoLoginToken,
6
7
  KakaoScopeInfo,
7
8
  KakaoServiceTerms,
@@ -25,6 +26,7 @@ export interface Spec extends TurboModule {
25
26
  serviceTerms(): Promise<KakaoServiceTerms[]>;
26
27
  shippingAddresses(): Promise<KakaoShippingAddressResult>;
27
28
  me(): Promise<KakaoUser>;
29
+ getAccessToken(): Promise<KakaoAccessTokenInfo>;
28
30
  }
29
31
 
30
32
  export default TurboModuleRegistry.getEnforcing<Spec>('RNCKakaoUser');