Giter Club home page Giter Club logo

sveltekit-firebase-multi-tenancy's Introduction

sveltekit-firebase-multi-tenancy

This is an example or POC of how to use SvelteKit with Firebase Auth and Firestore and how to model your Firestore for multi-tenancy. It also includes Tailwind CSS integration as a bonus.

The app uses Firebase emulator for Firestore and Firebase Auth locally. To run do pnpm i && pnpm start. The app does not follow any recommended structure, only minimal to get things to work. There is also Firebase functions project included, but it's empty because no cloud functions are used in this example. The app uses SvelteKit demo as starting project.

App Requirements

  • Users should only be allowed to access data in the company they belong to
  • Users should only be allowed to access their own data in the top users collection

Domain Model

  • A company has many jobs to do
  • A job can only be done by one company and company's employees
  • An employee (user) belongs to one company
  • One company has may employees

What it does

  • Starts SvelteKit app and Firebase emulator in one command
  • Firestore rules are applied automatically in emulator
  • Shows how to set custom claims for users in Firebase Auth
  • Shows how create users and data in Firestore from commandline using Firebase admin
  • Shows how to get same data from Firestore in slightly different ways

What it does not

Handle any errors. Happy path all the way!

Top of the head thoughts

  • Remember that Firstore only works in the browser
  • If you want to use it on the server, for example to fetch public data, use firebase-admin lib (not included)
  • If you don't disable SSR in SvelteKit you have to use dynamic imports for Firestore
  • Firestore security rules are crucial to get right. Can't emphasize it strongly enough!
  • There is no right way to model data in Firestore, but always think data duplication and model data based on your app's views
  • If you need to do some admin stuff use Firebase functions and call them from your app. You get more freedom and security
  • I don't think that Firebase Auth works on the server, but not 100% sure. More like 95%

Seed data

To create new user and company pair in Firebase emulator run the command when the emulator is running.

$ ./create_org_and_user.js --name "Google" --email [email protected]

Do it at least twice so you get at least two companies.

IMPORTANT

In order for Firebase admin to connect to Firebase emulator you have to export a couple of system variables. See .env file.

Important files

  • create_org_and_user.js
  • src/lib/firebase.ts
  • src/lib/auth.ts
  • src/lib/store.ts
  • src/routes/__layout.svelte
  • src/routes/index.svelte

Troubleshooting

Getting this Line must be greater than or equal to 1, got -1 error? Me too and I honestly have no idea why or what it means.

Does the app crash in dev server with <Header> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules error? For me too and I have no idea why.

It's most likely some kind of Vite-related ESM error. See sveltejs/kit#2670

Disclaimer

I take no responsibility if you use the examples and something goes wrong. Use at your own risk and use your judgement.

More Knowledge

If you want to learn more interesting things feel free follow me on Twitter or step by my blog - codechips.me.

sveltekit-firebase-multi-tenancy's People

Contributors

codechips avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.