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

Create markdown docs #131

Merged
merged 4 commits into from
Nov 14, 2020
Merged
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
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: ['https://paypal.me/mikkopaderes']
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
ember-cloud-firestore-adapter
==============================================================================

Unofficial Ember Data Adapter and Serializer for Cloud Firestore
This is an unofficial Ember Data Adapter and Serializer for Cloud Firestore. It's completely unrelated to [EmberFire](https://github.com/firebase/emberfire) but its purpose is of the same.

Features
------------------------------------------------------------------------------

- **Customizable data structure** - There's an opinionated default on how your data will be structured but there's enough API to make it fit to your existing ones
- **Realtime bindings** - Listen to realtime updates easily
- **Authentication** - Integrate [Firebase Authentication](https://firebase.google.com/products/auth/) powered by [Ember Simple Auth](https://github.com/simplabs/ember-simple-auth)
- **FastBoot support** - Perform server-side rendering to speed up your boot time
- **Firebase Emulator** - Develop and test your app using the [Firebase Local Emulator Suite](https://firebase.google.com/docs/emulator-suite)

Why Was This Built?
------------------------------------------------------------------------------

This was built becase EmberFire development is super slow or may even be abandoned by now.

In order to continue development with Ember and Cloud Firestore, I had to build this addon and opted to make it generic enough to be used by other developers too.

Compatibility
------------------------------------------------------------------------------
Expand All @@ -11,7 +26,6 @@ Compatibility
* Ember CLI v2.13 or above
* Node.js v10 or above


Installation
------------------------------------------------------------------------------

Expand All @@ -27,10 +41,10 @@ Once you've installed it, you can now install the addon itself:
ember install ember-cloud-firestore-adapter
```

Usage
Getting Started
------------------------------------------------------------------------------

Checkout the docs [here](https://mikkopaderes.github.io/ember-cloud-firestore-adapter).
Checkout the docs [here](docs/getting-started.md).

Contributing
------------------------------------------------------------------------------
Expand Down
92 changes: 92 additions & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Authentication

The adapter supports Firebase authentication through the [ember-simple-auth](https://github.com/simplabs/ember-simple-auth) addon.

## Signing in

Using the `session` service provided by ember-simple-auth, a callback must be passed-in which will be responsible for authenticating the user.

The callback will have the [`firebase.auth.Auth`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth) as a param. Use this to authenticate the user using any of the providers available. It **must** also return a promise that will resolve to an instance of [`firebase.auth.UserCredential`](https://firebase.google.com/docs/reference/js/firebase.auth#usercredential).

```javascript
this.session.authenticate('authenticator:firebase', (auth) => {
return auth.signInWithEmailAndPassword('my_email@gmail.com', 'my_password');
});
```

## Signing out

Also using the `session` service provided by ember-simple-auth, we just call `invalidate()`.

```javascript
this.session.invalidate();
```

## FastBoot

Authentication in FastBoot is possible through service worker and a [custom authentication system](https://firebase.google.com/docs/auth/web/custom-auth). You will have to do some setup on both your Ember app and server.

### Setup your Ember app

1. Install the `ember-service-worker` addon
2. Create `app/session-stores/application.js` file

The contents of the file should be something like this:

```javascript
import Firebase from 'ember-cloud-firestore-adapter/session-stores/firebase';

export default Firebase.extend();
```

The built-in service worker of this addon (powered by `ember-service-worker`) will intercept all `fetch` requests in order to add the result of [`getIdToken()`](https://firebase.google.com/docs/reference/js/firebase.User#getidtoken) in the request `Header`. This means that when a user visits your app, their ID token would be part of the request in which your server would process this for authentication.

### Setup your server

To continue with the server authentication process, you will need to:

1. Verify the ID token via [`verifyIdToken()`](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#verifyidtoken)
2. Create the custom token when the verification succeeds via [`createCustomToken()`](https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth-1#createcustomtoken).
3. Set the request's `Authorization` header to `Bearer: <created_custom_token>`.

That new header should now be picked up by your FastBoot app and `ember-cloud-firestore-adapter` would handle the rest through [`signInWithCustomToken()`](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signinwithcustomtoken).

Here's an example Express server that does the above in Cloud Functions for Firebase:

```javascript
const express = require('express');

const admin = require('firebase-admin');
const fastbootMiddleware = require('fastboot-express-middleware');
const functions = require('firebase-functions');

const app = express();

const distPath = 'app';

admin.initializeApp();

app.get('/*', async (req, res, next) => {
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
try {
const idToken = req.headers.authorization.split('Bearer ')[1];
const auth = admin.auth();
const decodedIdToken = await auth.verifyIdToken(idToken);
const { uid } = decodedIdToken;
const customToken = await auth.createCustomToken(uid);

req.headers.authorization = `Bearer ${customToken}`;
} catch (error) {}
}

next();
}, fastbootMiddleware(distPath));

app.use(express.static(distPath));

exports.app = functions.https.onRequest(app);
```

---

[Next: Testing »](testing.md)
Loading