Skip to content

Commit f9df5dd

Browse files
committed
Refactor for displaying logos and changed README.md
1 parent fbb043e commit f9df5dd

File tree

7 files changed

+55
-31
lines changed

7 files changed

+55
-31
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ REACT_APP_FIRE_BASE_APP_ID = ''
88
REACT_APP_FIRE_BASE_MEASURMENT_ID = ''
99
REACT_APP_CLOUD_FUNCTIONS_REST_API = '<FIREBASE_CLOUD_FUNCTIONS_URL>/requestsApp'
1010
REACT_APP_LOGIN_PAGE_URL = 'http://localhost:3000/login'
11+
REACT_APP_FIRE_BASE_STORAGE_API = 'https://firebasestorage.googleapis.com/v0/b/${REACT_APP_FIRE_BASE_STORAGE_BUCKET}'

.firebaserc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"projects": {
33
"production": "react-firebase-admin-eeac2",
4-
"staging": "react-firebase-admin-eeac2"
4+
"staging": "react-firebase-admin-eeac2",
5+
"default": "react-firebase-admin-eeac2"
56
}
67
}

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ Boilerplate with React ⚛️ and Firebase 🔥designed to quickly spin up a ful
5757
- [How to translate a Text with a variable](#how-to-translate-a-text-with-a-variable)
5858
- [How to internationalize a Date](#how-to-internationalize-a-date)
5959
- [How to add your language on DatePicker](#how-to-add-your-language-on-datepicker)
60+
- [File Uploading](#file-uploading)
61+
- [Image Resize](#image-resize)
62+
- [Storage Rules](#storage-rules)
63+
- [Setting up your storage](#setting-up-your-storage)
6064
- [Contributors](#contributors)
6165
- [License](#license)
6266

@@ -147,6 +151,7 @@ React Firebase Admin is our in-house admin dashboard boilerplate, used in many o
147151
- [Sharp](https://github.com/lovell/sharp) (★ 15.8k) high performance Node.js image processing.
148152
- [Glob](https://github.com/isaacs/node-glob) (★ 6.2k) glob functionality for Node.js.
149153
- [Fs-extra](https://github.com/jprichardson/node-fs-extra) (★ 6.6k) Node.js: extra methods for the fs object like copy(), remove(), mkdirs().
154+
- [Resize Image](https://github.com/firebase/extensions/tree/master/storage-resize-images) (★ 372) Firebase Extension to create resized versions of images uploaded to Cloud Storage.
150155

151156
## Prerequisites
152157

@@ -569,6 +574,24 @@ const date = Date.now();
569574
- Add another **registerLocale** with your language as the first parameter and the import from `date-fns` as second parameter.
570575
- Place your language with its date format on **dateFormat**.
571576
577+
## File Uploading
578+
579+
### Image Resize
580+
581+
For resizing images uploaded to our storage, we decided to add [Resize Image](https://github.com/firebase/extensions/tree/master/storage-resize-images), it is a Plugin made by Firebase for image resize, this plugin allows you to set sizes for resized images, deletion of the original upload, and two optional settings, Cloud Storage path for resized images and Cache-Control header for resized images.
582+
583+
### Storage Rules
584+
585+
To make images reachable, we needed to set our storage rules to allow users to `write` on the storage made for the user logo, only if they are authenticated, but they can always `read`, this was set for saving the user´s logo path on the database.
586+
587+
### Setting up your storage
588+
589+
Paste `'https://firebasestorage.googleapis.com/v0/b/${REACT_APP_FIRE_BASE_STORAGE_BUCKET}'` into the `REACT_APP_FIRE_BASE_STORAGE_API` environment variable in your `.env` file.
590+
591+
**Should look like this**
592+
593+
`REACT_APP_FIRE_BASE_STORAGE_API = 'https://firebasestorage.googleapis.com/v0/b/${REACT_APP_FIRE_BASE_STORAGE_BUCKET}'`
594+
572595
## Contributors
573596
574597
We'd like to thank these awesome people who made this whole thing happen:

firebase.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
"destination": "/index.html"
1616
}
1717
]
18+
},
19+
"storage": {
20+
"rules": "storage.rules"
1821
}
1922
}

package-lock.json

Lines changed: 6 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/state/actions/users.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,19 @@ export const fetchUsers = () => {
7070
);
7171
};
7272
};
73+
7374
const deleteLogo = async id => {
74-
const userRef = firebase.database().ref(`users/${id}`);
75+
const basePath = 'users/';
76+
const userRef = firebase.database().ref(`${basePath}${id}`);
7577
const oldLogo = (await userRef.child('logoUrl').once('value')).val();
7678
if (oldLogo) {
79+
const fileExtension = oldLogo
80+
.split('.')
81+
.pop()
82+
.split('?')[0];
7783
await firebase
7884
.storage()
79-
.ref(oldLogo)
85+
.ref(`${basePath}${id}_200x200.${fileExtension}`)
8086
.delete();
8187
}
8288
};
@@ -88,7 +94,7 @@ export const deleteUser = id => {
8894
try {
8995
await deleteLogo(id);
9096
} catch (error) {
91-
const errorMessage = firebaseError(error.response.data.error.code);
97+
const errorMessage = firebaseError(error.code);
9298
toastr.error('', errorMessage);
9399
return dispatch(
94100
USERS_DELETE_USER_FAIL({
@@ -118,11 +124,11 @@ const uploadLogo = async (uid, file) => {
118124

119125
const fileName = `${uid}.${fileExtension}`;
120126

121-
const basePath = 'users/';
127+
await storageRef.child(`users/${fileName}`).put(file);
122128

123-
await storageRef.child(`${basePath}${fileName}`).put(file);
129+
const bucketUrl = `${process.env.REACT_APP_FIRE_BASE_STORAGE_API}`;
124130

125-
return `${basePath}${uid}_200x200.${fileExtension}`;
131+
return `${bucketUrl}/o/users%2F${uid}_200x200.${fileExtension}?alt=media`;
126132
};
127133

128134
export const createUser = ({

storage.rules

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
service firebase.storage {
2+
match /b/{bucket}/o {
3+
match /users/{imageId} {
4+
allow write: if request.auth!=null;
5+
allow read: if true;
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)