Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication): add getIdTokenResult method #786

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/popular-cobras-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': major
---

feat: add getIdTokenResult method
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export interface FirebaseAuthenticationPlugin {
* @since 0.1.0
*/
getIdToken(options?: GetIdTokenOptions): Promise<GetIdTokenResult>;
/**
* Returns a deserialized JSON Web Token (JWT) used to identify the user to a Firebase service.
*
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
* @since 6.4.0
*/
getIdTokenResult(options?: GetIdTokenResultOptions): Promise<GetIdTokenResultResult>;
/**
* Returns the `SignInResult` from the redirect-based sign-in flow.
*
Expand Down Expand Up @@ -655,6 +661,18 @@ export interface GetIdTokenOptions {
forceRefresh: boolean;
}

/**
* @since 6.4.0
*/
export interface GetIdTokenResultOptions {
/**
* Force refresh regardless of token expiration.
*
* @since 6.4.0
*/
forceRefresh: boolean;
}

/**
* @since 0.1.0
*/
Expand All @@ -667,6 +685,38 @@ export interface GetIdTokenResult {
token: string;
}

export interface GetIdTokenResultResult {
/**
* The authentication time formatted as a UTC string.
*
* @remarks
* This is the time the user authenticated (signed in) and not the time the token was refreshed.
*/
authTime: string;
/** The ID token expiration time formatted as a UTC string. */
expirationTime: string;
/** The ID token issuance time formatted as a UTC string. */
issuedAtTime: string;
/**
* The sign-in provider through which the ID token was obtained (anonymous, custom, phone,
* password, etc).
*
* @remarks
* Note, this does not map to provider IDs.
*/
signInProvider: string | null;
/**
* The type of second factor associated with this session, provided the user was multi-factor
* authenticated (eg. phone, etc).
*/
signInSecondFactor: string | null;
/**
* The entire payload claims of the ID token including the standard reserved claims as well as
* the custom claims.
*/
claims: Record<string, unknown>;
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @since 1.1.0
*/
Expand Down
17 changes: 14 additions & 3 deletions packages/authentication/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import type {
FetchSignInMethodsForEmailResult,
FirebaseAuthenticationPlugin,
GetCurrentUserResult,
GetIdTokenResultResult,
GetIdTokenOptions,
GetIdTokenResult,
GetTenantIdResult,
Expand Down Expand Up @@ -109,8 +110,7 @@ import { Persistence, ProviderId } from './definitions';

export class FirebaseAuthenticationWeb
extends WebPlugin
implements FirebaseAuthenticationPlugin
{
implements FirebaseAuthenticationPlugin {
public static readonly AUTH_STATE_CHANGE_EVENT = 'authStateChange';
public static readonly ID_TOKEN_CHANGE_EVENT = 'idTokenChange';
public static readonly PHONE_CODE_SENT_EVENT = 'phoneCodeSent';
Expand Down Expand Up @@ -217,6 +217,17 @@ export class FirebaseAuthenticationWeb
return result;
}

public async getIdTokenResult(
options?: GetIdTokenOptions,
) {
const auth = getAuth();
if (!auth.currentUser) {
throw new Error(FirebaseAuthenticationWeb.ERROR_NO_USER_SIGNED_IN);
}
const result: GetIdTokenResultResult = await auth.currentUser.getIdTokenResult(options?.forceRefresh);
return result;
}

public async getRedirectResult(): Promise<SignInResult> {
const auth = getAuth();
const userCredential = await getRedirectResult(auth);
Expand Down Expand Up @@ -981,4 +992,4 @@ export class FirebaseAuthenticationWeb
private throwNotAvailableError(): never {
throw new Error('Not available on web.');
}
}
}
Loading