From 7eef86551f7afe834bf4218ca1eaf163b08bd9ad Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Wed, 18 Sep 2024 16:52:57 +0100 Subject: [PATCH 1/2] Expose `exchangeNativeSocial` to `useAuth0` hook --- package-lock.json | 4 ++-- package.json | 4 ++-- src/hooks/auth0-context.ts | 9 +++++++++ src/hooks/auth0-provider.tsx | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index af2807a4..fde8b4e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "react-native-auth0", - "version": "4.0.0", + "version": "4.0.1-sm.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "react-native-auth0", - "version": "4.0.0", + "version": "4.0.1-sm.0", "license": "MIT", "dependencies": { "base-64": "^0.1.0", diff --git a/package.json b/package.json index 81bfc504..74a2dc7a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "react-native-auth0", + "name": "@speechmatics/react-native-auth0", "title": "React Native Auth0", - "version": "4.0.0", + "version": "4.0.1-sm.0", "description": "React Native toolkit for Auth0 API", "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/src/hooks/auth0-context.ts b/src/hooks/auth0-context.ts index fa24ba04..d1babd44 100644 --- a/src/hooks/auth0-context.ts +++ b/src/hooks/auth0-context.ts @@ -14,6 +14,7 @@ import { WebAuthorizeParameters, PasswordlessWithSMSOptions, ClearSessionOptions, + ExchangeNativeSocialOptions, } from '../types'; export interface Auth0ContextInterface @@ -71,6 +72,13 @@ export interface Auth0ContextInterface authorizeWithRecoveryCode: ( parameters: LoginWithRecoveryCodeOptions ) => Promise; + /** + * Exchange an external token obtained via a native social authentication solution for the user's tokens. + * See {@link Auth#exchangeNativeSocial} + */ + exchangeNativeSocial: ( + parameters: ExchangeNativeSocialOptions + ) => Promise; /** * Whether the SDK currently holds valid, unexpired credentials. * @param minTtl The minimum time in seconds that the access token should last before expiration @@ -139,6 +147,7 @@ const initialContext = { authorizeWithOOB: stub, authorizeWithOTP: stub, authorizeWithRecoveryCode: stub, + exchangeNativeSocial: stub, hasValidCredentials: stub, clearSession: stub, getCredentials: stub, diff --git a/src/hooks/auth0-provider.tsx b/src/hooks/auth0-provider.tsx index d07df5a3..58646235 100644 --- a/src/hooks/auth0-provider.tsx +++ b/src/hooks/auth0-provider.tsx @@ -14,6 +14,7 @@ import { ClearSessionOptions, ClearSessionParameters, Credentials, + ExchangeNativeSocialOptions, LoginWithEmailOptions, LoginWithOOBOptions, LoginWithOTPOptions, @@ -301,6 +302,23 @@ const Auth0Provider = ({ [client] ); + const exchangeNativeSocial = useCallback( + async (parameters: ExchangeNativeSocialOptions) => { + try { + const credentials = await client.auth.exchangeNativeSocial(parameters); + const user = getIdTokenProfileClaims(credentials.idToken); + + await client.credentialsManager.saveCredentials(credentials); + dispatch({ type: 'LOGIN_COMPLETE', user }); + return credentials; + } catch (error) { + dispatch({ type: 'ERROR', error }); + return; + } + }, + [client] + ); + const hasValidCredentials = useCallback( async (minTtl: number = 0) => { return await client.credentialsManager.hasValidCredentials(minTtl); @@ -330,6 +348,7 @@ const Auth0Provider = ({ authorizeWithOOB, authorizeWithOTP, authorizeWithRecoveryCode, + exchangeNativeSocial, hasValidCredentials, clearSession, getCredentials, @@ -346,6 +365,7 @@ const Auth0Provider = ({ authorizeWithOOB, authorizeWithOTP, authorizeWithRecoveryCode, + exchangeNativeSocial, hasValidCredentials, clearSession, getCredentials, From a6d46141f06641fae7d923f8b08e6f0acc41d27f Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Fri, 20 Sep 2024 10:31:02 +0100 Subject: [PATCH 2/2] Add revoke function to top level hook --- src/hooks/auth0-context.ts | 6 +++++ src/hooks/auth0-provider.tsx | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/hooks/auth0-context.ts b/src/hooks/auth0-context.ts index d1babd44..75e044cd 100644 --- a/src/hooks/auth0-context.ts +++ b/src/hooks/auth0-context.ts @@ -15,6 +15,7 @@ import { PasswordlessWithSMSOptions, ClearSessionOptions, ExchangeNativeSocialOptions, + RevokeOptions, } from '../types'; export interface Auth0ContextInterface @@ -113,6 +114,10 @@ export interface Auth0ContextInterface * Clears the user's credentials without clearing their web session and logs them out. */ clearCredentials: () => Promise; + /** + *Revokes an issued refresh token. See {@link Auth#revoke} + */ + revoke: (parameters: RevokeOptions) => Promise; } export interface AuthState { @@ -152,6 +157,7 @@ const initialContext = { clearSession: stub, getCredentials: stub, clearCredentials: stub, + revoke: stub, }; const Auth0Context = createContext(initialContext); diff --git a/src/hooks/auth0-provider.tsx b/src/hooks/auth0-provider.tsx index 58646235..8dc10a55 100644 --- a/src/hooks/auth0-provider.tsx +++ b/src/hooks/auth0-provider.tsx @@ -23,6 +23,7 @@ import { MultifactorChallengeOptions, PasswordlessWithEmailOptions, PasswordlessWithSMSOptions, + RevokeOptions, User, WebAuthorizeOptions, WebAuthorizeParameters, @@ -336,6 +337,40 @@ const Auth0Provider = ({ } }, [client]); +<<<<<<< HEAD +======= + const requireLocalAuthentication = useCallback( + async ( + title?: string, + description?: string, + cancelTitle?: string, + fallbackTitle?: string, + strategy = LocalAuthenticationStrategy.deviceOwnerWithBiometrics + ) => { + try { + await client.credentialsManager.requireLocalAuthentication( + title, + description, + cancelTitle, + fallbackTitle, + strategy + ); + } catch (error) { + dispatch({ type: 'ERROR', error }); + return; + } + }, + [client.credentialsManager] + ); + + const revoke = useCallback( + (parameters: RevokeOptions) => { + return client.auth.revoke(parameters); + }, + [client] + ); + +>>>>>>> b6515de (Add revoke function to top level hook) const contextValue = useMemo( () => ({ ...state, @@ -353,6 +388,11 @@ const Auth0Provider = ({ clearSession, getCredentials, clearCredentials, +<<<<<<< HEAD +======= + requireLocalAuthentication, + revoke, +>>>>>>> b6515de (Add revoke function to top level hook) }), [ state, @@ -370,6 +410,11 @@ const Auth0Provider = ({ clearSession, getCredentials, clearCredentials, +<<<<<<< HEAD +======= + requireLocalAuthentication, + revoke, +>>>>>>> b6515de (Add revoke function to top level hook) ] );