gapi-oauth-react-hooks
Advanced tools
Comparing version 1.0.7 to 1.0.8
/// <reference types="gapi.auth2" /> | ||
/// <reference types="react" /> | ||
import { GapiState } from "../types/gapiState"; | ||
export declare const useGapiLoading: () => readonly [GapiState, gapi.auth2.BasicProfile | undefined, import("react").Dispatch<import("react").SetStateAction<gapi.auth2.BasicProfile | undefined>>, import("react").Dispatch<import("react").SetStateAction<GapiState>>]; | ||
interface UseGapiLoadingProps { | ||
state: GapiState; | ||
signedUser?: gapi.auth2.BasicProfile; | ||
setState: React.Dispatch<React.SetStateAction<GapiState>>; | ||
setSignedUser: React.Dispatch<React.SetStateAction<gapi.auth2.BasicProfile | undefined>>; | ||
} | ||
export declare const useGapiLoading: () => UseGapiLoadingProps; | ||
export {}; |
@@ -0,2 +1,39 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
import { useEffect, useState } from "react"; | ||
import { gapiAuth2Init, gapiGetAuth2Instance, gapiLoad } from "../indirection/gapi.lib.indirection"; | ||
import { loadScript, removeScript } from "../logic/resource.loading.logic"; | ||
@@ -7,8 +44,6 @@ import { useGapiConfig } from "./use.gapi.config.hook"; | ||
var _a = useState("Loading"), state = _a[0], setState = _a[1]; | ||
var _b = useState(), user = _b[0], setUser = _b[1]; | ||
var setSignedInUser = function (gapi, isMounted) { | ||
if (!isMounted) | ||
return; | ||
if (gapi.isSignedIn.get()) { | ||
setUser(gapi.currentUser.get().getBasicProfile()); | ||
var _b = useState(), signedUser = _b[0], setSignedUser = _b[1]; | ||
var setSignedInUser = function (auth) { | ||
if (auth.isSignedIn.get()) { | ||
setSignedUser(auth.currentUser.get().getBasicProfile()); | ||
setState("SignedIn"); | ||
@@ -21,14 +56,17 @@ } | ||
useEffect(function () { | ||
console.log("gapi loading use effect", config); | ||
var isMounted = true; | ||
loadScript(document, "google-login", "https://apis.google.com/js/api.js", function () { | ||
window.gapi.load("auth2", function () { | ||
var GoogleAuth = window.gapi.auth2.getAuthInstance(); | ||
if (!GoogleAuth) { | ||
window.gapi.auth2.init(config).then(function (res) { return setSignedInUser(res, isMounted); }, function (err) { return setState("Errored"); }); | ||
} | ||
else { | ||
setSignedInUser(GoogleAuth, isMounted); | ||
} | ||
}); | ||
return gapiLoad("auth2", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var GoogleAuth; | ||
return __generator(this, function (_a) { | ||
GoogleAuth = gapiGetAuth2Instance(); | ||
if (!GoogleAuth) { | ||
gapiAuth2Init(config).then(function (res) { return setSignedInUser(res); }, function (err) { return setState("Errored"); }); | ||
} | ||
else { | ||
setSignedInUser(GoogleAuth); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
}); | ||
@@ -40,3 +78,3 @@ return function () { | ||
}, [config]); | ||
return [state, user, setUser, setState]; | ||
return { state: state, signedUser: signedUser, setSignedUser: setSignedUser, setState: setState }; | ||
}; |
/// <reference types="gapi.auth2" /> | ||
export declare const useGapiLogin: () => readonly [import("..").GapiState, gapi.auth2.BasicProfile | undefined, () => Promise<void>, () => Promise<void>]; | ||
import { GapiState } from "../"; | ||
interface UseGapiLoginProps { | ||
state: GapiState; | ||
signedUser?: gapi.auth2.BasicProfile; | ||
handleGoogleSignIn: () => Promise<void>; | ||
handleGoogleSignout: () => Promise<void>; | ||
} | ||
export declare const useGapiLogin: () => UseGapiLoginProps; | ||
export {}; |
@@ -37,5 +37,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { gapiGetAuth2Instance } from "../indirection/gapi.lib.indirection"; | ||
import { useGapiLoading } from "./use.gapi.loading.hook"; | ||
export var useGapiLogin = function () { | ||
var _a = useGapiLoading(), gapiState = _a[0], signedUser = _a[1], setSignedUser = _a[2], setGapiState = _a[3]; | ||
var _a = useGapiLoading(), state = _a.state, signedUser = _a.signedUser, setSignedUser = _a.setSignedUser, setState = _a.setState; | ||
var handleGoogleSignIn = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -46,3 +47,3 @@ var authInstance, user, err_1; | ||
case 0: | ||
if (gapiState !== "NotSignedIn") | ||
if (state !== "NotSignedIn") | ||
throw new Error("gapi is not ready for sign in"); | ||
@@ -52,3 +53,3 @@ _a.label = 1; | ||
_a.trys.push([1, 3, , 4]); | ||
authInstance = window.gapi.auth2.getAuthInstance(); | ||
authInstance = gapiGetAuth2Instance(); | ||
return [4 /*yield*/, authInstance.signIn({ prompt: "consent" })]; | ||
@@ -58,8 +59,8 @@ case 2: | ||
setSignedUser(user.getBasicProfile()); | ||
setGapiState("SignedIn"); | ||
setState("SignedIn"); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
err_1 = _a.sent(); | ||
console.log("gapi login error:", err_1); | ||
setGapiState("Errored"); | ||
// console.error("gapi login error:", err); | ||
setState("Errored"); | ||
return [3 /*break*/, 4]; | ||
@@ -75,5 +76,5 @@ case 4: return [2 /*return*/]; | ||
case 0: | ||
if (gapiState !== "SignedIn") | ||
if (state !== "SignedIn") | ||
return [2 /*return*/]; | ||
authInstance = window.gapi.auth2.getAuthInstance(); | ||
authInstance = gapiGetAuth2Instance(); | ||
return [4 /*yield*/, authInstance.signOut()]; | ||
@@ -89,8 +90,8 @@ case 1: | ||
}); }; | ||
return [ | ||
gapiState, | ||
signedUser, | ||
handleGoogleSignIn, | ||
handleGoogleSignout, | ||
]; | ||
return { | ||
state: state, | ||
signedUser: signedUser, | ||
handleGoogleSignIn: handleGoogleSignIn, | ||
handleGoogleSignout: handleGoogleSignout, | ||
}; | ||
}; |
@@ -17,7 +17,6 @@ export var loadScript = function (document, id, jsSrc, callback) { | ||
export var removeScript = function (document, id) { | ||
var _a; | ||
var element = document.getElementById(id); | ||
if (element) { | ||
(_a = element.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(element); | ||
if (element && element.parentNode) { | ||
element.parentNode.removeChild(element); | ||
} | ||
}; |
{ | ||
"name": "gapi-oauth-react-hooks", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"repository": "https://github.com/jpb06/gapi-oauth-react-hooks.git", | ||
@@ -10,4 +10,11 @@ "author": "jpb06 <jpb.06@outlook.com>", | ||
"devDependencies": { | ||
"@testing-library/dom": "^7.24.3", | ||
"@testing-library/jest-dom": "^5.11.4", | ||
"@testing-library/react": "^11.0.4", | ||
"@testing-library/react-hooks": "^3.4.2", | ||
"@testing-library/user-event": "^12.1.7", | ||
"@types/gapi": "^0.0.39", | ||
"@types/gapi.auth2": "^0.0.52", | ||
"@types/jest": "^26.0.14", | ||
"@types/jsdom": "^16.2.4", | ||
"@types/node": "^14.11.2", | ||
@@ -17,3 +24,10 @@ "@types/react": "^16.9.50", | ||
"del-cli": "^3.0.1", | ||
"jest": "^26.4.2", | ||
"jest-coverage-badges": "^1.1.2", | ||
"jest-environment-jsdom-sixteen": "^1.0.3", | ||
"jsdom": "^16.4.0", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"react-test-renderer": "^16.13.1", | ||
"ts-jest": "^26.4.1", | ||
"typescript": "^4.0.3" | ||
@@ -25,4 +39,9 @@ }, | ||
"copy-readme": "copyfiles README.md ./dist/", | ||
"package-publish": "yarn build && yarn copy-package && yarn copy-readme && cd ./dist && yarn publish" | ||
"copy-badges": "copyfiles ./badges/* ./dist/", | ||
"test": "jest ---watch", | ||
"test:cov": "jest --coverage --watchAll", | ||
"test:silentcov": "jest --coverage --silent --runInBand --watchAll=false", | ||
"test:badges": "yarn test:silentcov && jest-coverage-badges --output ./badges", | ||
"package-publish": "yarn test:badges && yarn copy-badges && yarn build && yarn copy-package && yarn copy-readme && cd ./dist && yarn publish" | ||
} | ||
} |
@@ -5,2 +5,4 @@ # gapi-oauth-react-hooks | ||
![Statements](./badges/badge-statements.svg) ![Branches](./badges/badge-branches.svg) ![Functions](./badges/badge-functions.svg) ![Lines](./badges/badge-lines.svg) | ||
## Purpose | ||
@@ -42,3 +44,3 @@ | ||
```Typescript | ||
import { useGapiLogin, GapiState } from "gapi-oauth-react-hooks"; | ||
import { useGapiLogin } from "gapi-oauth-react-hooks"; | ||
@@ -58,7 +60,7 @@ interface SignedInProps { | ||
export const Login = () => { | ||
const [state, user, handleSignIn, handleSignOut] = useGapiLogin(); | ||
const {state, signedUser, handleGoogleSignIn, handleGoogleSignout} = useGapiLogin(); | ||
const display = { | ||
Loading: <>Well, gapi is being loaded...</>, | ||
SignedIn: <SignedIn user={user} onSignOut={handleSignOut} />, | ||
SignedIn: <SignedIn user={signedUser} onSignOut={handleSignOut} />, | ||
NotSignedIn: <SimpleButton onClick={handleSignIn} text="Login" />, | ||
@@ -95,6 +97,6 @@ Errored: <>Oh no!</>, | ||
This hook returns: | ||
This hook returns an object containing: | ||
- gapiState : the state of gapi. | ||
- signedUser : the user signed in. | ||
- state : the state of gapi. | ||
- signedUser : the user signed in, if any. | ||
- handleGoogleSignIn : The signin function. | ||
@@ -107,2 +109,3 @@ - handleGoogleSignout : The signout function. | ||
- 1.0.8 : Adding tests coverage. | ||
- 1.0.7 : Improving example in readme. | ||
@@ -109,0 +112,0 @@ - 1.0.6 : Moving type GapiState to its own file. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17929
18
266
114
22