diff --git a/packages/auth/__tests__/auth.test.ts b/packages/auth/__tests__/auth.test.ts index 2ef7e2bd3f..2e28ab1685 100644 --- a/packages/auth/__tests__/auth.test.ts +++ b/packages/auth/__tests__/auth.test.ts @@ -76,6 +76,7 @@ import auth, { TotpMultiFactorGenerator, TwitterAuthProvider, PhoneAuthState, + revokeAccessToken, } from '../lib'; const PasswordPolicyImpl = require('../lib/password-policy/PasswordPolicyImpl').default; @@ -310,6 +311,10 @@ describe('Auth', function () { expect(onIdTokenChanged).toBeDefined(); }); + it('`revokeAccessToken` function is properly exposed to end user', function () { + expect(revokeAccessToken).toBeDefined(); + }); + it('`sendPasswordResetEmail` function is properly exposed to end user', function () { expect(sendPasswordResetEmail).toBeDefined(); }); diff --git a/packages/auth/lib/modular/index.d.ts b/packages/auth/lib/modular/index.d.ts index 6c2e0b8487..7c0df9dfba 100644 --- a/packages/auth/lib/modular/index.d.ts +++ b/packages/auth/lib/modular/index.d.ts @@ -785,6 +785,23 @@ export interface PasswordValidationStatus { readonly passwordPolicy: PasswordPolicy; } +export interface TokenType { + getType(): string; + getValue(): string; +} + +export interface AuthorizationCodeToken extends TokenType { + getType(): string; + getValue(): string; +} + +export interface AccessToken extends TokenType { + getType(): string; + getValue(): string; +} + +export async function revokeAccessToken(auth: Auth, tokenType: TokenType): Promise; + export { AppleAuthProvider, EmailAuthProvider, diff --git a/packages/auth/lib/modular/index.js b/packages/auth/lib/modular/index.js index b9385d9c46..b3339bb2b0 100644 --- a/packages/auth/lib/modular/index.js +++ b/packages/auth/lib/modular/index.js @@ -19,6 +19,7 @@ import { getApp } from '@react-native-firebase/app'; import { fetchPasswordPolicy } from '../password-policy/passwordPolicyApi'; import { PasswordPolicyImpl } from '../password-policy/PasswordPolicyImpl'; import { MultiFactorUser } from '../multiFactor'; +import { TokenType } from '../Token'; import { MODULAR_DEPRECATION_ARG } from '@react-native-firebase/app/lib/common'; /** @@ -192,13 +193,84 @@ export function onIdTokenChanged(auth, nextOrObserver) { } /** - * Revoke the given access token, Currently only supports Apple OAuth access tokens. - * @param auth - The Auth Instance. - * @param token - The Access Token + * Revokes access tokens using different token types + * @param {Object} auth - The Firebase Auth instance + * @param {TokenType} tokenType - The token type containing revocation details + * @returns {Promise} + */ +export async function revokeAccessToken(auth, tokenType) { + if (!(tokenType instanceof TokenType)) { + throw new Error('tokenType must be an instance of TokenType or its subclasses'); + } + + const type = tokenType.getType(); + const token = tokenType.getValue(); + + switch (type) { + case 'authorization_code': + return await auth.revokeToken(token); + + case 'access_token': + // Let the SDK handle access token revocation + return await auth.revokeAccessToken(token); + + default: + throw new Error(`Unsupported token type: ${type}`); + } +} + +/** + * Base class for token types used in revocation */ -export async function revokeAccessToken(auth, token) { - throw new Error('revokeAccessToken() is only supported on Web'); -} //TO DO: Add Support +export class TokenType { + constructor(token) { + if (this.constructor === TokenType) { + throw new Error('TokenType is abstract and cannot be instantiated directly'); + } + this.token = token; + } + + /** + * Abstract method to be implemented by subclasses + * @returns {string} The type identifier for this token + */ + getType() { + throw new Error('getType() must be implemented by subclasses'); + } + + /** + * @returns {string} The token value + */ + getValue() { + return this.token; + } +} + +/** + * Authorization Code token type for Apple Sign-in revocation + */ +export class AuthorizationCodeToken extends TokenType { + constructor(authorizationCode) { + super(authorizationCode); + } + + getType() { + return 'authorization_code'; + } +} + +/** + * Access Token type for Apple API revocation + */ +export class AccessToken extends TokenType { + constructor(accessToken) { + super(accessToken); + } + + getType() { + return 'access_token'; + } +} /** * Sends a password reset email to the given email address.