Skip to content

Commit

Permalink
Add two new functions to top level hook:
Browse files Browse the repository at this point in the history
- `revoke` Exposes the Auth0 SDK function for revoking refresh tokens
- `exchangeNativeSocial`: Exposes the option for a Native social
  exchange
  • Loading branch information
mnemitz committed Dec 18, 2024
1 parent 1b902ca commit ea194e7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 15 additions & 0 deletions src/hooks/auth0-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
WebAuthorizeParameters,
PasswordlessWithSMSOptions,
ClearSessionOptions,
ExchangeNativeSocialOptions,
RevokeOptions,
} from '../types';

export interface Auth0ContextInterface<TUser extends User = User>
Expand Down Expand Up @@ -71,6 +73,13 @@ export interface Auth0ContextInterface<TUser extends User = User>
authorizeWithRecoveryCode: (
parameters: LoginWithRecoveryCodeOptions
) => Promise<Credentials | undefined>;
/**
* Exchange an external token obtained via a native social authentication solution for the user's tokens.
* See {@link Auth#exchangeNativeSocial}
*/
exchangeNativeSocial: (
parameters: ExchangeNativeSocialOptions
) => Promise<Credentials | undefined>;
/**
* Whether the SDK currently holds valid, unexpired credentials.
* @param minTtl The minimum time in seconds that the access token should last before expiration
Expand Down Expand Up @@ -105,6 +114,10 @@ export interface Auth0ContextInterface<TUser extends User = User>
* Clears the user's credentials without clearing their web session and logs them out.
*/
clearCredentials: () => Promise<void>;
/**
*Revokes an issued refresh token. See {@link Auth#revoke}
*/
revoke: (parameters: RevokeOptions) => Promise<void>;
}

export interface AuthState<TUser extends User = User> {
Expand Down Expand Up @@ -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<Auth0ContextInterface>(initialContext);
Expand Down
30 changes: 30 additions & 0 deletions src/hooks/auth0-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ClearSessionOptions,
ClearSessionParameters,
Credentials,
ExchangeNativeSocialOptions,
LoginWithEmailOptions,
LoginWithOOBOptions,
LoginWithOTPOptions,
Expand All @@ -22,6 +23,7 @@ import {
MultifactorChallengeOptions,
PasswordlessWithEmailOptions,
PasswordlessWithSMSOptions,
RevokeOptions,
User,
WebAuthorizeOptions,
WebAuthorizeParameters,
Expand Down Expand Up @@ -301,6 +303,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);
Expand All @@ -318,6 +337,13 @@ const Auth0Provider = ({
}
}, [client]);

const revoke = useCallback(
(parameters: RevokeOptions) => {
return client.auth.revoke(parameters);
},
[client]
);

const contextValue = useMemo(
() => ({
...state,
Expand All @@ -330,10 +356,12 @@ const Auth0Provider = ({
authorizeWithOOB,
authorizeWithOTP,
authorizeWithRecoveryCode,
exchangeNativeSocial,
hasValidCredentials,
clearSession,
getCredentials,
clearCredentials,
revoke,
}),
[
state,
Expand All @@ -346,10 +374,12 @@ const Auth0Provider = ({
authorizeWithOOB,
authorizeWithOTP,
authorizeWithRecoveryCode,
exchangeNativeSocial,
hasValidCredentials,
clearSession,
getCredentials,
clearCredentials,
revoke,
]
);

Expand Down

0 comments on commit ea194e7

Please sign in to comment.