Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/auth/__tests__/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import auth, {
TotpMultiFactorGenerator,
TwitterAuthProvider,
PhoneAuthState,
revokeAccessToken,
} from '../lib';

const PasswordPolicyImpl = require('../lib/password-policy/PasswordPolicyImpl').default;
Expand Down Expand Up @@ -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();
});
Expand Down
17 changes: 17 additions & 0 deletions packages/auth/lib/modular/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;

export {
AppleAuthProvider,
EmailAuthProvider,
Expand Down
84 changes: 78 additions & 6 deletions packages/auth/lib/modular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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<void>}
*/
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.
Expand Down
Loading