forked from utae/react-native-naver-login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
89 lines (77 loc) · 1.96 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
import { NativeModules, Platform } from "react-native";
const { IosNaverLogin, RNNaverLogin } = NativeModules; // 여기 이름은 달라야 함.
export const UserCancelErrorCode = {
android: 'user_cancel',
iOS: 2
};
export interface NaverLoginError extends Error {
errCode?: number | string;
errDesc?: string;
}
export interface ICallback<T> {
(error: NaverLoginError | undefined, result: T | undefined): void;
}
export interface TokenResponse {
accessToken: string;
refreshToken: string;
expiresAt: string;
tokenType: string;
}
export interface GetProfileResponse {
resultcode: string;
message: string;
response: {
id: string;
profile_image: string | null;
email: string;
name: string;
birthday: string | null;
age: string | null;
birthyear: number | null;
gender: string | null;
mobile: string | null;
mobile_e164: string | null;
nickname: string | null;
};
}
export interface ConfigParam {
kConsumerKey: string;
kConsumerSecret: string;
kServiceAppName: string;
/** Only for iOS */
kServiceAppUrlScheme?: string;
}
const NaverLoginIos = {
login(param: ConfigParam, callback: ICallback<TokenResponse>): void {
IosNaverLogin.login(param, callback);
},
logout(): void {
IosNaverLogin.logout();
},
};
const RNNaverLoginAndr = {
login(param: ConfigParam, callback: ICallback<TokenResponse>): void {
RNNaverLogin.login(param, callback);
},
logout(): void {
RNNaverLogin.logout();
},
};
export const getProfile = (token: string): Promise<GetProfileResponse> => {
return fetch("https://openapi.naver.com/v1/nid/me", {
method: "GET",
headers: {
Authorization: "Bearer " + token,
},
})
.then((response) => response.json())
.then((responseJson) => {
return responseJson;
})
.catch((err) => {
console.log("getProfile err");
console.log(err);
});
};
export const NaverLogin =
Platform.OS === "ios" ? NaverLoginIos : RNNaverLoginAndr;