From 8d3b0ee1d500556ac6fc156bbe544a5e25ee9eec Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Fri, 24 Apr 2020 16:01:35 +0200 Subject: [PATCH] User Model; add User to db --- TODO.md | 1 + client/src/components/List/index.jsx | 23 +++++++++++++++ client/src/pages/Account/index.jsx | 3 -- models/User.js | 27 ++++++++++++++++++ routes/api/users.js | 42 ++++++++++++++++++---------- 5 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 models/User.js diff --git a/TODO.md b/TODO.md index 1e28437..6e1919b 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,4 @@ - [ ] Material UI - [ ] AuthSlice - [ ] Redux thunk for validation +- [x] On user creation push to server after validation diff --git a/client/src/components/List/index.jsx b/client/src/components/List/index.jsx index e1d4fb4..2885f1f 100644 --- a/client/src/components/List/index.jsx +++ b/client/src/components/List/index.jsx @@ -29,6 +29,29 @@ export default function List() { if (user) { const token = await firebase.auth.currentUser.getIdToken(); dispatch(getItemsAsync(token)); + + // push to authslice ... + //import axios from "axios"; + // import * as URL from "../../constants/urls"; + // const u = await firebase.auth.currentUser; + // console.log(u); + // await axios.post( + // URL.USERS, + // { + // username: u.displayName, + // email: u.email, + // roles: { + // ADMIN: true, + // }, + // photoUrl: u.photoURL, + // phoneNumber: u.phoneNumber, + // }, + // { + // headers: { + // authorization: `Bearer ${token}`, + // }, + // } + // ); } }); }, [dispatch, firebase.auth]); diff --git a/client/src/pages/Account/index.jsx b/client/src/pages/Account/index.jsx index 3504806..0677297 100644 --- a/client/src/pages/Account/index.jsx +++ b/client/src/pages/Account/index.jsx @@ -2,7 +2,6 @@ import React from "react"; import { Container } from "reactstrap"; import PasswordChangeForm from "../../components/PasswordChangeForm"; import PasswordForgetForm from "../../components/PasswordForgetForm"; -import { useFirebase } from "../../services/auth"; const styles = { root: { @@ -12,8 +11,6 @@ const styles = { }; const AccountPage = () => { - const auth = useFirebase(); - return (

Account

diff --git a/models/User.js b/models/User.js new file mode 100644 index 0000000..d1930d9 --- /dev/null +++ b/models/User.js @@ -0,0 +1,27 @@ +import mongoose from "mongoose"; + +const UserSchema = new mongoose.Schema({ + username: { + type: String, + required: true, + }, + email: { + type: String, + required: true, + }, + roles: { + type: Map, + of: Boolean, + }, + photoUrl: { + type: String, + default: "", + }, + phoneNumber: { + type: String, + default: "", + }, +}); + +const User = mongoose.model("user", UserSchema); +export default User; diff --git a/routes/api/users.js b/routes/api/users.js index e25d422..01d4b29 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -1,27 +1,39 @@ import express from "express"; import admin from "../../services/auth/index.js"; +import User from "../../models/User.js"; const router = express.Router(); +/** + * @route POST /api/users/ + * @access Public + * @description Creates a user + */ router.post("/", async (req, res) => { - const { - email, - firstName, - lastName, - password, - phoneNumber, - photoUrl, - } = req.body; - - const user = await admin.auth().createUser({ - email, - phoneNumber, - password, - displayName: `${firstName} ${lastName}`, - photoUrl, + const newUser = new User({ + ...req.body, }); + //TODO: verify email uniqueness + const user = await newUser.save(); + // const user = await admin.auth().createUser({ + // email, + // phoneNumber, + // password, + // displayName: `${firstName} ${lastName}`, + // photoUrl, + // }); + return res.json(user); }); +/** + * @route GET /api/users + * @description Get all users + * @access Public + */ +router.get("/", (req, res) => { + User.find().then((u) => res.json(u)); +}); + export default router;