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
40 changes: 39 additions & 1 deletion packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="@capacitor/cli" />

import type { PluginListenerHandle } from '@capacitor/core';
import { ParsedToken } from 'firebase/auth';
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved

declare module '@capacitor/cli' {
export interface PluginsConfig {
Expand Down Expand Up @@ -106,6 +107,11 @@ export interface FirebaseAuthenticationPlugin {
* @since 0.1.0
*/
getIdToken(options?: GetIdTokenOptions): Promise<GetIdTokenResult>;
/**
* Fetches the Firebase Auth ID Token and other helper properties for getting different data associated with the token as well as all the decoded payload claims for the currently signed-in user.
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
*
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
*/
getIdTokenResult(options?: GetIdTokenOptions): Promise<GetIdTokenInfo>;
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
/**
* Returns the `SignInResult` from the redirect-based sign-in flow.
*
Expand Down Expand Up @@ -667,6 +673,38 @@ export interface GetIdTokenResult {
token: string;
}

export interface GetIdTokenInfo extends GetIdTokenResult {
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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: ParsedToken;
josephgodwinkimani marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @since 1.1.0
*/
Expand Down Expand Up @@ -1551,4 +1589,4 @@ export enum ProviderId {
YAHOO = 'yahoo.com',
PASSWORD = 'password',
PHONE = 'phone',
}
}
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,
GetIdTokenInfo,
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: GetIdTokenInfo = 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.');
}
}
}