mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-06 00:36:39 +00:00
User Model; add User to db
This commit is contained in:
parent
40ccfaf90f
commit
8d3b0ee1d5
5 changed files with 78 additions and 18 deletions
1
TODO.md
1
TODO.md
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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]);
|
||||||
|
|
|
||||||
|
|
@ -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
27
models/User.js
Normal 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;
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue