mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-06 00:36:39 +00:00
secures api routes using Firebase; add create users route
This commit is contained in:
parent
9e26eff3c8
commit
e36d7fc43a
7 changed files with 1311 additions and 31 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,5 +1,5 @@
|
||||||
**/node_modules
|
**/node_modules
|
||||||
**keys**
|
**/config
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/*.env
|
/*.env
|
||||||
29
middlewares/auth/index.js
Normal file
29
middlewares/auth/index.js
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
import admin from "../../services/auth/index.js";
|
||||||
|
|
||||||
|
const getAuthToken = (req, res, next) => {
|
||||||
|
if (
|
||||||
|
req.headers.authorization &&
|
||||||
|
req.headers.authorization.split(" ")[0] === "Bearer"
|
||||||
|
) {
|
||||||
|
req.authToken = req.headers.authorization.split(" ")[1];
|
||||||
|
} else {
|
||||||
|
req.authToken = null;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
|
const authenticationChecker = (req, res, next) => {
|
||||||
|
getAuthToken(req, res, async () => {
|
||||||
|
try {
|
||||||
|
const { authToken } = req;
|
||||||
|
const userInfo = await admin.auth().verifyIdToken(authToken);
|
||||||
|
req.authId = userInfo.uid;
|
||||||
|
return next();
|
||||||
|
} catch (e) {
|
||||||
|
return res
|
||||||
|
.status(401)
|
||||||
|
.json({ error: "You are not authorized to make this request." });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
export default authenticationChecker;
|
||||||
1265
package-lock.json
generated
1265
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,8 +4,8 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node --experimental-modules server.js",
|
"start": "node --experimental-modules --experimental-json-modules server.js",
|
||||||
"server": "nodemon server.js",
|
"server": "nodemon --experimental-json-modules server.js",
|
||||||
"client": "npm start --prefix client",
|
"client": "npm start --prefix client",
|
||||||
"client-install": "npm install --prefix client",
|
"client-install": "npm install --prefix client",
|
||||||
"dev": "concurrently \"npm run server\" \"npm run client\"",
|
"dev": "concurrently \"npm run server\" \"npm run client\"",
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
"@babel/preset-env": "^7.9.5",
|
"@babel/preset-env": "^7.9.5",
|
||||||
"concurrently": "^5.1.0",
|
"concurrently": "^5.1.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
|
"firebase-admin": "^8.11.0",
|
||||||
"helmet": "^3.22.0",
|
"helmet": "^3.22.0",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
"mongoose": "^5.9.10",
|
"mongoose": "^5.9.10",
|
||||||
|
|
|
||||||
27
routes/api/users.js
Normal file
27
routes/api/users.js
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
import express from "express";
|
||||||
|
import admin from "../../services/auth/index.js";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.json(user);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
|
|
@ -4,6 +4,8 @@ import path from "path";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import items from "./routes/api/items.js";
|
import items from "./routes/api/items.js";
|
||||||
|
import users from "./routes/api/users.js";
|
||||||
|
import authenticationChecker from "./middlewares/auth/index.js";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
|
@ -13,7 +15,6 @@ const PORT = process.env.PORT || 5000;
|
||||||
const db =
|
const db =
|
||||||
process.env.MONGO_URI ||
|
process.env.MONGO_URI ||
|
||||||
"mongodb+srv://ruidy:Xyxoo971+mongodb@projectscluster-xcfet.mongodb.net/test?retryWrites=true&w=majority";
|
"mongodb+srv://ruidy:Xyxoo971+mongodb@projectscluster-xcfet.mongodb.net/test?retryWrites=true&w=majority";
|
||||||
|
|
||||||
// connection to database
|
// connection to database
|
||||||
mongoose
|
mongoose
|
||||||
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
|
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
|
||||||
|
|
@ -22,10 +23,12 @@ mongoose
|
||||||
|
|
||||||
// some security
|
// some security
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
|
app.use(authenticationChecker);
|
||||||
// bodyparser middleware
|
// bodyparser middleware
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
// Register routes
|
// Register routes
|
||||||
app.use("/api/items/", items);
|
app.use("/api/items/", items);
|
||||||
|
app.use("/api/users/", users);
|
||||||
|
|
||||||
// Serve static assets in production
|
// Serve static assets in production
|
||||||
if (process.env.NODE_ENV === "production") {
|
if (process.env.NODE_ENV === "production") {
|
||||||
|
|
|
||||||
9
services/auth/index.js
Normal file
9
services/auth/index.js
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import admin from "firebase-admin";
|
||||||
|
import serviceAccount from "../../config/service_account.json";
|
||||||
|
|
||||||
|
admin.initializeApp({
|
||||||
|
credential: admin.default.credential.cert(serviceAccount),
|
||||||
|
databaseURL: "https://devprojects-4749c.firebaseio.com",
|
||||||
|
});
|
||||||
|
|
||||||
|
export default admin;
|
||||||
Loading…
Reference in a new issue