diff --git a/.changeset/healthy-seahorses-cheer.md b/.changeset/healthy-seahorses-cheer.md new file mode 100644 index 00000000..e7a7371d --- /dev/null +++ b/.changeset/healthy-seahorses-cheer.md @@ -0,0 +1,5 @@ +--- +'@capacitor-firebase/authentication': minor +--- + +feat(web): add method `setPersistence` diff --git a/packages/authentication/README.md b/packages/authentication/README.md index ebae5b94..8905da7b 100644 --- a/packages/authentication/README.md +++ b/packages/authentication/README.md @@ -398,6 +398,7 @@ const useEmulator = async () => { * [`sendPasswordResetEmail(...)`](#sendpasswordresetemail) * [`sendSignInLinkToEmail(...)`](#sendsigninlinktoemail) * [`setLanguageCode(...)`](#setlanguagecode) +* [`setPersistence(...)`](#setpersistence) * [`setTenantId(...)`](#settenantid) * [`signInAnonymously()`](#signinanonymously) * [`signInWithApple(...)`](#signinwithapple) @@ -959,6 +960,25 @@ Sets the user-facing language code for auth operations. -------------------- +### setPersistence(...) + +```typescript +setPersistence(options: SetPersistenceOptions) => Promise +``` + +Sets the type of persistence for the currently saved auth session. + +Only available for Web. + +| Param | Type | +| ------------- | ----------------------------------------------------------------------- | +| **`options`** | SetPersistenceOptions | + +**Since:** 5.2.0 + +-------------------- + + ### setTenantId(...) ```typescript @@ -1663,6 +1683,22 @@ bundle identifiers. | **`languageCode`** | string | BCP 47 language code. | 0.1.0 | +#### SetPersistenceOptions + +| Prop | Type | Description | Since | +| ----------------- | --------------------------------------------------- | ---------------------- | ----- | +| **`persistence`** | Persistence | The persistence types. | 5.2.0 | + + +#### Persistence + +An interface covering the possible persistence mechanism types. + +| Prop | Type | Description | +| ---------- | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **`type`** | 'SESSION' \| 'LOCAL' \| 'NONE' | Type of Persistence. - 'SESSION' is used for temporary persistence such as `sessionStorage`. - 'LOCAL' is used for long term persistence such as `localStorage` or `IndexedDB`. - 'NONE' is used for in-memory, or no persistence. | + + #### SetTenantIdOptions | Prop | Type | Description | Since | @@ -1831,6 +1867,16 @@ Callback to receive the verification ID. ### Enums +#### Persistence + +| Members | Value | Description | Since | +| -------------------- | ------------------------------- | -------------------------------------------- | ----- | +| **`IndexedDbLocal`** | 'INDEXED_DB_LOCAL' | Long term persistence using IndexedDB. | 5.2.0 | +| **`InMemory`** | 'IN_MEMORY' | No persistence. | 5.2.0 | +| **`BrowserLocal`** | 'BROWSER_LOCAL' | Long term persistence using local storage. | 5.2.0 | +| **`BrowserSession`** | 'BROWSER_SESSION' | Temporary persistence using session storage. | 5.2.0 | + + #### ProviderId | Members | Value | diff --git a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java index ddbd39a5..16df03fa 100644 --- a/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java +++ b/packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthenticationPlugin.java @@ -477,6 +477,11 @@ public void setLanguageCode(PluginCall call) { } } + @PluginMethod + public void setPersistence(PluginCall call) { + call.reject("Not available on Android."); + } + @PluginMethod public void setTenantId(PluginCall call) { try { diff --git a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.m b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.m index bdf0b8fb..18d9bf31 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.m +++ b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.m @@ -31,6 +31,7 @@ CAP_PLUGIN_METHOD(sendPasswordResetEmail, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(sendSignInLinkToEmail, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(setLanguageCode, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(setPersistence, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(setTenantId, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(signInAnonymously, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(signInWithApple, CAPPluginReturnPromise); diff --git a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift index 3df72778..3c9e3438 100644 --- a/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift +++ b/packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift @@ -337,6 +337,10 @@ public class FirebaseAuthenticationPlugin: CAPPlugin { call.resolve() } + @objc func setPersistence(_ call: CAPPluginCall) { + call.reject("Not available on iOS.") + } + @objc func setTenantId(_ call: CAPPluginCall) { guard let tenantId = call.getString("tenantId") else { call.reject(errorTenantIdMissing) diff --git a/packages/authentication/src/definitions.ts b/packages/authentication/src/definitions.ts index 3731fdc9..b0047487 100644 --- a/packages/authentication/src/definitions.ts +++ b/packages/authentication/src/definitions.ts @@ -265,6 +265,14 @@ export interface FirebaseAuthenticationPlugin { * @since 0.1.0 */ setLanguageCode(options: SetLanguageCodeOptions): Promise; + /** + * Sets the type of persistence for the currently saved auth session. + * + * Only available for Web. + * + * @since 5.2.0 + */ + setPersistence(options: SetPersistenceOptions): Promise; /** * Sets the tenant id. * @@ -609,6 +617,48 @@ export interface SetLanguageCodeOptions { languageCode: string; } +/** + * @since 5.2.0 + */ +export interface SetPersistenceOptions { + /** + * The persistence types. + * + * @since 5.2.0 + */ + persistence: Persistence; +} + +/** + * @since 5.2.0 + */ +export enum Persistence { + /** + * Long term persistence using IndexedDB. + * + * @since 5.2.0 + */ + IndexedDbLocal = 'INDEXED_DB_LOCAL', + /** + * No persistence. + * + * @since 5.2.0 + */ + InMemory = 'IN_MEMORY', + /** + * Long term persistence using local storage. + * + * @since 5.2.0 + */ + BrowserLocal = 'BROWSER_LOCAL', + /** + * Temporary persistence using session storage. + * + * @since 5.2.0 + */ + BrowserSession = 'BROWSER_SESSION', +} + /** * @since 1.1.0 */ diff --git a/packages/authentication/src/web.ts b/packages/authentication/src/web.ts index d73e8c2b..e1921a17 100644 --- a/packages/authentication/src/web.ts +++ b/packages/authentication/src/web.ts @@ -15,6 +15,8 @@ import { OAuthProvider, TwitterAuthProvider, applyActionCode, + browserLocalPersistence, + browserSessionPersistence, confirmPasswordReset, connectAuthEmulator, createUserWithEmailAndPassword, @@ -22,6 +24,8 @@ import { getAdditionalUserInfo, getAuth, getRedirectResult, + inMemoryPersistence, + indexedDBLocalPersistence, isSignInWithEmailLink, linkWithCredential, linkWithPopup, @@ -30,6 +34,7 @@ import { sendEmailVerification, sendPasswordResetEmail, sendSignInLinkToEmail, + setPersistence, signInAnonymously, signInWithCustomToken, signInWithEmailAndPassword, @@ -65,6 +70,7 @@ import type { SendPasswordResetEmailOptions, SendSignInLinkToEmailOptions, SetLanguageCodeOptions, + SetPersistenceOptions, SetTenantIdOptions, SignInResult, SignInWithCustomTokenOptions, @@ -80,7 +86,7 @@ import type { UseEmulatorOptions, User, } from './definitions'; -import { ProviderId } from './definitions'; +import { Persistence, ProviderId } from './definitions'; export class FirebaseAuthenticationWeb extends WebPlugin @@ -357,10 +363,22 @@ export class FirebaseAuthenticationWeb auth.languageCode = options.languageCode; } - public async signInAnonymously(): Promise { + public async setPersistence(options: SetPersistenceOptions): Promise { const auth = getAuth(); - const userCredential = await signInAnonymously(auth); - return this.createSignInResult(userCredential, null); + switch (options.persistence) { + case Persistence.BrowserLocal: + await setPersistence(auth, browserLocalPersistence); + break; + case Persistence.BrowserSession: + await setPersistence(auth, browserSessionPersistence); + break; + case Persistence.IndexedDbLocal: + await setPersistence(auth, indexedDBLocalPersistence); + break; + case Persistence.InMemory: + await setPersistence(auth, inMemoryPersistence); + break; + } } public async setTenantId(options: SetTenantIdOptions): Promise { @@ -368,6 +386,12 @@ export class FirebaseAuthenticationWeb auth.tenantId = options.tenantId; } + public async signInAnonymously(): Promise { + const auth = getAuth(); + const userCredential = await signInAnonymously(auth); + return this.createSignInResult(userCredential, null); + } + public async signInWithApple( options?: SignInWithOAuthOptions, ): Promise {