Repository URL to install this package:
|
Version:
0.9.0 ▾
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
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; };
exports.loadGoogleApi = loadGoogleApi;
exports.onLoadGoogleApi = onLoadGoogleApi;
exports.onOpenAuthenticationDialog = onOpenAuthenticationDialog;
exports.default = watchGoogle;
var _effects = require('redux-saga/effects');
var _reduxSaga = require('redux-saga');
var _injectScriptAsync = require('./injectScriptAsync');
var _injectScriptAsync2 = _interopRequireDefault(_injectScriptAsync);
var _api = require('./api');
var _api2 = _interopRequireDefault(_api);
var _actions = require('../actions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _marked = /*#__PURE__*/_regenerator2.default.mark(loadGoogleApi),
_marked2 = /*#__PURE__*/_regenerator2.default.mark(onLoadGoogleApi),
_marked3 = /*#__PURE__*/_regenerator2.default.mark(onOpenAuthenticationDialog),
_marked4 = /*#__PURE__*/_regenerator2.default.mark(onLoginGoogle),
_marked5 = /*#__PURE__*/_regenerator2.default.mark(watchGoogle);
function loadGoogleApi(options) {
var state;
return _regenerator2.default.wrap(function loadGoogleApi$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return (0, _effects.select)(function (globalState) {
return globalState[options.domain || 'user'];
});
case 3:
state = _context.sent;
if (state.google.apiReady) {
_context.next = 11;
break;
}
_context.next = 7;
return (0, _injectScriptAsync2.default)('https://apis.google.com/js/api.js?onload=gapiLoaded', 'gapiLoaded');
case 7:
_context.next = 9;
return new Promise(function (resolve) {
return window.gapi.load('client:auth2', resolve);
});
case 9:
_context.next = 11;
return window.gapi.client.init({
client_id: options.googleClientId + '.apps.googleusercontent.com',
scope: 'profile email openid'
});
case 11:
_context.next = 13;
return (0, _effects.put)((0, _actions.googleApiLoaded)());
case 13:
_context.next = 18;
break;
case 15:
_context.prev = 15;
_context.t0 = _context['catch'](0);
console.error(_context.t0);
case 18:
case 'end':
return _context.stop();
}
}
}, _marked, this, [[0, 15]]);
}
function onLoadGoogleApi(options) {
return _regenerator2.default.wrap(function onLoadGoogleApi$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return (0, _effects.call)(loadGoogleApi, options);
case 2:
case 'end':
return _context2.stop();
}
}
}, _marked2, this);
}
function onOpenAuthenticationDialog(options) {
return _regenerator2.default.wrap(function onOpenAuthenticationDialog$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return (0, _effects.call)(loadGoogleApi, options);
case 2:
case 'end':
return _context3.stop();
}
}
}, _marked3, this);
}
function onLoginGoogle(options) {
var GoogleAuth, googleUser, authResponse, user;
return _regenerator2.default.wrap(function onLoginGoogle$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.prev = 0;
GoogleAuth = window.gapi.auth2.getAuthInstance();
_context4.next = 4;
return GoogleAuth.signIn({
scope: 'profile email openid'
});
case 4:
googleUser = _context4.sent;
authResponse = googleUser.getAuthResponse();
_context4.next = 8;
return _api2.default.loginOrSignupWithGoogle(authResponse.access_token, options);
case 8:
user = _context4.sent;
_context4.next = 11;
return (0, _effects.call)(_api2.default.getLoggedInCookie, user.accessToken, options);
case 11:
_context4.next = 13;
return (0, _effects.put)((0, _actions.userLoaded)(user));
case 13:
if (!(user.premium && user.premium.active && user.premium.active.id)) {
_context4.next = 16;
break;
}
_context4.next = 16;
return (0, _effects.put)((0, _actions.loadTheme)());
case 16:
if (!(user.mandator && user.mandator.id)) {
_context4.next = 19;
break;
}
_context4.next = 19;
return (0, _effects.put)((0, _actions.loadBranding)());
case 19:
_context4.next = 21;
return (0, _effects.put)((0, _actions.hideAuthenticationDialog)());
case 21:
_context4.next = 23;
return (0, _reduxSaga.delay)(200);
case 23:
_context4.next = 25;
return (0, _effects.put)((0, _actions.loginGoogleSucceeded)());
case 25:
_context4.next = 31;
break;
case 27:
_context4.prev = 27;
_context4.t0 = _context4['catch'](0);
_context4.next = 31;
return (0, _effects.put)((0, _actions.loginGoogleFailed)(_context4.t0));
case 31:
case 'end':
return _context4.stop();
}
}
}, _marked4, this, [[0, 27]]);
}
function watchGoogle(options) {
var config;
return _regenerator2.default.wrap(function watchGoogle$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
config = _extends({
googleClientId: '282023944456-5f4e1g0o1441h6b6pn7fuhgj8ri8bk3d',
googleRedirectUri: 'http://localhost:3000'
}, options);
_context5.next = 3;
return (0, _effects.all)([(0, _effects.takeLatest)(_actions.ActionTypes.LOAD_GOOGLE_API, onLoadGoogleApi, config), (0, _effects.takeLatest)(_actions.ActionTypes.SHOW_LOGIN_DIALOG, onOpenAuthenticationDialog, config), (0, _effects.takeLatest)(_actions.ActionTypes.SHOW_SIGNUP_DIALOG, onOpenAuthenticationDialog, config), (0, _effects.takeLatest)(_actions.ActionTypes.LOGIN_GOOGLE, onLoginGoogle, config)]);
case 3:
return _context5.abrupt('return', _context5.sent);
case 4:
case 'end':
return _context5.stop();
}
}
}, _marked5, this);
}