Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/sdks/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@near-js/accounts": "^2.0.2",
"@near-js/transactions": "^2.0.2",
"elliptic": "^6.6.1",
"firebase": "^11.2.0",
"jose": "^6.0.11",
"near-api-js": "^5.1.1",
"react-native-auth0": "^5.0.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum FirebaseProviderErrorCodes {
USER_NOT_LOGGED_IN = "USER_NOT_LOGGED_IN",
FIREBASE_NOT_INITIALIZED = "FIREBASE_NOT_INITIALIZED",
INVALID_PROVIDER = "INVALID_PROVIDER",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FirebaseProviderErrorCodes } from "./firebase.error-codes";

export class FirebaseProviderError extends Error {
constructor(code: FirebaseProviderErrorCodes) {
super(code.toString());
this.name = "FirebaseProviderError";
}
}
138 changes: 138 additions & 0 deletions packages/sdks/browser/src/providers/firebase/firebase.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { initializeApp, type FirebaseApp } from "firebase/app";
import {
getAuth,
signInWithPopup,
GoogleAuthProvider,
OAuthProvider,
type Auth,
type User,
} from "firebase/auth";
import {
FirebaseProviderOptions, FirebaseRequestDelegateActionSignatureOptions, FirebaseRequestTransactionSignatureOptions,
ProviderType
} from "./firebase.types";
import { decodeJwt } from "jose";
import { SignatureRequest } from "../../signers";
import { FirebaseProviderError } from "./firebase.errors";
import { FirebaseProviderErrorCodes } from "./firebase.error-codes";
import { IFastAuthProvider } from "../../client/providers/fast-auth.provider";
import firebase from "firebase/compat/app";
import AuthProvider = firebase.auth.AuthProvider;

export class FirebaseProvider implements IFastAuthProvider {
private readonly options: FirebaseProviderOptions;
private app: FirebaseApp;
private auth: Auth;
private currentUser: User | null = null;
private googleProvider: GoogleAuthProvider;
private appleProvider: OAuthProvider;

constructor(options: FirebaseProviderOptions) {
this.options = options;
this.app = initializeApp({
apiKey: this.options.apiKey,
authDomain: this.options.authDomain,
projectId: this.options.projectId,
storageBucket: this.options.storageBucket,
messagingSenderId: this.options.messagingSenderId,
appId: this.options.appId,
});
this.auth = getAuth(this.app);
this.googleProvider = new GoogleAuthProvider();
this.appleProvider = new OAuthProvider("apple.com");
this.appleProvider.addScope("email");
this.appleProvider.addScope("name");

// Set up auth state listener
this.auth.onAuthStateChanged((user) => {
this.currentUser = user;
});
}

/**
* Check if the user is signed in.
* @returns True if the user is signed in, false otherwise.
*/
async isLoggedIn(): Promise<boolean> {
return this.currentUser !== null;
}

/**
* Sign in to the client using Google.
* @returns The void.
*/
async login(providerType: ProviderType): Promise<void> {
if (!this.auth) {
throw new FirebaseProviderError(FirebaseProviderErrorCodes.FIREBASE_NOT_INITIALIZED);
}
let provider: AuthProvider;
switch (providerType) {
case "google":
provider = this.googleProvider;
break;
case "apple":
provider = this.appleProvider;
break;
default:
throw new FirebaseProviderError(FirebaseProviderErrorCodes.INVALID_PROVIDER);
}
const result = await signInWithPopup(this.auth, provider);
this.currentUser = result.user;
}

/**
* Log out of the client.
*/
async logout(): Promise<void> {
if (!this.auth) {
throw new FirebaseProviderError(FirebaseProviderErrorCodes.FIREBASE_NOT_INITIALIZED);
}
await this.auth.signOut();
this.currentUser = null;
}

/**
* Get the path for the user.
* @returns The path for the user.
*/
async getPath(): Promise<string> {
if (!this.currentUser) {
throw new FirebaseProviderError(FirebaseProviderErrorCodes.USER_NOT_LOGGED_IN);
}
const token = await this.currentUser.getIdToken();
const { sub } = decodeJwt(token);
if (!sub) {
throw new FirebaseProviderError(FirebaseProviderErrorCodes.USER_NOT_LOGGED_IN);
}
return `jwt#${this.options.issuerUrl}#${sub}`;
}

/**
* Request a signature from the client.
* @param _ The options for the request signature.
* @returns The signature.
*/
async requestTransactionSignature(_: FirebaseRequestTransactionSignatureOptions): Promise<void> {
// TODO: This would request a jwt for the custom issuer backend, claim the token hash, store the custom jwt
throw new Error("Method not implemented.");
}

/**
* Request a delegate action signature from the client.
* @param _ The options for the request delegate action signature.
* @returns The void.
*/
async requestDelegateActionSignature(_: FirebaseRequestDelegateActionSignatureOptions): Promise<void> {
// TODO: This would request a jwt for the custom issuer backend, claim the token hash, store the custom jwt
throw new Error("Method not implemented.");
}

/**
* Get the signature request.
* @returns The signature request.
*/
async getSignatureRequest(): Promise<SignatureRequest> {
// TODO: This would recover the custom jwt, and prepare the signature request
throw new Error("Method not implemented.");
}
}
29 changes: 29 additions & 0 deletions packages/sdks/browser/src/providers/firebase/firebase.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Transaction } from "near-api-js/lib/transaction";
import { DelegateAction } from "@near-js/transactions";

export type FirebaseProviderOptions = {
apiKey: string;
authDomain: string;
projectId: string;
storageBucket: string;
messagingSenderId: string;
appId: string;
redirectUri: string;
issuerUrl: string;
};

export type FirebaseBaseRequestSignatureOptions = {
imageUrl: string;
name: string;
redirectUri?: string;
};

export type FirebaseRequestTransactionSignatureOptions = FirebaseBaseRequestSignatureOptions & {
transaction: Transaction;
};

export type FirebaseRequestDelegateActionSignatureOptions = FirebaseBaseRequestSignatureOptions & {
delegateAction: DelegateAction;
};

export type ProviderType = "google" | "apple";
2 changes: 2 additions & 0 deletions packages/sdks/browser/src/providers/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./firebase.provider";
export * from "./firebase.types";
30 changes: 30 additions & 0 deletions packages/sdks/browser/src/providers/firebase/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DelegateAction } from "@near-js/transactions";
import {
encodeTransaction as encodeTransactionNear,
Transaction,
encodeDelegateAction as encodeDelegateActionNear,
} from "near-api-js/lib/transaction";

/**
* Encode a transaction to a number array.
* @param transaction The transaction to encode.
* @returns The encoded transaction.
*/
export function encodeTransaction(transaction: Transaction): number[] {
return encodeTransactionNear(transaction).reduce((acc: number[], curr: number) => {
acc.push(curr);
return acc;
}, [] as number[]);
}

/**
* Encode a delegate action to a number array.
* @param delegateAction The delegate action to encode.
* @returns The encoded delegate action.
*/
export function encodeDelegateAction(delegateAction: DelegateAction): number[] {
return encodeDelegateActionNear(delegateAction).reduce((acc: number[], curr: number) => {
acc.push(curr);
return acc;
}, [] as number[]);
}
1 change: 1 addition & 0 deletions packages/sdks/browser/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./auth0";
export * from "./firebase";
Loading
Loading