UNPKG

react-native-zalo-auth

Version:
106 lines (105 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Constants", { enumerable: true, get: function () { return _types.Constants; } }); exports.sendMessageByApp = exports.sendMessage = exports.register = exports.postFeedByApp = exports.postFeed = exports.logout = exports.login = exports.isAuthenticated = exports.inviteFriendUseApp = exports.getUserProfile = exports.getUserInvitableFriendList = exports.getUserFriendList = exports.getApplicationHashKey = exports.default = void 0; var _reactNative = require("react-native"); var _types = require("./types"); const LINKING_ERROR = `The package 'react-native-zalo-auth' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo managed workflow\n'; const ZaloKit = _reactNative.NativeModules.ZaloKit ? _reactNative.NativeModules.ZaloKit : new Proxy({}, { get() { throw new Error(LINKING_ERROR); } }); const login = async (authType = _types.Constants.AUTH_VIA_APP_OR_WEB) => { const AUTH_TYPES = [_types.Constants.AUTH_VIA_APP, _types.Constants.AUTH_VIA_APP_OR_WEB, _types.Constants.AUTH_VIA_WEB]; const AUTH_TYPE_KEYS = AUTH_TYPES.map(key => { return _types.Constants[key]; }); if (!AUTH_TYPES.includes(authType)) { throw new Error(`"authType" must be one of [${AUTH_TYPE_KEYS.join(', ')}]`); } return ZaloKit.login(authType); }; exports.login = login; const register = exports.register = ZaloKit.register; const logout = exports.logout = ZaloKit.logout; const isAuthenticated = exports.isAuthenticated = ZaloKit.isAuthenticated; const getUserProfile = async accessToken => { if (_reactNative.Platform.OS === 'android') { return await ZaloKit.getUserProfile(); } if (!accessToken) { console.warn('Access token is required for iOS'); return null; } try { const response = await fetch('https://graph.zalo.me/v2.0/me?fields=id,birthday,gender,picture,name,phone_number', { method: 'GET', headers: { access_token: accessToken } }); if (!response.ok) { throw new Error(`Zalo API error: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error('Failed to get Zalo user profile:', error); return null; } }; exports.getUserProfile = getUserProfile; const getApplicationHashKey = () => { if (_reactNative.Platform.OS === 'android') { return ZaloKit.getApplicationHashKey(); } throw new Error('This function is only supported on Android'); }; exports.getApplicationHashKey = getApplicationHashKey; const getUserFriendList = async (offset, count) => ZaloKit.getUserFriendList(offset, count); exports.getUserFriendList = getUserFriendList; const getUserInvitableFriendList = async (offset, count) => ZaloKit.getUserInvitableFriendList(offset, count); exports.getUserInvitableFriendList = getUserInvitableFriendList; const postFeed = async (message, link) => ZaloKit.postFeed(message, link); exports.postFeed = postFeed; const sendMessage = async (friendId, link, message) => ZaloKit.sendMessage(friendId, message, link); exports.sendMessage = sendMessage; const inviteFriendUseApp = async (friendIds, message) => { if (_reactNative.Platform.OS === 'android') { return ZaloKit.inviteFriendUseApp(friendIds, message); } return ZaloKit.inviteFriendUseApp(friendIds.join(','), message); }; exports.inviteFriendUseApp = inviteFriendUseApp; const sendMessageByApp = async feedData => ZaloKit.sendMessageByApp(feedData); exports.sendMessageByApp = sendMessageByApp; const postFeedByApp = async feedData => ZaloKit.postFeedByApp(feedData); exports.postFeedByApp = postFeedByApp; var _default = exports.default = { Constants: _types.Constants, getApplicationHashKey, login, logout, isAuthenticated, getUserProfile, getUserFriendList, getUserInvitableFriendList, postFeed, postFeedByApp, sendMessage, sendMessageByApp, inviteFriendUseApp, register }; //# sourceMappingURL=index.js.map