infinity-forge
Version:
60 lines • 3.75 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGoogleLogin = useGoogleLogin;
/* eslint-disable import/export */
var react_1 = require("react");
var GoogleOAuthProvider_1 = require("../GoogleOAuthProvider.js");
function useGoogleLogin(_a) {
var _b = _a.flow, flow = _b === void 0 ? 'implicit' : _b, _c = _a.scope, scope = _c === void 0 ? '' : _c, onSuccess = _a.onSuccess, onError = _a.onError, onNonOAuthError = _a.onNonOAuthError, overrideScope = _a.overrideScope, state = _a.state, props = __rest(_a, ["flow", "scope", "onSuccess", "onError", "onNonOAuthError", "overrideScope", "state"]);
var _d = (0, GoogleOAuthProvider_1.useGoogleOAuth)(), clientId = _d.clientId, scriptLoadedSuccessfully = _d.scriptLoadedSuccessfully;
var clientRef = (0, react_1.useRef)({});
var onSuccessRef = (0, react_1.useRef)(onSuccess);
onSuccessRef.current = onSuccess;
var onErrorRef = (0, react_1.useRef)(onError);
onErrorRef.current = onError;
var onNonOAuthErrorRef = (0, react_1.useRef)(onNonOAuthError);
onNonOAuthErrorRef.current = onNonOAuthError;
(0, react_1.useEffect)(function () {
var _a, _b;
if (!scriptLoadedSuccessfully)
return;
var clientMethod = flow === 'implicit' ? 'initTokenClient' : 'initCodeClient';
var client = (_b = (_a = window === null || window === void 0 ? void 0 : window.google) === null || _a === void 0 ? void 0 : _a.accounts) === null || _b === void 0 ? void 0 : _b.oauth2[clientMethod](__assign({ client_id: clientId, scope: overrideScope ? scope : "openid profile email ".concat(scope), callback: function (response) {
var _a, _b;
if (response.error)
return (_a = onErrorRef.current) === null || _a === void 0 ? void 0 : _a.call(onErrorRef, response);
(_b = onSuccessRef.current) === null || _b === void 0 ? void 0 : _b.call(onSuccessRef, response);
}, error_callback: function (nonOAuthError) {
var _a;
(_a = onNonOAuthErrorRef.current) === null || _a === void 0 ? void 0 : _a.call(onNonOAuthErrorRef, nonOAuthError);
}, state: state }, props));
clientRef.current = client;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [clientId, scriptLoadedSuccessfully, flow, scope, state]);
var loginImplicitFlow = (0, react_1.useCallback)(function (overrideConfig) { var _a; return (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.requestAccessToken(overrideConfig); }, []);
var loginAuthCodeFlow = (0, react_1.useCallback)(function () { var _a; return (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.requestCode(); }, []);
return flow === 'implicit' ? loginImplicitFlow : loginAuthCodeFlow;
}
//# sourceMappingURL=useGoogleLogin.js.map