Repository URL to install this package:
|
Version:
1.3.0 ▾
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getLocale = undefined;
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
exports.onLoginGoogle = onLoginGoogle;
exports.default = watchGoogle;
var _effects = require('redux-saga/effects');
var _authHelper = require('../../helpers/authHelper');
var _actions = require('../actions');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _marked = /*#__PURE__*/_regenerator2.default.mark(onLoginGoogle),
_marked2 = /*#__PURE__*/_regenerator2.default.mark(watchGoogle);
var GOOGLE_SCOPES = 'profile email openid https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/contacts.readonly';
var getLocale = exports.getLocale = function getLocale(state) {
return state.i18n && state.i18n.locale;
};
/**
* Opens a popup to perform the OAuth 2.0 code grant flow with Google Auth API
* Reference: https://developers.google.com/identity/protocols/OAuth2WebServer
* Note: this flow CANNOT be replaced by the implicit flow. The implicit flow does not provide a refresh token,
* which is required for offline access. Without the refresh token, the third party API cannot be used
* anymore after the access token expires (e.g. the user didn't log in for a long time).
*
* Note: this uses the "Web Client Development" app on Google Console.
*
* @param {string} options.cookieDomain
* @param {string} options.googleClientId The client id of the App from https://console.developers.google.com/
* @param {string} options.googleRedirectUri Should be in the form <host>/api/v2.0/users/google-code-for-login
*/
function onLoginGoogle(options) {
var locale, csrfToken, queryParams, url, win;
return _regenerator2.default.wrap(function onLoginGoogle$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return (0, _effects.select)(getLocale);
case 2:
locale = _context.sent;
csrfToken = (0, _authHelper.createCSRFToken)(options.cookieDomain);
queryParams = {
client_id: options.googleClientId,
response_type: 'code',
redirect_uri: encodeURIComponent(options.googleRedirectUri),
scope: GOOGLE_SCOPES,
access_type: 'offline',
include_granted_scopes: true,
prompt: 'consent',
state: encodeURIComponent(JSON.stringify({
is_mobile: 'false', // we are not making the call from a native app
callbackUrl: options.googleRedirectUri, // needs to match the URI passed to Google API
redirect_uri: '/', // required by our API
oauth_anti_csrf_token_cookie: csrfToken,
locale: locale,
doodleScope: ['CALENDARS', 'CONTACTS'] // connects automatically to the user's calendar and contacts
}))
};
url = 'https://accounts.google.com/o/oauth2/v2/auth?' + Object.entries(queryParams).map(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
return key + '=' + value;
}).join('&');
win = (0, _authHelper.openPopupWindow)('connect-google-login', url);
_context.prev = 7;
_context.next = 10;
return (0, _effects.call)(_authHelper.checkIfAuthWindowClosed, win);
case 10:
_context.next = 17;
break;
case 12:
_context.prev = 12;
_context.t0 = _context['catch'](7);
_context.next = 16;
return (0, _effects.put)((0, _actions.loginGoogleCancelled)());
case 16:
return _context.abrupt('return');
case 17:
_context.prev = 17;
_context.next = 20;
return (0, _effects.put)((0, _actions.loadUser)());
case 20:
_context.next = 22;
return (0, _effects.take)(_actions.ActionTypes.USER_LOADED);
case 22:
_context.next = 24;
return (0, _effects.put)((0, _actions.loginGoogleSucceeded)());
case 24:
_context.next = 30;
break;
case 26:
_context.prev = 26;
_context.t1 = _context['catch'](17);
_context.next = 30;
return (0, _effects.put)((0, _actions.loginGoogleFailed)(_context.t1));
case 30:
case 'end':
return _context.stop();
}
}
}, _marked, this, [[7, 12], [17, 26]]);
}
function watchGoogle(options) {
return _regenerator2.default.wrap(function watchGoogle$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return (0, _effects.all)([(0, _effects.takeLatest)(_actions.ActionTypes.LOGIN_GOOGLE, onLoginGoogle, options)]);
case 2:
return _context2.abrupt('return', _context2.sent);
case 3:
case 'end':
return _context2.stop();
}
}
}, _marked2, this);
}