Skip to content

Commit

Permalink
add admin roles on sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Nov 16, 2018
1 parent e2cf2e7 commit 05ba3d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions src/components/SignUp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Link, withRouter } from 'react-router-dom';

import { auth, db } from '../../firebase';
import * as routes from '../../constants/routes';
import * as ROLES from '../../constants/roles';

const SignUpPage = ({ history }) => (
<div>
Expand All @@ -16,6 +17,7 @@ const INITIAL_STATE = {
email: '',
passwordOne: '',
passwordTwo: '',
isAdmin: false,
error: null,
};

Expand All @@ -27,15 +29,20 @@ class SignUpForm extends Component {
}

onSubmit = event => {
const { username, email, passwordOne } = this.state;
const { username, email, passwordOne, isAdmin } = this.state;
const roles = [];

if (isAdmin) {
roles.push(ROLES.ADMIN);
}

const { history } = this.props;

auth
.doCreateUserWithEmailAndPassword(email, passwordOne)
.then(authUser => {
// Create a user in your own accessible Firebase Database too
db.doCreateUser(authUser.user.uid, username, email)
db.doCreateUser(authUser.user.uid, username, email, roles)
.then(() => {
this.setState(() => ({ ...INITIAL_STATE }));
history.push(routes.HOME);
Expand All @@ -55,12 +62,17 @@ class SignUpForm extends Component {
this.setState({ [event.target.name]: event.target.value });
};

onChangeCheckbox = event => {
this.setState({ [event.target.name]: event.target.checked });
};

render() {
const {
username,
email,
passwordOne,
passwordTwo,
isAdmin,
error,
} = this.state;

Expand Down Expand Up @@ -100,10 +112,18 @@ class SignUpForm extends Component {
type="password"
placeholder="Confirm Password"
/>
<label>
Admin:
<input
name="isAdmin"
type="checkbox"
checked={isAdmin}
onChange={this.onChangeCheckbox}
/>
</label>
<button disabled={isInvalid} type="submit">
Sign Up
</button>

{error && <p>{error.message}</p>}
</form>
);
Expand Down
1 change: 1 addition & 0 deletions src/constants/roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ADMIN = 'ADMIN';
6 changes: 3 additions & 3 deletions src/firebase/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { db } from './firebase';

// User API

export const doCreateUser = (id, username, email) =>
export const doCreateUser = (id, username, email, roles) =>
db.ref(`users/${id}`).set({
username,
email,
roles,
});

export const onceGetUsers = () =>
db.ref('users').once('value');
export const onceGetUsers = () => db.ref('users').once('value');

// Other db APIs ...

0 comments on commit 05ba3d3

Please sign in to comment.