forked from crossplatformkorea/react-native-kakao-login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
201 lines (177 loc) · 5.08 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { NativeModules } from 'react-native';
const { RNKakaoLogins } = NativeModules;
export interface ICallback<T> {
(error: Error | undefined, result: T | undefined): void;
}
export interface ITokenInfo {
accessTokenExpiresAt: string;
refreshToken: string;
refreshTokenExpiresAt: string | null;
accessToken: string;
scopes?: string[]; // iOS only
}
export interface IProfile {
id: string;
nickname: string | null;
profile_image_url: string | null;
thumb_image_url: string | null;
email: string | null;
display_id: string | null;
phone_number: string | null;
is_email_verified: boolean | null;
is_kakaotalk_user: boolean | null;
has_signed_up: boolean | null;
birthday: string | null;
gender: string | null;
}
/**
* KAKAO_ERROR 에러 코드
* 모든 에러코드가 등록되어있지는 않습니다. 아래의 링크를 참조하세요
*
* SHARED : 공통 에러
* IOS : iOS 에러코드
* ANDROID : Android 에러코드
*
* @url Android : https://developers.kakao.com/docs/android-reference/com/kakao/auth/ApiErrorCode.html
* @url iOS : https://developers.kakao.com/docs/ios-reference/KOError_h/index.html#//apple_ref/c/tdef/KOErrorCode
* */
export enum KAKAO_ERROR {
// SHARED
E_UNKNOWN = 'E_UNKNOWN',
E_CANCELLED_OPERATION = 'E_CANCELLED_OPERATION',
E_ILLEGAL_STATE = 'E_ILLEGAL_STATE',
// IOS
E_IN_PROGRESS_OPERATION = 'E_IN_PROGRESS_OPERATION',
E_TOKEN_NOT_FOUND = 'E_TOKEN_NOT_FOUND',
E_DEACTIVATED_SESSION = 'E_DEACTIVATED_SESSION',
E_ALREADY_LOGINED = 'E_ALREADY_LOGINED',
E_HTTP_ERROR = 'E_HTTP_ERROR',
E_BAD_RESPONSE = 'E_BAD_RESPONSE',
E_NETWORK_ERROR = 'E_NETWORK_ERROR',
E_NOT_SUPPORTED = 'E_NOT_SUPPORTED',
E_BAD_PARAMETER = 'E_BAD_PARAMETER',
// ANDROID
E_ILLEGAL_ARGUMENT = 'E_ILLEGAL_ARGUMENT',
E_MISS_CONFIGURATION = 'E_MISS_CONFIGURATION',
E_AUTHORIZATION_FAILED = 'E_AUTHORIZATION_FAILED',
E_JSON_PARSING_ERROR = 'E_JSON_PARSING_ERROR',
E_URI_LENGTH_EXCEEDED = 'E_URI_LENGTH_EXCEEDED',
E_KAKAOTALK_NOT_INSTALLED = 'E_KAKAOTALK_NOT_INSTALLED',
}
function isFunction(item): boolean {
return item ? typeof item === 'function' : false;
}
/**
* login
* @param {ICallback<ITokenInfo>} [callback] callback function
* @returns {Promise<ITokenInfo>}
*/
export function login(callback?: ICallback<ITokenInfo>): Promise<ITokenInfo> {
return RNKakaoLogins.login()
.then((result: ITokenInfo) => {
const timeReFormattedResult = {
...result,
accessTokenExpiresAt: result.accessTokenExpiresAt.replace(' ', 'T'),
refreshTokenExpiresAt: result.refreshTokenExpiresAt.replace(' ', 'T'),
};
if (isFunction(callback)) {
callback(null, timeReFormattedResult);
}
return timeReFormattedResult;
})
.catch((error) => {
if (isFunction(callback)) {
callback(error, null);
}
throw error;
});
}
/**
* logout
* @param {ICallback<string>} [callback] callback function
* @returns {Promise<string>}
*/
export function logout(callback?: ICallback<string>): Promise<string> {
return RNKakaoLogins.logout()
.then((result) => {
if (isFunction(callback)) {
callback(null, result);
}
return result;
})
.catch((error) => {
if (isFunction(callback)) {
callback(error, null);
}
throw error;
});
}
/**
* getProfile
* @param {ICallback<IProfile>} [callback] callback function. id, nickname, email, display_id, phone_number, email_verified, kakatalk_user, profile_image_path, has_signed_up values will be received with json string in result field.
* @returns {Promise<ITokenInfo>}
*/
export function getProfile(callback?: ICallback<IProfile>): Promise<IProfile> {
return RNKakaoLogins.getProfile()
.then((result: IProfile) => {
if (isFunction(callback)) {
callback(null, result);
}
return result;
})
.catch((error) => {
if (isFunction(callback)) {
callback(error, null);
}
throw error;
});
}
/**
* unlink
* @param {ICallback<string>} [callback] callback function
* @returns {Promise<string>}
*/
export function unlink(callback?: ICallback<string>): Promise<string> {
return RNKakaoLogins.unlink()
.then((result) => {
if (isFunction(callback)) {
callback(null, result);
}
return result;
})
.catch((error) => {
if (isFunction(callback)) {
callback(error, null);
}
throw error;
});
}
/**
* updateScopes
* @param {Array<string>} scopes request scopes
* @param {ICallback<ITokenInfo>} [callback] callback function
* @returns {Promise<ITokenInfo>}
*/
export function updateScopes(scopes: Array<string>, callback?: ICallback<ITokenInfo>): Promise<ITokenInfo> {
return RNKakaoLogins.updateScopes(scopes)
.then((result) => {
if (isFunction(callback)) {
callback(null, result);
}
return result;
})
.catch((error) => {
if (isFunction(callback)) {
callback(error, null);
}
throw error;
});
}
const KakaoLogins = {
login,
logout,
getProfile,
unlink,
updateScopes
};
export default KakaoLogins;