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

Multi-tenancy support #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-admin-firebase",
"name": "@rinfimate/react-admin-firebase",
"description": "A firebase data provider for the React Admin framework",
"version": "4.0.0",
"version": "4.0.3",
"peerDependencies": {
"firebase": "^9.6.4",
"react": "17.x || 18.x",
Expand All @@ -10,6 +10,7 @@
"react-router-dom": "5.x || 6.x"
},
"dependencies": {
"jwt-decode": "^3.1.2",
"lodash": "4.x",
"path-browserify": "^1.0.0",
"rxjs": "^6.5.x"
Expand All @@ -35,7 +36,7 @@
"tslint-config-prettier": "^1.18.0",
"typescript": "4.5.5"
},
"homepage": "https://github.com/benwinding/react-admin-firebase",
"homepage": "https://github.com/rinfimate/react-admin-firebase",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none of the changes in this file besides the dependency should be part of the pull request

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to make another branch on your fork say rinfimate:add-multi-tenant-support in order to only merge in changes that are necessary (you then can update this PR with the new branch)... otherwise you'll need to change your master branch to remove these changes

"email": "[email protected]",
"license": "MIT",
"scripts": {
Expand Down
27 changes: 24 additions & 3 deletions src/providers/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { messageTypes } from './../misc/messageTypes';
import { IFirebaseWrapper } from './database';
import { FirebaseWrapper } from './database/firebase/FirebaseWrapper';
import { RAFirebaseOptions } from './options';
import jwt_decode from "jwt-decode";

class AuthClient {

private fireWrapper: IFirebaseWrapper;

constructor(firebaseConfig: {}, optionsInput?: RAFirebaseOptions) {
Expand All @@ -23,9 +25,11 @@ class AuthClient {
return this.fireWrapper.authSetPersistence(persistenceInput);
}

public async HandleAuthLogin(params: { username: string; password: string }) {
const { username, password } = params;

public async HandleAuthLogin(params: { username: string; password: string, tenant?: string }) {
const { username, password, tenant } = params;
if (tenant) {
this.fireWrapper.auth().tenantId = tenant;
}
if (username && password) {
try {
const user = await this.fireWrapper.authSigninEmailPassword(
Expand Down Expand Up @@ -182,6 +186,21 @@ class AuthClient {
return null;
}
}

public async HandleGetTenant() {
try {
const user = await this.getUserLogin();
// @ts-ignore
const token = await user.getIdTokenResult();
const decodedToken:any = jwt_decode(token.token);
return decodedToken["firebase"]["tenant"];
} catch (e) {
log('HandleGetTenant: no user is logged in or tokenResult error', {
e,
});
return null;
}
}
Comment on lines +190 to +203
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add to the README on how this works? 🙏

}

export function AuthProvider(
Expand All @@ -207,6 +226,7 @@ export function AuthProvider(
getJWTSignInProvider: () => auth.HandleGetJWTSignInProvider(),
getJWTClaims: () => auth.HandleGetPermissions(),
getJWTToken: () => auth.HandleGetJWTToken(),
getTenant: () => auth.HandleGetTenant(),
};
return provider;
}
Expand All @@ -219,6 +239,7 @@ export type ReactAdminFirebaseAuthProvider = RaAuthProvider & {
getJWTSignInProvider: () => Promise<string | null>;
getJWTClaims: () => Promise<{ [key: string]: any } | null>;
getJWTToken: () => Promise<string | null>;
getTenant: () => Promise<string | null>;
};

function VerifyAuthProviderArgs(
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7241,6 +7241,11 @@ jws@^4.0.0:
jwa "^2.0.0"
safe-buffer "^5.0.1"

jwt-decode@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-3.1.2.tgz#3fb319f3675a2df0c2895c8f5e9fa4b67b04ed59"
integrity sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==

kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand Down Expand Up @@ -9037,11 +9042,6 @@ prettier-plugin-organize-imports@^3.2.2:
resolved "https://registry.yarnpkg.com/prettier-plugin-organize-imports/-/prettier-plugin-organize-imports-3.2.2.tgz#91993365e017daa5d0d28d8183179834224d8dd1"
integrity sha512-e97lE6odGSiHonHJMTYC0q0iLXQyw0u5z/PJpvP/3vRy6/Zi9kLBwFAbEGjDzIowpjQv8b+J04PDamoUSQbzGA==

prettier-plugin-organize-imports@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/prettier-plugin-organize-imports/-/prettier-plugin-organize-imports-3.2.2.tgz#91993365e017daa5d0d28d8183179834224d8dd1"
integrity sha512-e97lE6odGSiHonHJMTYC0q0iLXQyw0u5z/PJpvP/3vRy6/Zi9kLBwFAbEGjDzIowpjQv8b+J04PDamoUSQbzGA==

prettier@^2.8.3:
version "2.8.4"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.4.tgz#34dd2595629bfbb79d344ac4a91ff948694463c3"
Expand Down