-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add firebase provider #68
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/sdks/browser/src/providers/firebase/firebase.error-codes.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| } |
8 changes: 8 additions & 0 deletions
8
packages/sdks/browser/src/providers/firebase/firebase.errors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
138
packages/sdks/browser/src/providers/firebase/firebase.provider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
GuillemGarciaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Sign in to the client using Google. | ||
| * @returns The void. | ||
| */ | ||
GuillemGarciaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
GuillemGarciaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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
29
packages/sdks/browser/src/providers/firebase/firebase.types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
GuillemGarciaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
30
packages/sdks/browser/src/providers/firebase/utils/index.ts
GuillemGarciaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from "./auth0"; | ||
| export * from "./firebase"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.