1.2.3 • Published 1 year ago
firebase-auth-cfworkers v1.2.3
Supported operations:
- createSessionCookie()
- verifySessionCookie()
- signInWithEmailAndPassword()
- signUpWithEmailAndPassword()
- changePassword()
- lookupUser()
- setCustomUserClaims()
Install
npm i firebase-auth-cfworkers
Usage
Firebase tries to use the same method names and return values as the official Firebase/Admin SDK. Sometimes, the method signature are slightly different.
Create FirebaseAuth
import { FirebaseAuth } from 'firebase-auth-cfworkers';
const auth = new FirebaseAuth({
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;
Set Custom User Claims
//Set Custom User Claims
const res = await auth.setCustomUserClaims(uid,
{'admin':true}
);
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 CloudflareKv KV
import { FirebaseAuth, CloudflareKv } from 'firebase-auth-cfworkers';
const auth = new FirebaseAuth({
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),
});