Skip to content

Commit

Permalink
feat(authentication): add method setPersistence (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Sep 17, 2023
1 parent 05c5df4 commit 642f508
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-seahorses-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': minor
---

feat(web): add method `setPersistence`
46 changes: 46 additions & 0 deletions packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ const useEmulator = async () => {
* [`sendPasswordResetEmail(...)`](#sendpasswordresetemail)
* [`sendSignInLinkToEmail(...)`](#sendsigninlinktoemail)
* [`setLanguageCode(...)`](#setlanguagecode)
* [`setPersistence(...)`](#setpersistence)
* [`setTenantId(...)`](#settenantid)
* [`signInAnonymously()`](#signinanonymously)
* [`signInWithApple(...)`](#signinwithapple)
Expand Down Expand Up @@ -959,6 +960,25 @@ Sets the user-facing language code for auth operations.
--------------------


### setPersistence(...)

```typescript
setPersistence(options: SetPersistenceOptions) => Promise<void>
```

Sets the type of persistence for the currently saved auth session.

Only available for Web.

| Param | Type |
| ------------- | ----------------------------------------------------------------------- |
| **`options`** | <code><a href="#setpersistenceoptions">SetPersistenceOptions</a></code> |

**Since:** 5.2.0

--------------------


### setTenantId(...)

```typescript
Expand Down Expand Up @@ -1663,6 +1683,22 @@ bundle identifiers.
| **`languageCode`** | <code>string</code> | BCP 47 language code. | 0.1.0 |


#### SetPersistenceOptions

| Prop | Type | Description | Since |
| ----------------- | --------------------------------------------------- | ---------------------- | ----- |
| **`persistence`** | <code><a href="#persistence">Persistence</a></code> | The persistence types. | 5.2.0 |


#### Persistence

An interface covering the possible persistence mechanism types.

| Prop | Type | Description |
| ---------- | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`type`** | <code>'SESSION' \| 'LOCAL' \| 'NONE'</code> | Type of <a href="#persistence">Persistence</a>. - '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 |
Expand Down Expand Up @@ -1831,6 +1867,16 @@ Callback to receive the verification ID.
### Enums


#### Persistence

| Members | Value | Description | Since |
| -------------------- | ------------------------------- | -------------------------------------------- | ----- |
| **`IndexedDbLocal`** | <code>'INDEXED_DB_LOCAL'</code> | Long term persistence using IndexedDB. | 5.2.0 |
| **`InMemory`** | <code>'IN_MEMORY'</code> | No persistence. | 5.2.0 |
| **`BrowserLocal`** | <code>'BROWSER_LOCAL'</code> | Long term persistence using local storage. | 5.2.0 |
| **`BrowserSession`** | <code>'BROWSER_SESSION'</code> | Temporary persistence using session storage. | 5.2.0 |


#### ProviderId

| Members | Value |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ export interface FirebaseAuthenticationPlugin {
* @since 0.1.0
*/
setLanguageCode(options: SetLanguageCodeOptions): Promise<void>;
/**
* Sets the type of persistence for the currently saved auth session.
*
* Only available for Web.
*
* @since 5.2.0
*/
setPersistence(options: SetPersistenceOptions): Promise<void>;
/**
* Sets the tenant id.
*
Expand Down Expand Up @@ -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
*/
Expand Down
32 changes: 28 additions & 4 deletions packages/authentication/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import {
OAuthProvider,
TwitterAuthProvider,
applyActionCode,
browserLocalPersistence,
browserSessionPersistence,
confirmPasswordReset,
connectAuthEmulator,
createUserWithEmailAndPassword,
deleteUser,
getAdditionalUserInfo,
getAuth,
getRedirectResult,
inMemoryPersistence,
indexedDBLocalPersistence,
isSignInWithEmailLink,
linkWithCredential,
linkWithPopup,
Expand All @@ -30,6 +34,7 @@ import {
sendEmailVerification,
sendPasswordResetEmail,
sendSignInLinkToEmail,
setPersistence,
signInAnonymously,
signInWithCustomToken,
signInWithEmailAndPassword,
Expand Down Expand Up @@ -65,6 +70,7 @@ import type {
SendPasswordResetEmailOptions,
SendSignInLinkToEmailOptions,
SetLanguageCodeOptions,
SetPersistenceOptions,
SetTenantIdOptions,
SignInResult,
SignInWithCustomTokenOptions,
Expand All @@ -80,7 +86,7 @@ import type {
UseEmulatorOptions,
User,
} from './definitions';
import { ProviderId } from './definitions';
import { Persistence, ProviderId } from './definitions';

export class FirebaseAuthenticationWeb
extends WebPlugin
Expand Down Expand Up @@ -357,17 +363,35 @@ export class FirebaseAuthenticationWeb
auth.languageCode = options.languageCode;
}

public async signInAnonymously(): Promise<SignInResult> {
public async setPersistence(options: SetPersistenceOptions): Promise<void> {
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<void> {
const auth = getAuth();
auth.tenantId = options.tenantId;
}

public async signInAnonymously(): Promise<SignInResult> {
const auth = getAuth();
const userCredential = await signInAnonymously(auth);
return this.createSignInResult(userCredential, null);
}

public async signInWithApple(
options?: SignInWithOAuthOptions,
): Promise<SignInResult> {
Expand Down

0 comments on commit 642f508

Please sign in to comment.