The Road to React with Firebase by Robin Wieruch

The Road to React with Firebase by Robin Wieruch

Author:Robin Wieruch [Robin Wieruch]
Language: eng
Format: epub
Publisher: leanpub.com
Published: 2017-12-12T00:00:00+00:00


We don’t want to grant any user the power to sign up as admin, but we’ll keep it simple for now, and you can decide which circumstances prompt you to assign roles to users later. Next, add the roles property to your user when they are created in the database. Since we need an object of roles, we’ll initialize it as an empty object and add conditional roles to it:

src/components/SignUp/index.js import React, { Component } from 'react'; import { Link, withRouter } from 'react-router-dom'; import { compose } from 'recompose'; import { withFirebase } from '../Firebase'; import * as ROUTES from '../../constants/routes'; import * as ROLES from '../../constants/roles'; ... class SignUpFormBase extends Component { ... onSubmit = event => { const { username, email, passwordOne, isAdmin } = this.state; const roles = {}; if (isAdmin) { roles[ROLES.ADMIN] = ROLES.ADMIN; } this.props.firebase .doCreateUserWithEmailAndPassword(email, passwordOne) .then(authUser => { // Create a user in your Firebase realtime database return this.props.firebase .user(authUser.user.uid) .set({ username, email, roles, }) }) .then(() => { this.setState({ ...INITIAL_STATE }); this.props.history.push(ROUTES.HOME); }) .catch(error => { this.setState({ error }); }); event.preventDefault(); }; ... } ...



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.