Skip to content

Commit

Permalink
fix: improved definition types and added a missing callback function …
Browse files Browse the repository at this point in the history
…in `TokenResponse` type (#199)

* fix: improved definition types and added a missing callback function in TokenResponse type

* chore: added patch update

* fix: re export types

---------

Co-authored-by: Alexandru Bereghici <abereghici@tripadvisor.com>
Co-authored-by: Mo'men Sherif <momensherif.2019@gmail.com>
  • Loading branch information
3 people committed Feb 23, 2023
1 parent 4559cdb commit 934e58b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-colts-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-oauth/google': patch
---

Improved definition types. Added missing error_callback function in TokenResponse type"
5 changes: 0 additions & 5 deletions .changeset/old-years-try.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/poor-beers-lie.md

This file was deleted.

85 changes: 48 additions & 37 deletions packages/@react-oauth/google/src/google-auth-window.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
interface Window {
google?: {
accounts: {
id: {
initialize: (input: IdConfiguration) => void;
prompt: (momentListener?: MomentListener) => void;
renderButton: (
parent: HTMLElement,
options: GsiButtonConfiguration,
) => void;
disableAutoSelect: () => void;
storeCredential: (
credential: { id: string; password: string },
callback?: () => void,
) => void;
cancel: () => void;
onGoogleLibraryLoad: Function;
revoke: (accessToken: string, done: () => void) => void;
};
oauth2: {
initTokenClient: (config: TokenClientConfig) => {
requestAccessToken: (
overridableClientConfig?: OverridableTokenClientConfig,
import {
IdConfiguration,
MomentListener,
GsiButtonConfiguration,
TokenClientConfig,
OverridableTokenClientConfig,
CodeClientConfig,
TokenResponse,
} from './types';
declare global {
interface Window {
google?: {
accounts: {
id: {
initialize: (input: IdConfiguration) => void;
prompt: (momentListener?: MomentListener) => void;
renderButton: (
parent: HTMLElement,
options: GsiButtonConfiguration,
) => void;
disableAutoSelect: () => void;
storeCredential: (
credential: { id: string; password: string },
callback?: () => void,
) => void;
cancel: () => void;
onGoogleLibraryLoad: Function;
revoke: (accessToken: string, done: () => void) => void;
};
initCodeClient: (config: CodeClientConfig) => {
requestCode: () => void;
oauth2: {
initTokenClient: (config: TokenClientConfig) => {
requestAccessToken: (
overridableClientConfig?: OverridableTokenClientConfig,
) => void;
};
initCodeClient: (config: CodeClientConfig) => {
requestCode: () => void;
};
hasGrantedAnyScope: (
tokenResponse: TokenResponse,
firstScope: string,
...restScopes: string[]
) => boolean;
hasGrantedAllScopes: (
tokenResponse: TokenResponse,
firstScope: string,
...restScopes: string[]
) => boolean;
revoke: (accessToken: string, done?: () => void) => void;
};
hasGrantedAnyScope: (
tokenResponse: TokenResponse,
firstScope: string,
...restScopes: string[]
) => boolean;
hasGrantedAllScopes: (
tokenResponse: TokenResponse,
firstScope: string,
...restScopes: string[]
) => boolean;
revoke: (accessToken: string, done?: () => void) => void;
};
};
};
}
}
11 changes: 1 addition & 10 deletions packages/@react-oauth/google/src/hooks/useGoogleLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import {
CodeClientConfig,
CodeResponse,
OverridableTokenClientConfig,
NonOAuthError,
} from '../types';

export type NonOAuthError = {
/**
* Some non-OAuth errors, such as the popup window is failed to open;
* or closed before an OAuth response is returned.
* https://developers.google.com/identity/oauth2/web/reference/js-reference#TokenClientConfig
* https://developers.google.com/identity/oauth2/web/reference/js-reference#CodeClientConfig
*/
type: 'popup_failed_to_open' | 'popup_closed' | 'unknown';
};

interface ImplicitFlowOptions
extends Omit<TokenClientConfig, 'client_id' | 'scope' | 'callback'> {
onSuccess?: (
Expand Down
21 changes: 18 additions & 3 deletions packages/@react-oauth/google/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type Context = 'signin' | 'signup' | 'use';
export type Context = 'signin' | 'signup' | 'use';

type UxMode = 'popup' | 'redirect';
export type UxMode = 'popup' | 'redirect';

type ErrorCode =
export type ErrorCode =
| 'invalid_request'
| 'access_denied'
| 'unauthorized_client'
Expand Down Expand Up @@ -160,6 +160,15 @@ export interface TokenResponse {
error_uri?: string;
}

export type NonOAuthError = {
/**
* Some non-OAuth errors, such as the popup window is failed to open;
* or closed before an OAuth response is returned.
* https://developers.google.com/identity/oauth2/web/reference/js-reference#TokenClientConfig
* https://developers.google.com/identity/oauth2/web/reference/js-reference#CodeClientConfig
*/
type: 'popup_failed_to_open' | 'popup_closed' | 'unknown';
};
export interface TokenClientConfig {
/**
* The client ID for your application. You can find this value in the
Expand All @@ -180,6 +189,12 @@ export interface TokenClientConfig {
*/
callback?: (response: TokenResponse) => void;

/**
* Optional. The JavaScript function that handles some non-OAuth errors,
* such as the popup window is failed to open; or closed before an OAuth response is returned.
*/
error_callback?: (nonOAuthError: NonOAuthError) => void;

/**
* Optional, defaults to 'select_account'. A space-delimited, case-sensitive list of prompts to present the user
*/
Expand Down

1 comment on commit 934e58b

@vercel
Copy link

@vercel vercel bot commented on 934e58b Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-oauth – ./

react-oauth-git-master-momensherif.vercel.app
react-oauth.vercel.app
react-oauth-momensherif.vercel.app

Please sign in to comment.