User Model; add User to db

This commit is contained in:
Ruidy Nemausat 2020-04-24 16:01:35 +02:00
parent 40ccfaf90f
commit 8d3b0ee1d5
5 changed files with 78 additions and 18 deletions

View file

@ -6,3 +6,4 @@
- [ ] Material UI - [ ] Material UI
- [ ] AuthSlice - [ ] AuthSlice
- [ ] Redux thunk for validation - [ ] Redux thunk for validation
- [x] On user creation push to server after validation

View file

@ -29,6 +29,29 @@ export default function List() {
if (user) { if (user) {
const token = await firebase.auth.currentUser.getIdToken(); const token = await firebase.auth.currentUser.getIdToken();
dispatch(getItemsAsync(token)); 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]); }, [dispatch, firebase.auth]);

View file

@ -2,7 +2,6 @@ import React from "react";
import { Container } from "reactstrap"; import { Container } from "reactstrap";
import PasswordChangeForm from "../../components/PasswordChangeForm"; import PasswordChangeForm from "../../components/PasswordChangeForm";
import PasswordForgetForm from "../../components/PasswordForgetForm"; import PasswordForgetForm from "../../components/PasswordForgetForm";
import { useFirebase } from "../../services/auth";
const styles = { const styles = {
root: { root: {
@ -12,8 +11,6 @@ const styles = {
}; };
const AccountPage = () => { const AccountPage = () => {
const auth = useFirebase();
return ( return (
<Container style={styles.root}> <Container style={styles.root}>
<h1>Account</h1> <h1>Account</h1>

27
models/User.js Normal file
View file

@ -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;

View file

@ -1,27 +1,39 @@
import express from "express"; import express from "express";
import admin from "../../services/auth/index.js"; import admin from "../../services/auth/index.js";
import User from "../../models/User.js";
const router = express.Router(); const router = express.Router();
/**
* @route POST /api/users/
* @access Public
* @description Creates a user
*/
router.post("/", async (req, res) => { router.post("/", async (req, res) => {
const { const newUser = new User({
email, ...req.body,
firstName,
lastName,
password,
phoneNumber,
photoUrl,
} = req.body;
const user = await admin.auth().createUser({
email,
phoneNumber,
password,
displayName: `${firstName} ${lastName}`,
photoUrl,
}); });
//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); 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; export default router;