UNPKG

react-facebook-auth

Version:
196 lines (165 loc) 7.17 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var FacebookAuth = function (_React$Component) { _inherits(FacebookAuth, _React$Component); function FacebookAuth() { var _ref; var _temp, _this, _ret; _classCallCheck(this, FacebookAuth); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = FacebookAuth.__proto__ || Object.getPrototypeOf(FacebookAuth)).call.apply(_ref, [this].concat(args))), _this), _this.responseApi = function (authResponse) { window.FB.api('/me', { locale: _this.props.language, fields: _this.props.fields }, function (me) { _this.props.callback(_extends({}, me, authResponse)); }); }, _this.checkLoginState = function (response) { if (response.authResponse) { _this.responseApi(response.authResponse); return; } if (_this.props.onFailure) { _this.props.onFailure({ status: response.status }); } else { _this.props.callback({ status: response.status }); } }, _this.checkLoginAfterRefresh = function (response) { if (response.status === 'unknown') { window.FB.login(function (loginResponse) { return _this.checkLoginState(loginResponse); }, true); } else { _this.checkLoginState(response); } }, _this.click = function (e) { var _this$props = _this.props, scope = _this$props.scope, returnScopes = _this$props.returnScopes, appId = _this$props.appId, onClick = _this$props.onClick, reAuthenticate = _this$props.reAuthenticate, reRequest = _this$props.reRequest, redirectUri = _this$props.redirectUri, disableRedirect = _this$props.disableRedirect, isMobile = _this$props.isMobile; if (typeof onClick === 'function') { onClick(e); if (e.defaultPrevented) { return; } } var params = { client_id: appId, redirect_uri: redirectUri, state: 'facebookdirect', return_scopes: returnScopes, scope: scope }; if (reAuthenticate) { params.auth_type = 'reauthenticate'; } if (reRequest) { params.auth_type = 'rerequest'; } if (isMobile && !disableRedirect) { window.location.href = '//www.facebook.com/dialog/oauth?' + (0, _utils.toQueryString)(params); } else { window.FB.login(_this.checkLoginState, { scope: scope, return_scopes: returnScopes, auth_type: params.auth_type }); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(FacebookAuth, [{ key: 'componentDidMount', value: function componentDidMount() { if (document.getElementById('facebook-jssdk')) { return; } this.setfbAsyncInit(); this.loadSdkAsynchronously(); var rootElem = document.getElementById('fb-root'); if (!rootElem) { rootElem = document.createElement('div'); rootElem.id = 'fb-root'; document.body.appendChild(rootElem); } } }, { key: 'setfbAsyncInit', value: function setfbAsyncInit() { var _this2 = this; var _props = this.props, appId = _props.appId, xfbml = _props.xfbml, cookie = _props.cookie, version = _props.version, autoLoad = _props.autoLoad; window.fbAsyncInit = function () { window.FB.init({ version: 'v' + version, appId: appId, xfbml: xfbml, cookie: cookie }); if (autoLoad || window.location.search.indexOf('facebookdirect') !== -1) { window.FB.getLoginStatus(_this2.checkLoginAfterRefresh); } }; } }, { key: 'loadSdkAsynchronously', value: function loadSdkAsynchronously() { var language = this.props.language; (function (d, s, id) { var fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) { return; } var js = d.createElement(s); js.id = id; js.src = 'https://connect.facebook.net/' + language + '/sdk.js'; fjs.parentNode.insertBefore(js, fjs); })(document, 'script', 'facebook-jssdk'); } }, { key: 'render', value: function render() { var Component = this.props.component; return _react2.default.createElement(Component, _extends({ onClick: this.click }, this.props.customProps)); } }]); return FacebookAuth; }(_react2.default.Component); FacebookAuth.defaultProps = { redirectUri: typeof window !== 'undefined' ? window.location.href : '/', scope: 'public_profile,email', onFailure: undefined, returnScopes: false, xfbml: false, cookie: false, isMobile: (0, _utils.isMobile)(), reAuthenticate: false, reRequest: false, fields: 'name,email,picture', version: '2.8', language: 'en_US', autoLoad: false, disableRedirect: false, customProps: {} }; exports.default = FacebookAuth; module.exports = exports['default'];