Skip to content

Marplex/flarebase-auth

Repository files navigation

Flarebase Auth

Firebase/Admin Auth Javascript Library for Cloudflare Workers


License GitHub CI NPM NPM Downloads Github

Supported operations:

  • createSessionCookie()
  • verifySessionCookie()
  • signInWithEmailAndPassword()
  • signUpWithEmailAndPassword()
  • changePassword()
  • lookupUser()

Install

npm i @marplex/flarebase-auth

Usage

Flarebase tries to use the same method names and return values as the official Firebase/Admin SDK. Sometimes, the method signature are slightly different.

Create FlarebaseAuth

import { FlarebaseAuth } from '@marplex/flarebase-auth';

const auth = new FlarebaseAuth({
  apiKey: 'Firebase api key',
  projectId: 'Firebase project id',
  privateKey: 'Firebase private key or service account private key',
  serviceAccountEmail: 'Firebase service account email',
});

Sign-in with email/pass

//Sign in with username and password
const { token, user } = await auth.signInWithEmailAndPassword(
  'my@email.com',
  'supersecurepassword'
);

const userEmail = user.email;
const refreshToken = token.refreshToken;

Sign-up with email/pass

//Sign up with username and password
const { token, user } = await auth.signUpWithEmailAndPassword(
  'my@email.com',
  'supersecurepassword'
);

const userEmail = user.email;
const refreshToken = token.refreshToken;

Create session cookies

//Create a new session cookie from the user idToken
const { token, user } = await auth.signInWithEmailAndPassword(
  'my@email.com',
  'supersecurepassword'
);

const sessionCookie = await auth.createSessionCookie(token.idToken);

Verify session cookies

auth
  .verifySessionCookie(sessionCookie)
  .then((token) => useToken(token))
  .catch((e) => console.log('Invalid session cookie'));

Cache OAuth tokens with Cloudflare KV

import { FlarebaseAuth, CloudflareKv } from '@marplex/flarebase-auth';

const auth = new FlarebaseAuth({
  apiKey: 'Firebase api key',
  projectId: 'Firebase project id',
  privateKey: 'Firebase private key or service account private key',
  serviceAccountEmail: 'Firebase service account email',
  cache: new CloudflareKv(NAMESPACE),
});

Test environment

If you want to test this library, have a look at /src/test.env.example.json. Create a new file in the same directory called test.env.json with the real values and run the tests with npm test

{
  "FIREBASE_TEST_CREDENTIALS": {
    "apiKey": "MY FIREBASE API KEY",
    "projectId": "FIREBASE PROJECT ID",
    "privateKey": "FIREBASE PRIVATE KEY OR SERVICE ACCOUNT PRIVATE KEY",
    "serviceAccountEmail": "FIREBASE SERVICE ACCOUNT EMAIL"
  },

  "FIREBASE_TEST_USER": {
    "email": "test@test.com",
    "password": "password123"
  }
}

To-do

  • Add caching support (Cloudflare KV)
  • sendEmailVerification()
  • confirmEmailVerification()
  • deleteAccount()

License

Copyright (c) 2022 Marco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.