UNPKG

@react-native-kakao/user

Version:
149 lines 4.18 kB
import { camelCaseObject, filterNonNullishKeys, is } from '@mj-studio/js-util'; import { kFetch, kFetchFormUrlEncoded, kGlobalStorage, kRunWebAPI } from '@react-native-kakao/core'; import * as querystring from 'querystring'; function isoToUnix(value) { if (!is.string(value)) { return value; } return Date.parse(value) / 1000; } const KakaoUser = { getAccessToken: () => kRunWebAPI(async () => { const { id, expires_in, app_id } = (await kFetch('https://kapi.kakao.com/v1/user/access_token_info', { headers: { ...kGlobalStorage.headers } })).body; return { id, expiresIn: expires_in, appId: app_id }; }), issueAccessTokenWithCodeWeb: ({ clientSecret, code, redirectUri }) => kRunWebAPI(async () => { const res = await kFetchFormUrlEncoded('https://kauth.kakao.com/oauth/token', { method: 'POST', body: filterNonNullishKeys({ grant_type: 'authorization_code', client_id: kGlobalStorage.restApiKey, redirect_uri: encodeURIComponent(redirectUri), code, client_secret: clientSecret }) }).then(r => r.body); return camelCaseObject(res); }), setAccessTokenWeb: token => { kGlobalStorage.accessToken = token; return new Promise(r => { Kakao.Auth.setAccessToken(token); r(42); }); }, login: params => kRunWebAPI(() => { const { loginHint, nonce, prompt, redirectUri, scope, state, throughTalk, serviceTerms } = (params === null || params === void 0 ? void 0 : params.web) ?? {}; return Kakao.Auth.authorize(filterNonNullishKeys({ loginHint, nonce, prompt: prompt === null || prompt === void 0 ? void 0 : prompt.join(','), redirectUri, scope: scope === null || scope === void 0 ? void 0 : scope.join(','), serviceTerms: serviceTerms === null || serviceTerms === void 0 ? void 0 : serviceTerms.join(','), state, throughTalk })); }), logout: () => kRunWebAPI(() => Kakao.Auth.logout()), isLogined: async () => { try { const { appId } = await KakaoUser.getAccessToken(); return !!appId; } catch (e) { return false; } }, isKakaoTalkLoginAvailable: async () => false, unlink: () => kRunWebAPI(() => Kakao.API.request({ url: '/v1/user/unlink' })), scopes: scopes => kRunWebAPI(async () => { const response = await Kakao.API.request({ url: `/v2/user/scopes${scopes ? '?' + querystring.stringify({ scopes }) : ''}` }); return camelCaseObject(response.scopes); }), revokeScopes: scopes => kRunWebAPI(() => Kakao.API.request({ url: '/v2/user/revoke/scopes', data: { scopes } })), serviceTerms: () => kRunWebAPI(async () => camelCaseObject((await Kakao.API.request({ url: '/v2/user/service_terms' })).service_terms.map(v => ({ ...v, agreed_at: v.agreed_at ? isoToUnix(v.agreed_at) : null })))), shippingAddresses: () => kRunWebAPI(async () => camelCaseObject(await Kakao.API.request({ url: '/v1/user/shipping_address' }))), me: () => kRunWebAPI(async () => { const ret = camelCaseObject(await Kakao.API.request({ url: '/v2/user/me' })); const kakaoAccount = ret.kakaoAccount ?? {}; const properties = ret.properties ?? {}; return { ...ret, ...properties, ...kakaoAccount, email: kakaoAccount.email, name: null, nickname: kakaoAccount.profile.nickname, profileImageUrl: properties.profileImage, thumbnailImageUrl: properties.thumbnailImage, phoneNumber: kakaoAccount.profile.phoneNumber, isEmailValid: ret.isEmailValid, isEmailVerified: ret.isEmailVerified, connectedAt: isoToUnix(ret.connectedAt), synchedAt: isoToUnix(ret.synchedAt) }; }) }; export const { isKakaoTalkLoginAvailable, isLogined, login, logout, me, revokeScopes, scopes, serviceTerms, shippingAddresses, unlink, setAccessTokenWeb, issueAccessTokenWithCodeWeb, getAccessToken } = KakaoUser; //# sourceMappingURL=index.web.js.map