gapi-oauth-react-hooks
Advanced tools
Comparing version 3.3.1 to 3.4.0
@@ -7,3 +7,3 @@ "use strict"; | ||
var useGapiConfig = function () { | ||
return react_1.useMemo(function () { return ({ | ||
return (0, react_1.useMemo)(function () { return ({ | ||
client_id: gapi_config_logic_1.GapiConfig.ClientId, | ||
@@ -10,0 +10,0 @@ cookie_policy: 'single_host_origin', |
@@ -46,6 +46,6 @@ "use strict"; | ||
var useGapiLoading = function () { | ||
var config = use_gapi_config_hook_1.useGapiConfig(); | ||
var _a = react_1.useState('Loading'), state = _a[0], setState = _a[1]; | ||
var _b = react_1.useState(), signedUser = _b[0], setSignedUser = _b[1]; | ||
var _c = react_1.useState(), authResponse = _c[0], setAuthResponse = _c[1]; | ||
var config = (0, use_gapi_config_hook_1.useGapiConfig)(); | ||
var _a = (0, react_1.useState)('Loading'), state = _a[0], setState = _a[1]; | ||
var _b = (0, react_1.useState)(), signedUser = _b[0], setSignedUser = _b[1]; | ||
var _c = (0, react_1.useState)(), authResponse = _c[0], setAuthResponse = _c[1]; | ||
var setSignedInUser = function (auth) { | ||
@@ -55,3 +55,3 @@ if (auth.isSignedIn.get()) { | ||
setAuthResponse(currentUser.getAuthResponse()); | ||
setSignedUser(conversion_logic_1.asPlainObject(currentUser.getBasicProfile())); | ||
setSignedUser((0, conversion_logic_1.asPlainObject)(currentUser.getBasicProfile())); | ||
setState('SignedIn'); | ||
@@ -63,10 +63,10 @@ } | ||
}; | ||
react_1.useEffect(function () { | ||
(0, react_1.useEffect)(function () { | ||
var handleScriptLoaded = function () { | ||
gapi_lib_indirection_1.gapiLoad('auth2', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
(0, gapi_lib_indirection_1.gapiLoad)('auth2', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var GoogleAuth; | ||
return __generator(this, function (_a) { | ||
GoogleAuth = gapi_lib_indirection_1.gapiGetAuth2Instance(); | ||
GoogleAuth = (0, gapi_lib_indirection_1.gapiGetAuth2Instance)(); | ||
if (!GoogleAuth) { | ||
gapi_lib_indirection_1.gapiAuth2Init(config).then(function (res) { return setSignedInUser(res); }, function (_) { return setState('Errored'); }); | ||
(0, gapi_lib_indirection_1.gapiAuth2Init)(config).then(function (res) { return setSignedInUser(res); }, function (_) { return setState('Errored'); }); | ||
} | ||
@@ -83,5 +83,5 @@ else { | ||
}; | ||
resource_loading_logic_1.loadScript(document, 'google-login', 'https://apis.google.com/js/api.js', handleScriptLoaded, handleScriptLoadError); | ||
(0, resource_loading_logic_1.loadScript)(document, 'google-login', 'https://apis.google.com/js/api.js', handleScriptLoaded, handleScriptLoadError); | ||
return function () { | ||
resource_loading_logic_1.removeScript(document, 'google-login'); | ||
(0, resource_loading_logic_1.removeScript)(document, 'google-login'); | ||
}; | ||
@@ -88,0 +88,0 @@ }, [config]); |
@@ -44,3 +44,3 @@ "use strict"; | ||
var useGoogleAuth = function () { | ||
var _a = use_gapi_loading_hook_1.useGapiLoading(), state = _a.state, signedUser = _a.signedUser, authResponse = _a.authResponse, setState = _a.setState, setSignedUser = _a.setSignedUser, setAuthResponse = _a.setAuthResponse; | ||
var _a = (0, use_gapi_loading_hook_1.useGapiLoading)(), state = _a.state, signedUser = _a.signedUser, authResponse = _a.authResponse, setState = _a.setState, setSignedUser = _a.setSignedUser, setAuthResponse = _a.setAuthResponse; | ||
var handleSignIn = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -57,3 +57,3 @@ var authInstance, user, err_1; | ||
_a.trys.push([1, 3, , 4]); | ||
authInstance = gapi_lib_indirection_1.gapiGetAuth2Instance(); | ||
authInstance = (0, gapi_lib_indirection_1.gapiGetAuth2Instance)(); | ||
return [4 /*yield*/, authInstance.signIn({ prompt: 'consent' })]; | ||
@@ -63,3 +63,3 @@ case 2: | ||
setAuthResponse(user.getAuthResponse()); | ||
setSignedUser(conversion_logic_1.asPlainObject(user.getBasicProfile())); | ||
setSignedUser((0, conversion_logic_1.asPlainObject)(user.getBasicProfile())); | ||
setState('SignedIn'); | ||
@@ -84,3 +84,3 @@ return [3 /*break*/, 4]; | ||
} | ||
authInstance = gapi_lib_indirection_1.gapiGetAuth2Instance(); | ||
authInstance = (0, gapi_lib_indirection_1.gapiGetAuth2Instance)(); | ||
return [4 /*yield*/, authInstance.signOut()]; | ||
@@ -87,0 +87,0 @@ case 1: |
{ | ||
"name": "gapi-oauth-react-hooks", | ||
"version": "3.3.1", | ||
"version": "3.4.0", | ||
"repository": "https://github.com/jpb06/gapi-oauth-react-hooks.git", | ||
@@ -10,11 +10,11 @@ "author": "jpb06 <jpb.06@outlook.com>", | ||
"devDependencies": { | ||
"@testing-library/react-hooks": "^7.0.1", | ||
"@testing-library/react-hooks": "^7.0.2", | ||
"@types/gapi": "^0.0.41", | ||
"@types/gapi.auth2": "^0.0.55", | ||
"@types/jest": "^27.0.1", | ||
"@types/jest": "^27.0.2", | ||
"@types/jsdom": "^16.2.13", | ||
"@types/node": "^16.7.1", | ||
"@types/react": "^17.0.19", | ||
"@typescript-eslint/eslint-plugin": "^4.29.3", | ||
"@typescript-eslint/parser": "^4.29.3", | ||
"@types/node": "^16.10.1", | ||
"@types/react": "^17.0.24", | ||
"@typescript-eslint/eslint-plugin": "^4.31.2", | ||
"@typescript-eslint/parser": "^4.31.2", | ||
"copyfiles": "^2.4.1", | ||
@@ -24,11 +24,12 @@ "del-cli": "^4.0.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.24.1", | ||
"eslint-plugin-prettier": "^3.4.1", | ||
"jest": "^27.0.6", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-jest": "^24.4.2", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.2.2", | ||
"jsdom": "^17.0.0", | ||
"prettier": "^2.3.2", | ||
"prettier": "^2.4.1", | ||
"react": "^17.0.2", | ||
"react-test-renderer": "^17.0.2", | ||
"ts-jest": "^27.0.5", | ||
"typescript": "^4.3.5" | ||
"typescript": "^4.4.3" | ||
}, | ||
@@ -35,0 +36,0 @@ "scripts": { |
# gapi-oauth-react-hooks | ||
[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/jpb06/gapi-oauth-react-hooks) | ||
![npm bundle size](https://img.shields.io/bundlephobia/min/gapi-oauth-react-hooks) | ||
![Code quality](https://img.shields.io/codefactor/grade/github/jpb06/gapi-oauth-react-hooks?logo=codefactor) | ||
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_gapi-oauth-react-hooks&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=jpb06_gapi-oauth-react-hooks) | ||
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_gapi-oauth-react-hooks&metric=security_rating)](https://sonarcloud.io/dashboard?id=jpb06_gapi-oauth-react-hooks) | ||
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=jpb06_gapi-oauth-react-hooks&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=jpb06_gapi-oauth-react-hooks) | ||
![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/gapi-oauth-react-hooks?label=snyk%20vulnerabilities) | ||
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=jpb06_gapi-oauth-react-hooks&metric=code_smells)](https://sonarcloud.io/dashboard?id=jpb06_gapi-oauth-react-hooks) | ||
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=jpb06_gapi-oauth-react-hooks&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=jpb06_gapi-oauth-react-hooks) | ||
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jpb06_gapi-oauth-react-hooks&metric=coverage)](https://sonarcloud.io/dashboard?id=jpb06_gapi-oauth-react-hooks) | ||
![Coverage](./badges/coverage-global%20coverage.svg) | ||
@@ -15,6 +23,2 @@ ![Github workflow](https://img.shields.io/github/workflow/status/jpb06/gapi-oauth-react-hooks/checks?label=last%20workflow&logo=github-actions) | ||
## :zap: Requirements | ||
- react >= 16.8 | ||
## :zap: Installation | ||
@@ -86,32 +90,46 @@ | ||
### :diamonds: Types | ||
### 🔶 Types | ||
#### :black_circle: A type defining the states Gapi can be at | ||
#### 🌀 `GapiState` | ||
This type can have the following values: | ||
This type defines gapi state. | ||
- 'Loading' : gapi is not ready yet. | ||
- 'Errored' : an error occured while loading gapi. | ||
- 'SignedIn' : gapi is ready and a user is signed in. | ||
- 'NotSignedIn' : gapi is ready and no user is signed in. | ||
| Value | Description | | ||
| ----------- | -------------------------------------- | | ||
| Loading | gapi is not ready yet | | ||
| Errored | an error occured while loading gapi | | ||
| SignedIn | gapi is ready and a user is signed in | | ||
| NotSignedIn | gapi is ready and no user is signed in | | ||
#### :black_circle: An interface defining user data | ||
#### 🌀 `UserProfile` | ||
- id : the id of the user. | ||
- email : the user email. | ||
- familyName : the user family name. | ||
- givenName : the user given name. | ||
- name : the user name. | ||
- imageUrl: the user avatar. | ||
This type defines user data properties. | ||
### :diamonds: Functions | ||
| Property | Description | | ||
| ---------- | -------------------- | | ||
| id | the id of the user | | ||
| email | the user email | | ||
| familyName | the user family name | | ||
| givenName | the user given name | | ||
| name | the user name | | ||
| imageUrl | the user avatar | | ||
#### :black_circle: A static class to hold gapi configuration | ||
#### 🌀 `GoogleAuthHookProps` | ||
This class contains a config function that takes three parameters: | ||
This type defines what returns the `useGoogleAuth` hook. | ||
- clientId. | ||
- scope. | ||
- redirectUri. | ||
| Property | Description | | ||
| ------------ | ---------------------------- | | ||
| state | The gapi state | | ||
| signedUser | The signer user (duh) | | ||
| authResponse | The auth response | | ||
| onSignIn | A function initiating login | | ||
| onSignOut | A function initiating logout | | ||
### 🔶 Functions | ||
#### 🌀 `GapiConfig.setup` | ||
This static class contains a config function that takes three parameters. Once called, `useGoogleAuth` can be used. | ||
```javascript | ||
@@ -123,17 +141,22 @@ import { GapiConfig } from 'gapi-oauth-react-hooks'; | ||
#### :black_circle: A react hook to handle signin and signout using gapi auth2 | ||
| Parameter | Description | | ||
| ----------- | ------------------- | | ||
| clientId | The gapi client id | | ||
| scope | The requested scope | | ||
| redirectUri | The redirect Uri | | ||
This hook returns an object containing: | ||
#### 🌀 `useGoogleAuth` | ||
- state : the state of gapi. | ||
- signedUser : the user signed in, if any. | ||
- authResponse : the google auth response. | ||
- onSignIn : The signin function. | ||
- onSignOut : The signout function. | ||
This react hook handles signin and signout using gapi auth2. | ||
```javascript | ||
import { useGoogleAuth } from 'gapi-oauth-react-hooks'; | ||
import { useGoogleAuth, GoogleAuthHookProps } from 'gapi-oauth-react-hooks'; | ||
const { state, signedUser, authResponse, onSignIn, onSignOut } = | ||
useGoogleAuth(); | ||
const { | ||
state, | ||
signedUser, | ||
authResponse, | ||
onSignIn, | ||
onSignOut, | ||
}: GoogleAuthHookProps = useGoogleAuth(); | ||
``` |
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
23894
160
23