@asgardeo/auth-react 0.2.14 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/api.ts CHANGED
@@ -21,8 +21,8 @@ import {
21
21
  AuthClientConfig,
22
22
  BasicUserInfo,
23
23
  Config,
24
- CustomGrantConfig,
25
24
  DecodedIDTokenPayload,
25
+ FetchResponse,
26
26
  Hooks,
27
27
  HttpClientInstance,
28
28
  HttpRequestConfig,
@@ -30,6 +30,7 @@ import {
30
30
  OIDCEndpoints,
31
31
  SignInConfig
32
32
  } from "@asgardeo/auth-spa";
33
+ import { SPACustomGrantConfig } from "@asgardeo/auth-spa/src/models/request-custom-grant";
33
34
  import { AuthStateInterface } from "./models";
34
35
 
35
36
  class AuthAPI {
@@ -168,7 +169,7 @@ class AuthAPI {
168
169
  *
169
170
  * @param {HttpRequestConfig} config - The config object containing attributes necessary to send a request.
170
171
  *
171
- * @return {Promise<HttpResponse>} - Returns a Promise that resolves with the response to the request.
172
+ * @return {Promise<FetchResponse>} - Returns a Promise that resolves with the response to the request.
172
173
  */
173
174
  public async httpRequest(config: HttpRequestConfig): Promise<HttpResponse<any>> {
174
175
  return this._client.httpRequest(config);
@@ -182,7 +183,7 @@ class AuthAPI {
182
183
  *
183
184
  * @param {HttpRequestConfig[]} config - The config object containing attributes necessary to send a request.
184
185
  *
185
- * @return {Promise<HttpResponse[]>} - Returns a Promise that resolves with the responses to the requests.
186
+ * @return {Promise<FetchResponse[]>} - Returns a Promise that resolves with the responses to the requests.
186
187
  */
187
188
  public async httpRequestAll(configs: HttpRequestConfig[]): Promise<HttpResponse<any>[]> {
188
189
  return this._client.httpRequestAll(configs);
@@ -193,17 +194,17 @@ class AuthAPI {
193
194
  *
194
195
  * @param {CustomGrantRequestParams} config - The request parameters.
195
196
  *
196
- * @return {Promise<HttpResponse<any> | SignInResponse>} - A Promise that resolves with
197
+ * @return {Promise<FetchResponse<any> | SignInResponse>} - A Promise that resolves with
197
198
  * the value returned by the custom grant request.
198
199
  */
199
200
  public requestCustomGrant(
200
- config: CustomGrantConfig,
201
- callback: (response: BasicUserInfo | HttpResponse<any>) => void,
201
+ config: SPACustomGrantConfig,
202
+ callback: (response: BasicUserInfo | FetchResponse<any>) => void,
202
203
  dispatch: (state: AuthStateInterface) => void
203
- ): Promise<BasicUserInfo | HttpResponse<any>> {
204
+ ): Promise<BasicUserInfo | FetchResponse<any>> {
204
205
  return this._client
205
206
  .requestCustomGrant(config)
206
- .then((response: BasicUserInfo | HttpResponse<any>) => {
207
+ .then((response: BasicUserInfo | FetchResponse<any>) => {
207
208
  if (!response) {
208
209
  return;
209
210
  }
@@ -351,15 +352,7 @@ class AuthAPI {
351
352
  */
352
353
  public on(hook: Hooks.CustomGrant, callback: (response?: any) => void, id: string): Promise<void>;
353
354
  public on(
354
- hook:
355
- | Hooks.RevokeAccessToken
356
- | Hooks.HttpRequestError
357
- | Hooks.HttpRequestFinish
358
- | Hooks.HttpRequestStart
359
- | Hooks.HttpRequestSuccess
360
- | Hooks.Initialize
361
- | Hooks.SignIn
362
- | Hooks.SignOut,
355
+ hook: Exclude<Hooks, Hooks.CustomGrant>,
363
356
  callback: (response?: any) => void
364
357
  ): Promise<void>;
365
358
  public on(hook: Hooks, callback: (response?: any) => void, id?: string): Promise<void> {
@@ -405,9 +398,9 @@ class AuthAPI {
405
398
  email: basicUserInfo.email,
406
399
  isAuthenticated: true,
407
400
  isLoading: false,
408
- username: basicUserInfo.username,
409
401
  isSigningOut: false,
410
- sub: basicUserInfo.sub
402
+ sub: basicUserInfo.sub,
403
+ username: basicUserInfo.username
411
404
  };
412
405
 
413
406
  this.updateState(stateToUpdate);
@@ -429,8 +422,8 @@ AuthAPI.DEFAULT_STATE = {
429
422
  email: "",
430
423
  isAuthenticated: false,
431
424
  isLoading: true,
432
- username: "",
433
- sub: ""
425
+ sub: "",
426
+ username: ""
434
427
  };
435
428
 
436
429
  export default AuthAPI;
@@ -19,22 +19,22 @@
19
19
  import {
20
20
  AuthClientConfig,
21
21
  BasicUserInfo,
22
- CustomGrantConfig,
22
+ FetchResponse,
23
23
  Hooks,
24
24
  HttpRequestConfig,
25
- HttpResponse,
26
- SignInConfig,
27
- SPAUtils
25
+ SPAUtils,
26
+ SignInConfig
28
27
  } from "@asgardeo/auth-spa";
28
+ import { SPACustomGrantConfig } from "@asgardeo/auth-spa/src/models/request-custom-grant";
29
29
  import React, {
30
30
  FunctionComponent,
31
31
  PropsWithChildren,
32
+ ReactNode,
32
33
  createContext,
33
34
  useContext,
34
35
  useEffect,
35
36
  useMemo,
36
- useState,
37
- ReactNode
37
+ useState
38
38
  } from "react";
39
39
  import { AuthParams, ReactConfig } from ".";
40
40
  import AuthAPI from "./api";
@@ -44,7 +44,7 @@ import { AuthContextInterface, AuthReactConfig, AuthStateInterface } from "./mod
44
44
  * Default `AuthReactConfig` config.
45
45
  */
46
46
  const defaultConfig: Partial<ReactConfig> = {
47
- disableTrySignInSilently: false
47
+ disableTrySignInSilently: true
48
48
  };
49
49
 
50
50
  const AuthClient = new AuthAPI();
@@ -88,8 +88,8 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
88
88
  const httpRequest = (config: HttpRequestConfig) => AuthClient.httpRequest(config);
89
89
  const httpRequestAll = (configs: HttpRequestConfig[]) => AuthClient.httpRequestAll(configs);
90
90
  const requestCustomGrant = (
91
- config: CustomGrantConfig,
92
- callback?: (response: BasicUserInfo | HttpResponse<any>) => void
91
+ config: SPACustomGrantConfig,
92
+ callback?: (response: BasicUserInfo | FetchResponse<any>) => void
93
93
  ) => AuthClient.requestCustomGrant(config, callback, dispatch);
94
94
  const revokeAccessToken = () => AuthClient.revokeAccessToken(dispatch);
95
95
  const getOIDCServiceEndpoints = () => AuthClient.getOIDCServiceEndpoints();
@@ -148,9 +148,12 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
148
148
  authParams = await getAuthParams();
149
149
  }
150
150
 
151
- if (SPAUtils.hasAuthSearchParamsInURL()
151
+ const url = new URL(location.href);
152
+
153
+ if ((SPAUtils.hasAuthSearchParamsInURL()
154
+ && new URL(url.origin + url.pathname).toString() === new URL(config?.signInRedirectURL).toString())
152
155
  || authParams?.authorizationCode
153
- || new URL(location.href).searchParams.get("error") )
156
+ || url.searchParams.get("error") )
154
157
  {
155
158
  await signIn({ callOnlyOnRedirect: true }, authParams?.authorizationCode, authParams?.sessionState)
156
159
  .then(() => {
@@ -178,7 +181,7 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
178
181
  // This uses the RP iframe to get the session. Hence, will not work if 3rd party cookies are disabled.
179
182
  // If the browser has these cookies disabled, we'll not be able to retrieve the session on refreshes.
180
183
  await trySignInSilently()
181
- .then((_: BasicUserInfo | boolean) => {
184
+ .then(() => {
182
185
  // TODO: Add logs when a logger is available.
183
186
  // Tracked here https://github.com/asgardeo/asgardeo-auth-js-sdk/issues/151.
184
187
  })
package/src/models.ts CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  Config,
24
24
  CustomGrantConfig,
25
25
  DecodedIDTokenPayload,
26
+ FetchResponse,
26
27
  Hooks,
27
28
  HttpClientInstance,
28
29
  HttpRequestConfig,
@@ -95,7 +96,7 @@ export interface AuthContextInterface {
95
96
  httpRequestAll(configs: HttpRequestConfig[]): Promise<HttpResponse<any>[]>;
96
97
  requestCustomGrant(
97
98
  config: CustomGrantConfig,
98
- callback?: (response: BasicUserInfo | HttpResponse<any>) => void
99
+ callback?: (response: BasicUserInfo | FetchResponse<any>) => void
99
100
  ): void;
100
101
  revokeAccessToken(): Promise<boolean>;
101
102
  getOIDCServiceEndpoints(): Promise<OIDCEndpoints>;
@@ -111,15 +112,7 @@ export interface AuthContextInterface {
111
112
  trySignInSilently: () => Promise<boolean | BasicUserInfo>;
112
113
  on(hook: Hooks.CustomGrant, callback: (response?: any) => void, id: string): void;
113
114
  on(
114
- hook:
115
- | Hooks.RevokeAccessToken
116
- | Hooks.HttpRequestError
117
- | Hooks.HttpRequestFinish
118
- | Hooks.HttpRequestStart
119
- | Hooks.HttpRequestSuccess
120
- | Hooks.Initialize
121
- | Hooks.SignIn
122
- | Hooks.SignOut,
115
+ hook: Exclude<Hooks, Hooks.CustomGrant>,
123
116
  callback: (response?: any) => void
124
117
  ): void;
125
118
  on(hook: Hooks, callback: (response?: any) => void, id?: string): void;
@@ -16,7 +16,7 @@
16
16
  * under the License.
17
17
  */
18
18
 
19
- import React, { Suspense, FunctionComponent, PropsWithChildren, ReactElement, useEffect } from "react";
19
+ import React, { FunctionComponent, PropsWithChildren, ReactElement, Suspense, useEffect } from "react";
20
20
  import { useAuthContext } from "./authenticate";
21
21
  import { AuthenticatedComponent } from "./authenticated-component";
22
22