Skip to content

Commit

Permalink
Modified create user and image resize
Browse files Browse the repository at this point in the history
  • Loading branch information
TOPOFGR committed May 19, 2020
1 parent 5d62cd8 commit 0fd917a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 154 deletions.
1 change: 1 addition & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
".write": "auth !== null && auth.token.email_verified === true && ((auth.token.isAdmin === true && data.child('isAdmin').val() === false) || auth.uid === $user)",
".read": "auth !== null && auth.token.email_verified === true && ((auth.token.isAdmin === true && data.child('isAdmin').val() === false) || auth.uid === $user)"
},
".write": "auth !== null && auth.token.email_verified === true && auth.token.isAdmin === true",
".read": "auth !== null && auth.token.email_verified === true && auth.token.isAdmin === true"
}
}
Expand Down
123 changes: 16 additions & 107 deletions functions/requests/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Busboy = require('busboy');
const path = require('path');
const os = require('os');
const fs = require('fs');
const uuid = require('uuid/v4');

const router = express.Router();

Expand All @@ -24,8 +25,8 @@ const uploadImageToBucket = async uploadedImage => {
});
};

const createUserAuth = async (email, password, isAdmin) => {
const { uid } = await admin.auth().createUser({ email, password });
const createUserAuth = async (email, isAdmin) => {
const { uid } = await admin.auth().createUser({ email, password: uuid() });

await admin.auth().setCustomUserClaims(uid, {
isAdmin
Expand All @@ -34,114 +35,22 @@ const createUserAuth = async (email, password, isAdmin) => {
return uid;
};

const createUserOnDb = async (
name,
email,
location,
logoUrl,
userId,
createdAt,
isAdmin
) => {
const userData = {
name,
email,
location,
logoUrl,
createdAt,
isAdmin
};

const response = await admin
.database()
.ref(`users/${userId}`)
.set({ ...userData });

return response;
};

router.post('/', (request, response) => {
cors(request, response, () => {
const busboy = new Busboy({ headers: request.headers });

let uploadedImage = null;

let fieldData = {};

busboy.on('field', (fieldName, value) => {
fieldData = { ...fieldData, [`${fieldName}`]: value };
});

busboy.on('file', (fieldName, file, fileName, encoding, mimetype) => {
const filepath = path.join(os.tmpdir(), fileName);

uploadedImage = { file: filepath, type: mimetype, fileName };

file.pipe(fs.createWriteStream(filepath));
});

busboy.on('finish', async () => {
const { name, email, password, location, createdAt } = fieldData;

const isAdmin = JSON.parse(fieldData.isAdmin);
router.post('/', async (request, response) => {
const { email, isAdmin } = request.body;

let id;

try {
console.log('Creating user in auth and setting custom claims');
id = await createUserAuth(email, password, isAdmin);
console.log('Created user auth and setting custom claims');
} catch (error) {
console.error(
'Error while creating user in auth and setting custom claims',
error
);
return response.status(500).json({ error });
}

let logoUrl = null;

if (uploadedImage) {
try {
console.log('Uploading logo to bucket');
await uploadImageToBucket(uploadedImage);
logoUrl = `https://storage.googleapis.com/${bucket.name}/${uploadedImage.fileName}`;
console.log('Uploaded logo to bucket', logoUrl);
} catch (error) {
console.error('Error while uploading image to bucket', error);
return response.status(500).json({ error });
}
}

try {
console.log('Creating user');
await createUserOnDb(
name,
email,
location,
logoUrl,
id,
createdAt,
isAdmin
);
console.log('Created user');
} catch (error) {
console.error('Error while creating user', error);
return response.status(500).json({ error });
}
if (!email) {
return response.status(400).json({ error: { code: 'auth/invalid-email' } });
}

return response.status(201).json({
id,
name,
location,
email,
logoUrl,
isAdmin
});
});
let uid;
try {
uid = await createUserAuth(email, isAdmin);
} catch (error) {
console.error('Error while creating user', error);
return response.status(500).json({ error });
}

busboy.end(request.rawBody);
});
return response.status(200).json({ uid });
});

router.delete('/:id', async (request, response) => {
Expand Down
2 changes: 2 additions & 0 deletions src/firebase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import firebase from 'firebase/app';
import 'firebase/database';
import 'firebase/auth';
import 'firebase/storage';

const config = {
apiKey: process.env.REACT_APP_FIRE_BASE_KEY,
Expand All @@ -15,5 +16,6 @@ const config = {

firebase.initializeApp(config);
firebase.database();
firebase.storage();

export default firebase;
112 changes: 66 additions & 46 deletions src/state/actions/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,63 +113,83 @@ export const createUser = ({

const userToken = await user.getIdToken();

const body = new FormData();

if (file) {
const fileExtension = file.name.split('.')[1];

const fileName = `${uuid()}.${fileExtension}`;

body.append('logo', file, fileName);
let response;
try {
response = await axios(userToken).post('/users', { email, isAdmin });
} catch (error) {
const errorMessage = firebaseError(error.response.data.error.code);
toastr.error('', errorMessage);
return dispatch(
USERS_CREATE_USER_FAIL({
error: errorMessage
})
);
}

body.append('name', name);
body.append('location', location);
body.append('email', email);
body.append('password', uuid());
body.append('createdAt', createdAt);
body.append('isAdmin', isAdmin);
const { uid } = response.data;
let path = null;
if (file) {
const storageRef = firebase.storage().ref();

axios(userToken)
.post('/users', body)
.then(async response => {
const userCreated = response.data;
const fileExtension = file.name.split('.').pop();

const actionCodeSettings = {
url: process.env.REACT_APP_LOGIN_PAGE_URL,
handleCodeInApp: true
};

try {
await firebase
.auth()
.sendSignInLinkToEmail(email, actionCodeSettings);
} catch (error) {
const errorMessage = firebaseError(error.response.data.error.code);

return dispatch(
USERS_CREATE_USER_FAIL({
error: errorMessage
})
);
}
const fileName = `${uid}.${fileExtension}`;

toastr.success('', 'User created successfully');
return dispatch(
USERS_CREATE_USER_SUCCESS({
user: userCreated
})
);
})
.catch(error => {
const errorMessage = firebaseError(error.response.data.error.code);
const basePath = 'users/';
try {
await storageRef.child(`${basePath}${fileName}`).put(file);
} catch (error) {
const errorMessage = firebaseError(error.code);
toastr.error('', errorMessage);
return dispatch(
USERS_CREATE_USER_FAIL({
error: errorMessage
})
);
});
}
path = `${basePath}${uid}_200x200.${fileExtension}`;
}

try {
await firebase
.database()
.ref(`users/${uid}`)
.set({
name,
email,
location,
logoUrl: path,
createdAt,
isAdmin
});
} catch (error) {
const errorMessage = firebaseError(error.code);
toastr.error('', errorMessage);
return dispatch(
USERS_CREATE_USER_FAIL({
error: errorMessage
})
);
}

const actionCodeSettings = {
url: process.env.REACT_APP_LOGIN_PAGE_URL,
handleCodeInApp: true
};

try {
await firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings);
} catch (error) {
const errorMessage = firebaseError(error.code);
return dispatch(
USERS_CREATE_USER_FAIL({
error: errorMessage
})
);
}

toastr.success('', 'User created successfully');
return dispatch(USERS_CREATE_USER_SUCCESS({ user: response.data }));
};
};

Expand Down
16 changes: 15 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const FIREBASE_RESPONSE = {
USER_DISABLED: 'auth/user-disabled',
TOO_MANY_REQUESTS: 'auth/too-many-requests',
EXPIRED_ACTION_CODE: 'auth/expired-action-code',
INVALID_ACTION_CODE: 'auth/invalid-action-code'
INVALID_ACTION_CODE: 'auth/invalid-action-code',
QUOTA_EXCEEDED_STORAGE: 'storage/quota-exceeded',
UNAUTHENTICATED_STORAGE: 'storage/unauthenticated',
UNAUTHORIZED_STORAGE: 'storage/unauthorized'
};

export const firebaseError = error => {
Expand Down Expand Up @@ -44,6 +47,17 @@ export const firebaseError = error => {
errorMessage =
'The invitation link has expired, get in touch with your administrator';
break;
case FIREBASE_RESPONSE.QUOTA_EXCEEDED_STORAGE:
errorMessage =
'Internal server error, get in touch with your administrator';
break;
case FIREBASE_RESPONSE.UNAUTHENTICATED_STORAGE:
errorMessage = 'Unauthenticated, please authenticate and try again.';
break;
case FIREBASE_RESPONSE.UNAUTHORIZED_STORAGE:
errorMessage =
'Unauthoriez, you are not authorized to perform this action.';
break;
default:
errorMessage = 'Unknown error, get in touch with your administrator';
}
Expand Down

0 comments on commit 0fd917a

Please sign in to comment.