From 970276fdc5921bff629e0927432f065fc31f1483 Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Wed, 18 Dec 2024 14:31:01 +0000 Subject: [PATCH] Add two new functions to top level hook: - `revoke` Exposes the Auth0 SDK function for revoking refresh tokens - `exchangeNativeSocial`: Exposes the option for a Native social exchange --- package-lock.json | 8 ++++---- package.json | 4 ++-- src/hooks/auth0-context.ts | 15 +++++++++++++++ src/hooks/auth0-provider.tsx | 37 ++++++++++++++++++++++++++++++------ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index af2807a4..13cb8256 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "react-native-auth0", - "version": "4.0.0", + "name": "@speechmatics/react-native-auth0", + "version": "4.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "react-native-auth0", - "version": "4.0.0", + "name": "@speechmatics/react-native-auth0", + "version": "4.0.1", "license": "MIT", "dependencies": { "base-64": "^0.1.0", diff --git a/package.json b/package.json index 81bfc504..884fc35b 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", "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..75e044cd 100644 --- a/src/hooks/auth0-context.ts +++ b/src/hooks/auth0-context.ts @@ -14,6 +14,8 @@ import { WebAuthorizeParameters, PasswordlessWithSMSOptions, ClearSessionOptions, + ExchangeNativeSocialOptions, + RevokeOptions, } from '../types'; export interface Auth0ContextInterface @@ -71,6 +73,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 @@ -105,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 { @@ -139,10 +152,12 @@ const initialContext = { authorizeWithOOB: stub, authorizeWithOTP: stub, authorizeWithRecoveryCode: stub, + exchangeNativeSocial: stub, hasValidCredentials: stub, 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 d07df5a3..fd8b19ea 100644 --- a/src/hooks/auth0-provider.tsx +++ b/src/hooks/auth0-provider.tsx @@ -1,9 +1,4 @@ -import React, { - useEffect, - useReducer, - useState, - PropsWithChildren, -} from 'react'; +import React, { useEffect, useReducer, PropsWithChildren } from 'react'; import { useCallback, useMemo } from 'react'; import jwtDecode from 'jwt-decode'; import PropTypes from 'prop-types'; @@ -14,6 +9,7 @@ import { ClearSessionOptions, ClearSessionParameters, Credentials, + ExchangeNativeSocialOptions, LoginWithEmailOptions, LoginWithOOBOptions, LoginWithOTPOptions, @@ -22,6 +18,7 @@ import { MultifactorChallengeOptions, PasswordlessWithEmailOptions, PasswordlessWithSMSOptions, + RevokeOptions, User, WebAuthorizeOptions, WebAuthorizeParameters, @@ -301,6 +298,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); @@ -318,6 +332,13 @@ const Auth0Provider = ({ } }, [client]); + const revoke = useCallback( + (parameters: RevokeOptions) => { + return client.auth.revoke(parameters); + }, + [client] + ); + const contextValue = useMemo( () => ({ ...state, @@ -330,10 +351,12 @@ const Auth0Provider = ({ authorizeWithOOB, authorizeWithOTP, authorizeWithRecoveryCode, + exchangeNativeSocial, hasValidCredentials, clearSession, getCredentials, clearCredentials, + revoke, }), [ state, @@ -346,10 +369,12 @@ const Auth0Provider = ({ authorizeWithOOB, authorizeWithOTP, authorizeWithRecoveryCode, + exchangeNativeSocial, hasValidCredentials, clearSession, getCredentials, clearCredentials, + revoke, ] );