Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { routingShape, sessionShape } from 'stores/propTypes';
  4. import { observer, inject } from 'mobx-react';
  5. import queryString from 'query-string';
  6.  
  7. import { getSocialUrl, socialSettings } from './settings';
  8. import messages from './messages';
  9.  
  10.  
  11. @inject('session', 'notifications', 'routing') @observer
  12. class SocialButtonProvider extends React.Component {
  13.   loginUsingSocial = async (token, type) => {
  14.     const { session, notifications, isMobile } = this.props;
  15.     let response;
  16.     try {
  17.       response = await fetch(`/api/rest-auth/${type}/`, {
  18.         method: 'POST',
  19.         headers: {
  20.           'Content-Type': 'application/json',
  21.         },
  22.         body: JSON.stringify({
  23.           access_token: token,
  24.         }),
  25.       });
  26.       if (!response.ok) {
  27.         notifications.addWarning(messages.emailIsDuplicated);
  28.       } else if (isMobile) {
  29.         const { routing: { location } } = this.props;
  30.         const nextUrl = queryString.parse(location.search).next;
  31.         window.location.assign(nextUrl);
  32.       } else {
  33.         session.updateLoginWithTokenView();
  34.       }
  35.     } catch (error) {
  36.       notifications.addWarning(messages.emailIsDuplicated);
  37.     }
  38.     return null;
  39.   };
  40.  
  41.   encodeQueryData(data) {
  42.     const queryParams = [];
  43.     Object.keys(data).forEach((key) => {
  44.       queryParams.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`);
  45.     });
  46.     return queryParams.join('&');
  47.   }
  48.  
  49.   handleSocialModal = (type) => {
  50.     const { social } = this.props;
  51.     const queryParams = socialSettings.get(type).params;
  52.     queryParams.client_id = social.clientId;
  53.     queryParams.scope = social.scope.join(' ');
  54.     const url = getSocialUrl(type, social.version) + this.encodeQueryData(queryParams);
  55.  
  56.     this.openSocialModal(url, type);
  57.   };
  58.  
  59.   openSocialModal = (url, type) => {
  60.     const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX;
  61.     const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY;
  62.  
  63.     const width = window.innerWidth;
  64.     const height = window.innerHeight;
  65.  
  66.     const systemZoom = width / window.screen.availWidth;
  67.     const left = Math.abs(width - 500) / 2 / systemZoom + dualScreenLeft;
  68.     const top = Math.abs(height - 500) / 2 / systemZoom + dualScreenTop;
  69.  
  70.     const externalWindow = window.open(url, type, `width=500, height=500, top=${top}, left=${left}`);
  71.  
  72.     window.addEventListener('focus', () => externalWindow.close());
  73.  
  74.     const interval = setInterval(() => {
  75.       const { location } = externalWindow;
  76.  
  77.       if (externalWindow.closed) {
  78.         clearInterval(interval);
  79.         window.removeEventListener('focus', () => externalWindow.close());
  80.         return;
  81.       }
  82.  
  83.       try {
  84.         const accessToken = location.href.match(/#(?:access_token)=([\S\s]*?)&/)[1];
  85.         if (accessToken) {
  86.           clearInterval(interval);
  87.           this.loginUsingSocial(accessToken, type);
  88.           externalWindow.close();
  89.         }
  90.         /* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
  91.       } catch (error) {}
  92.     }, 1500);
  93.   };
  94.  
  95.   render() {
  96.     return (
  97.       <React.Fragment>
  98.         {this.props.children(
  99.           this.handleSocialModal,
  100.         )}
  101.       </React.Fragment>
  102.     );
  103.   }
  104. }
  105.  
  106. SocialButtonProvider.propTypes = {
  107.   children: PropTypes.func.isRequired,
  108.   social: PropTypes.shape({
  109.     clientId: PropTypes.string,
  110.     version: PropTypes.string,
  111.     scope: PropTypes.array,
  112.   }),
  113.   isMobile: PropTypes.bool,
  114.   routing: routingShape,
  115.   session: sessionShape,
  116.   notifications: PropTypes.shape({
  117.     addWarning: PropTypes.func,
  118.   }),
  119. };
  120.  
  121. export default SocialButtonProvider;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement