A redux-saga integration for firebase.
Try out the example app and browse its code.
Install with:
yarn add redux-saga-firebase
Initialize a firebase app and instanciate redux-saga-firebase:
import firebase from 'firebase';
import ReduxSagaFirebase from 'redux-saga-firebase';
const myFirebaseApp = firebase.initializeApp({
apiKey: "qosjdqsdkqpdqldkqdkfojqjpfk",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
});
const reduxSagaFirebase = new ReduxSagaFirebase(myFirebaseApp)
You can now use reduxSagaFirebase
methods in your sagas:
const authProvider = new firebase.auth.GoogleAuthProvider();
function* loginSaga() {
try {
const data = yield call(reduxSagaFirebase.login, authProvider);
yield put(loginSuccess(data));
}
catch(error) {
yield put(loginFailure(error));
}
}
export default function* rootSaga() {
yield [
takeEvery(types.LOGIN.REQUEST, loginSaga);
];
}
new ReduxSagaFirebase(firebaseApp)
*reduxSagaFirebase.login(authProvider)
*reduxSagaFirebase.logout()
reduxSagaFirebase.authChannel()
*reduxSagaFirebase.get(path)
*reduxSagaFirebase.create(path, data)
*reduxSagaFirebase.update(path, data)
*reduxSagaFirebase.patch(path, data)
*reduxSagaFirebase.delete(path)
reduxSagaFirebase.channel(path, event)
*reduxSagaFirebase.call(functionName, parameters={})
reduxSagaFirebase.messageChannel()
reduxSagaFirebase.tokenRefreshChannel()
*reduxSagaFirebase.upload(path, file, metadata)
*reduxSagaFirebase.uploadString(path, string, format, metadata)
*reduxSagaFirebase.getDownloadURL(path)
*reduxSagaFirebase.getFileMetadata(path)
*reduxSagaFirebase.updateFileMetadata(path, newMetadata)
*reduxSagaFirebase.deleteFile(path)
Instanciate ReduxSagaFirebase
.
firebaseApp
: a firebase.app.App object.
A ReduxSagaFirebase
instance.
const firebaseApp = firebase.initializeApp({
apiKey: "qdsqdqqsdmqldmqdsmlqùlm",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
});
const rsf = new ReduxSagaFirebase(firebaseApp);
Starts the login process using the specified AuthProvider. (generator)
authProvider
: a firebase.auth.AuthProvider object.
A firebase.auth.AuthCredential instance.
const authProvider = new firebase.auth.GoogleAuthProvider();
function* loginSaga() {
try {
const data = yield call(rsf.login, authProvider);
yield put(loginSuccess(data));
}
catch(error) {
yield put(loginFailure(error));
}
}
Logs the user out. (generator)
none
none
function* logoutSaga() {
try {
const data = yield call(rsf.logout);
yield put(logoutSuccess(data));
}
catch(error) {
yield put(logoutFailure(error));
}
}
Gets a redux-saga Channel which emits every user change.
none
A redux-saga Channel which emits for every user change.
function* syncUserSaga() {
const channel = yield call(rsf.authChannel);
while(true) {
const { error, user } = yield take(channel);
if (user) yield put(syncUser(user));
else yield put(syncError(error));
}
}
Returns the data at this path in firebase's database.
path
: a string
Whatever value is store at this path in the database (number, string, object, etc).
function* getTodo() {
const firstTodo = yield call(rsf.get, 'todos/1');
yield put(gotTodo(firstTodo));
}
Create a new path in the database and stores the data there.
path
: a stringdata
: any value (number, string, object, etc)
The key newly created (a string).
function* addTodo() {
const key = yield call(rsf.create, 'todos', {
done: false,
label: 'Do this',
});
// `key` is something like "-Kfn7EyLEoHax0YGoQr0"
}
Replace the value store at path
in the database with data
.
path
: a stringdata
: any value (number, string, object, etc)
none
function* updateTodo() {
yield call(rsf.update, 'todos/-Kfn7EyLEoHax0YGoQr0', {
done: true, // yay, it's done now!
label: 'Do this',
});
}
Patches the value store at path
in the database with data
. Like reduxSagaFirebase.update
but doesn't remove unmentionned keys.
path
: a stringdata
: any value (number, string, object, etc)
none
function* updateTodo() {
// With this call, no need to re-send the todo label.
yield call(rsf.patch, 'todos/-Kfn7EyLEoHax0YGoQr0', {
done: true,
});
}
Removes the value at the specified path
in the database.
path
: a string
none
function* deleteTodo() {
yield call(rsf.delete, 'todos/-Kfn7EyLEoHax0YGoQr0');
}
Returns a redux-saga Channel which emits every change at the specified path in the database.
path
: a stringevent
(default:value
): a string describing the type of event to listen for. Options includes:value
,child_added
,child_removed
,child_changed
andchild_moved
. See Reference.on documentation for more information.
A redux-saga Channel which emits every change at the specified path in the database.
function* syncTodosSaga() {
const channel = yield call(rsf.channel, 'todos');
while(true) {
const todos = yield take(channel);
yield put(syncTodos(todos));
}
}
Calls a cloud function with the given parameters. The function has to be triggered by HTTP request.
This assumes that your functions are hosted in the us-central1
region.
If this is not the case you can change the region used by setting rsf.region
:
const rsf = new ReduxSagaFirebase(...);
rsf.region = 'other-region1';
functionName
: a string representing the function name. This will be used as a pathname in the https request.parameters
(default:{}
): a javascript object describing the query parameters to use in the http request.
A javascript object (application/json
) or a string (anything else) depending on the Content-Type of the response.
function* callFunction() {
// Will call: https://us-central1-project-id.firebaseapp.com/sayHello?name=Alfred
const result = yield call(rsf.call, 'sayHello', {
name: 'Alfred'
});
// `result` is either an object or a string (depends on response's Content-Type)
}
Returns a redux-saga Channel which emits for every message received.
none
A redux-saga Channel which emits for every message received.
function* readMessages() {
const channel = rsf.messageChannel();
while(true) {
const message = yield take(channel);
yield put(showMessage(message));
}
}
Returns a redux-saga Channel which emits every time the registration token is refreshed.
none
A redux-saga Channel which emits every time the registration token is refreshed.
function* refreshToken() {
const channel = rsf.tokenRefreshChannel();
while(true) {
const token = yield take(channel);
yield put(setToken(token));
}
}
Uploads a file to cloud storage.
path
: a string representing the path of the file in the bucket.file
: aBlob
, aFile
or anUint8Array
to upload at the specifiedpath
.metadata
(optional): an UploadMetadata object.
An UploadTask object.
function* uploadFile(action) {
const task = yield call(rsf.upload, action.path, action.file);
const channel = eventChannel(emit => task.on('state_changed', emit));
yield takeEvery(channel, ...);
}
Use this to upload a raw, base64
, base64url
, or data_url
encoded string to Cloud Storage.
path
: a string representing the path of the file in the bucket.string
: a string to upload.format
(optional): a string. Available options are:base64
,base64url
, ordata_url
.metadata
(optional): an UploadMetadata object.
An UploadTask object.
function* uploadString(action) {
const task = yield call(rsf.uploadString, action.path, action.fileData, 'base64');
const channel = eventChannel(emit => task.on('state_changed', emit));
yield takeEvery(channel, ...);
}
Returns a download url for the file at the specified path.
path
: a string representing the path of the file in the bucket.
A url as a string.
function* downloadFile(action) {
const url = yield call(rsf.getDownloadURL, action.path);
yield call(fetch, url, ...);
}
path
: a string representing the path of the file in the bucket.
A FullMetadata object.
function* metadata(action) {
const metadata = yield call(rsf.getFileMetadata, action.path);
return metadata;
}
Updates the metadata for a file.
path
: a string representing the path of the file in the bucket.newMetadata
: an object with keys from the SettableMetadata interface.
A FullMetadata object.
function* setToPng(action) {
const metadata = yield call(rsf.updateFileMetadata, action.path, {
contentType: 'image/png'
});
return metadata;
}
Deletes a file.
path
: a string representing the path of the file in the bucket.
none
function* deleteFile(action) {
yield call(rsf.deleteFile, action.path);
}
- Authentication integration
- Real-time database integration
- Functions integration
- Messaging integration
- Storage integration