UNPKG

react-gapi-auth2

Version:

React bindings for gapi.auth2 (Google's OAuth 2.0 JavaScript client)

82 lines (81 loc) 4.06 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __spreadArray = (this && this.__spreadArray) || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) to[j] = from[i]; return to; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleAuthProvider = exports.useGoogleAuthContext = void 0; /* eslint-disable react/prop-types */ // https://github.com/typescript-eslint/typescript-eslint/issues/2540 // eslint-disable-next-line no-use-before-define var react_1 = __importStar(require("react")); var loadGooglePlatform_1 = require("./loadGooglePlatform"); var GoogleAuthContext = react_1.createContext(null); var useGoogleAuthContext = function () { return react_1.useContext(GoogleAuthContext); }; exports.useGoogleAuthContext = useGoogleAuthContext; var GoogleAuthProvider = function (_a) { var children = _a.children, clientConfig = _a.clientConfig, _b = _a.disableInit, disableInit = _b === void 0 ? false : _b; var _c = react_1.useState(false), isPlatformLoaded = _c[0], setIsPlatformLoaded = _c[1]; var _d = react_1.useState(false), isAuth2Loaded = _d[0], setIsAuth2Loaded = _d[1]; var _e = react_1.useState(null), googleAuth = _e[0], setGoogleAuth = _e[1]; var _f = react_1.useState(null), errors = _f[0], setErrors = _f[1]; var addError = react_1.useCallback(function (message) { var error = new Error("react-gapi-auth2: " + message); setErrors(function (errs) { return (errs ? __spreadArray(__spreadArray([], errs), [error]) : [error]); }); }, []); // Load the Google APIs platform library to create the `gapi` object react_1.useEffect(function () { if (window.gapi) { setIsPlatformLoaded(true); } else { var cleanup = loadGooglePlatform_1.loadGooglePlatform(function () { return setIsPlatformLoaded(true); }, function (e) { return addError("Error loading Google APIs platform \n" + e.message); }); setIsPlatformLoaded(true); return cleanup; } }, [addError]); // After the platform library loads, load the `auth2` library react_1.useEffect(function () { if (isPlatformLoaded && !isAuth2Loaded) { window.gapi.load("auth2:signin2", { onerror: function () { return addError("Error loading auth2 library"); }, callback: function () { setIsAuth2Loaded(true); if (!disableInit) { gapi.auth2.init(clientConfig).then(function (googleAuth) { return setGoogleAuth(googleAuth); }, function (e) { return addError(e.error + ": " + e.details); }); } }, }); } }, [isPlatformLoaded, clientConfig, disableInit, isAuth2Loaded]); var value = react_1.useMemo(function () { return ({ googleAuth: googleAuth, isPlatformLoaded: isPlatformLoaded, isAuth2Loaded: isAuth2Loaded, errors: errors, }); }, [googleAuth, isPlatformLoaded, isAuth2Loaded, errors]); return (react_1.default.createElement(GoogleAuthContext.Provider, { value: value }, children)); }; exports.GoogleAuthProvider = GoogleAuthProvider;