diff --git a/lesson_01/server.js b/lesson_01/server.js
deleted file mode 100644
index d94bf30..0000000
--- a/lesson_01/server.js
+++ /dev/null
@@ -1,21 +0,0 @@
-const express = require('express')
-const app = express()
-const path = require('path')
-const PORT = process.env.PORT || 3500
-
-app.use('/', express.static(path.join(__dirname, '/public')))
-
-app.use('/', require('./routes/root'))
-
-app.all('*', (req, res) => {
- res.status(404)
- if (req.accepts('html')) {
- res.sendFile(path.join(__dirname, 'views', '404.html'))
- } else if (req.accepts('json')) {
- res.json({ message: '404 Not Found' })
- } else {
- res.type('txt').send('404 Not Found')
- }
-})
-
-app.listen(PORT, () => console.log(`Server running on port ${PORT}`))
\ No newline at end of file
diff --git a/lesson_01/.gitignore b/lesson_01_ProjectStructure/.gitignore
similarity index 100%
rename from lesson_01/.gitignore
rename to lesson_01_ProjectStructure/.gitignore
diff --git a/lesson_01/UserStories.md b/lesson_01_ProjectStructure/UserStories.md
similarity index 100%
rename from lesson_01/UserStories.md
rename to lesson_01_ProjectStructure/UserStories.md
diff --git a/lesson_01/package-lock.json b/lesson_01_ProjectStructure/package-lock.json
similarity index 100%
rename from lesson_01/package-lock.json
rename to lesson_01_ProjectStructure/package-lock.json
diff --git a/lesson_01/package.json b/lesson_01_ProjectStructure/package.json
similarity index 100%
rename from lesson_01/package.json
rename to lesson_01_ProjectStructure/package.json
diff --git a/lesson_01/public/css/style.css b/lesson_01_ProjectStructure/public/css/style.css
similarity index 100%
rename from lesson_01/public/css/style.css
rename to lesson_01_ProjectStructure/public/css/style.css
diff --git a/lesson_01_ProjectStructure/routes/root.js b/lesson_01_ProjectStructure/routes/root.js
new file mode 100644
index 0000000..a178766
--- /dev/null
+++ b/lesson_01_ProjectStructure/routes/root.js
@@ -0,0 +1,9 @@
+const express = require("express");
+const router = express.Router();
+const path = require("path");
+
+router.get("^/$|/index(.html)?", (req, res) => {
+ res.sendFile(path.join(__dirname, "..", "views", "index.html")); // telling it where to find the /index.html file
+});
+
+module.exports = router;
diff --git a/lesson_01_ProjectStructure/server.js b/lesson_01_ProjectStructure/server.js
new file mode 100644
index 0000000..f4411d2
--- /dev/null
+++ b/lesson_01_ProjectStructure/server.js
@@ -0,0 +1,22 @@
+const express = require("express");
+const app = express();
+const path = require("path");
+const PORT = process.env.PORT || 3500;
+
+app.use("/", express.static(path.join(__dirname, "/public"))); // telling express where to find static files
+
+app.use("/", require("./routes/root"));
+
+app.all("*", (req, res) => {
+ res.status(404);
+ // responses based on the html request headers
+ if (req.accepts("html")) {
+ res.sendFile(path.join(__dirname, "views", "404.html"));
+ } else if (req.accepts("json")) {
+ res.json({ message: "404 Not Found" });
+ } else {
+ res.type("txt").send("404 Not Found");
+ }
+});
+
+app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
diff --git a/lesson_01/views/404.html b/lesson_01_ProjectStructure/views/404.html
similarity index 100%
rename from lesson_01/views/404.html
rename to lesson_01_ProjectStructure/views/404.html
diff --git a/lesson_01/views/index.html b/lesson_01_ProjectStructure/views/index.html
similarity index 100%
rename from lesson_01/views/index.html
rename to lesson_01_ProjectStructure/views/index.html
diff --git a/lesson_02/server.js b/lesson_02/server.js
deleted file mode 100644
index eeb27cc..0000000
--- a/lesson_02/server.js
+++ /dev/null
@@ -1,36 +0,0 @@
-const express = require('express')
-const app = express()
-const path = require('path')
-const { logger } = require('./middleware/logger')
-const errorHandler = require('./middleware/errorHandler')
-const cookieParser = require('cookie-parser')
-const cors = require('cors')
-const corsOptions = require('./config/corsOptions')
-const PORT = process.env.PORT || 3500
-
-app.use(logger)
-
-app.use(cors(corsOptions))
-
-app.use(express.json())
-
-app.use(cookieParser())
-
-app.use('/', express.static(path.join(__dirname, 'public')))
-
-app.use('/', require('./routes/root'))
-
-app.all('*', (req, res) => {
- res.status(404)
- if (req.accepts('html')) {
- res.sendFile(path.join(__dirname, 'views', '404.html'))
- } else if (req.accepts('json')) {
- res.json({ message: '404 Not Found' })
- } else {
- res.type('txt').send('404 Not Found')
- }
-})
-
-app.use(errorHandler)
-
-app.listen(PORT, () => console.log(`Server running on port ${PORT}`))
\ No newline at end of file
diff --git a/lesson_02/.gitignore b/lesson_02_ExpressMiddleware/.gitignore
similarity index 100%
rename from lesson_02/.gitignore
rename to lesson_02_ExpressMiddleware/.gitignore
diff --git a/lesson_02/UserStories.md b/lesson_02_ExpressMiddleware/UserStories.md
similarity index 100%
rename from lesson_02/UserStories.md
rename to lesson_02_ExpressMiddleware/UserStories.md
diff --git a/lesson_02/config/allowedOrigins.js b/lesson_02_ExpressMiddleware/config/allowedOrigins.js
similarity index 100%
rename from lesson_02/config/allowedOrigins.js
rename to lesson_02_ExpressMiddleware/config/allowedOrigins.js
diff --git a/lesson_02_ExpressMiddleware/config/corsOptions.js b/lesson_02_ExpressMiddleware/config/corsOptions.js
new file mode 100644
index 0000000..228b929
--- /dev/null
+++ b/lesson_02_ExpressMiddleware/config/corsOptions.js
@@ -0,0 +1,24 @@
+const allowedOrigins = require("./allowedOrigins");
+
+const corsOptions = {
+ // Called to determine whether a request's origin is allowed
+ /*
+ 1. origin : the origin(domain) of incoming request
+ 2. callback : to indicate whether the origin is allowed or not
+ */
+ origin: (origin, callback) => {
+ /*
+ 1. checks if the origin is in the allowedOrigins
+ 2. if origin is not defined (apps that do not have an origin - postman, desktop ...)
+ */
+ if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
+ callback(null, true);
+ } else {
+ callback(new Error("Not allowed by CORS"));
+ }
+ },
+ credentials: true, // enables credentials to be sent and received during cross-oirigin requests
+ optionsSuccessStatus: 200,
+};
+
+module.exports = corsOptions;
diff --git a/lesson_02_ExpressMiddleware/middleware/errorHandler.js b/lesson_02_ExpressMiddleware/middleware/errorHandler.js
new file mode 100644
index 0000000..28285a5
--- /dev/null
+++ b/lesson_02_ExpressMiddleware/middleware/errorHandler.js
@@ -0,0 +1,18 @@
+const { logEvents } = require("./logger");
+
+// log error events by calling the logEvents()
+const errorHandler = (err, req, res, next) => {
+ logEvents(
+ `${err.name}: ${err.message}\t${req.method}\t${req.url}\t${req.headers.origin}`,
+ "errLog.log"
+ );
+ console.log(err.stack);
+
+ const status = res.statusCode ? res.statusCode : 500; // server error
+
+ res.status(status);
+
+ res.json({ message: err.message });
+};
+
+module.exports = errorHandler;
diff --git a/lesson_02_ExpressMiddleware/middleware/logger.js b/lesson_02_ExpressMiddleware/middleware/logger.js
new file mode 100644
index 0000000..6e6dd96
--- /dev/null
+++ b/lesson_02_ExpressMiddleware/middleware/logger.js
@@ -0,0 +1,33 @@
+const { format } = require("date-fns");
+const { v4: uuid } = require("uuid");
+const fs = require("fs");
+const fsPromises = require("fs").promises;
+const path = require("path");
+
+const logEvents = async (message, logFileName) => {
+ const dateTime = format(new Date(), "yyyyMMdd\tHH:mm:ss");
+ const logItem = `${dateTime}\t${uuid()}\t${message}\n`;
+
+ try {
+ // If the log dir doesn't exist, create the log dir
+ if (!fs.existsSync(path.join(__dirname, "..", "logs"))) {
+ await fsPromises.mkdir(path.join(__dirname, "..", "logs"));
+ }
+ // Once the log dir is created, append the logItem into the file named "logFileName"
+ await fsPromises.appendFile(
+ path.join(__dirname, "..", "logs", logFileName),
+ logItem
+ );
+ } catch (err) {
+ console.log(err);
+ }
+};
+
+// call upon the logEvents() to log events
+const logger = (req, res, next) => {
+ logEvents(`${req.method}\t${req.url}\t${req.headers.origin}`, "reqLog.log");
+ console.log(`${req.method} ${req.path}`);
+ next();
+};
+
+module.exports = { logEvents, logger };
diff --git a/lesson_02/package-lock.json b/lesson_02_ExpressMiddleware/package-lock.json
similarity index 100%
rename from lesson_02/package-lock.json
rename to lesson_02_ExpressMiddleware/package-lock.json
diff --git a/lesson_02/package.json b/lesson_02_ExpressMiddleware/package.json
similarity index 100%
rename from lesson_02/package.json
rename to lesson_02_ExpressMiddleware/package.json
diff --git a/lesson_02/public/css/style.css b/lesson_02_ExpressMiddleware/public/css/style.css
similarity index 100%
rename from lesson_02/public/css/style.css
rename to lesson_02_ExpressMiddleware/public/css/style.css
diff --git a/lesson_02_ExpressMiddleware/routes/root.js b/lesson_02_ExpressMiddleware/routes/root.js
new file mode 100644
index 0000000..67fcb72
--- /dev/null
+++ b/lesson_02_ExpressMiddleware/routes/root.js
@@ -0,0 +1,9 @@
+const express = require("express");
+const router = express.Router();
+const path = require("path");
+
+router.get("^/$|/index(.html)?", (req, res) => {
+ res.sendFile(path.join(__dirname, "..", "views", "index.html")); // runs the index.html file whenever the route is at "/" or "/index" or "/index.html"
+});
+
+module.exports = router;
diff --git a/lesson_02_ExpressMiddleware/server.js b/lesson_02_ExpressMiddleware/server.js
new file mode 100644
index 0000000..7ce4b8a
--- /dev/null
+++ b/lesson_02_ExpressMiddleware/server.js
@@ -0,0 +1,37 @@
+const express = require("express");
+const app = express();
+const path = require("path");
+const { logger } = require("./middleware/logger");
+const errorHandler = require("./middleware/errorHandler");
+const cookieParser = require("cookie-parser");
+const cors = require("cors");
+const corsOptions = require("./config/corsOptions");
+const PORT = process.env.PORT || 3500;
+
+app.use(logger); // log the server RESTFUL Apis activities
+
+app.use(cors(corsOptions)); // by default, no CORS (others cannot access the apis of this site), need to enable it (choose who is allow to access our apis)
+
+app.use(express.json());
+
+app.use(cookieParser()); // parse cookies
+
+app.use("/", express.static(path.join(__dirname, "public"))); // tell express where the static resources are at
+
+app.use("/", require("./routes/root"));
+
+// routes that cannot be resolved will send back different responses based on the requests accept header
+app.all("*", (req, res) => {
+ res.status(404);
+ if (req.accepts("html")) {
+ res.sendFile(path.join(__dirname, "views", "404.html"));
+ } else if (req.accepts("json")) {
+ res.json({ message: "404 Not Found" });
+ } else {
+ res.type("txt").send("404 Not Found");
+ }
+});
+
+app.use(errorHandler);
+
+app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
diff --git a/lesson_02/views/404.html b/lesson_02_ExpressMiddleware/views/404.html
similarity index 100%
rename from lesson_02/views/404.html
rename to lesson_02_ExpressMiddleware/views/404.html
diff --git a/lesson_02/views/index.html b/lesson_02_ExpressMiddleware/views/index.html
similarity index 100%
rename from lesson_02/views/index.html
rename to lesson_02_ExpressMiddleware/views/index.html
diff --git a/lesson_03/server.js b/lesson_03/server.js
deleted file mode 100644
index fa14181..0000000
--- a/lesson_03/server.js
+++ /dev/null
@@ -1,51 +0,0 @@
-require('dotenv').config()
-const express = require('express')
-const app = express()
-const path = require('path')
-const { logger, logEvents } = require('./middleware/logger')
-const errorHandler = require('./middleware/errorHandler')
-const cookieParser = require('cookie-parser')
-const cors = require('cors')
-const corsOptions = require('./config/corsOptions')
-const connectDB = require('./config/dbConn')
-const mongoose = require('mongoose')
-const PORT = process.env.PORT || 3500
-
-console.log(process.env.NODE_ENV)
-
-connectDB()
-
-app.use(logger)
-
-app.use(cors(corsOptions))
-
-app.use(express.json())
-
-app.use(cookieParser())
-
-app.use('/', express.static(path.join(__dirname, 'public')))
-
-app.use('/', require('./routes/root'))
-
-app.all('*', (req, res) => {
- res.status(404)
- if (req.accepts('html')) {
- res.sendFile(path.join(__dirname, 'views', '404.html'))
- } else if (req.accepts('json')) {
- res.json({ message: '404 Not Found' })
- } else {
- res.type('txt').send('404 Not Found')
- }
-})
-
-app.use(errorHandler)
-
-mongoose.connection.once('open', () => {
- console.log('Connected to MongoDB')
- app.listen(PORT, () => console.log(`Server running on port ${PORT}`))
-})
-
-mongoose.connection.on('error', err => {
- console.log(err)
- logEvents(`${err.no}: ${err.code}\t${err.syscall}\t${err.hostname}`, 'mongoErrLog.log')
-})
diff --git a/lesson_03/.gitignore b/lesson_03_MongoDB/.gitignore
similarity index 100%
rename from lesson_03/.gitignore
rename to lesson_03_MongoDB/.gitignore
diff --git a/lesson_03/UserStories.md b/lesson_03_MongoDB/UserStories.md
similarity index 100%
rename from lesson_03/UserStories.md
rename to lesson_03_MongoDB/UserStories.md
diff --git a/lesson_03/config/allowedOrigins.js b/lesson_03_MongoDB/config/allowedOrigins.js
similarity index 100%
rename from lesson_03/config/allowedOrigins.js
rename to lesson_03_MongoDB/config/allowedOrigins.js
diff --git a/lesson_02/config/corsOptions.js b/lesson_03_MongoDB/config/corsOptions.js
similarity index 100%
rename from lesson_02/config/corsOptions.js
rename to lesson_03_MongoDB/config/corsOptions.js
diff --git a/lesson_03/config/dbConn.js b/lesson_03_MongoDB/config/dbConn.js
similarity index 100%
rename from lesson_03/config/dbConn.js
rename to lesson_03_MongoDB/config/dbConn.js
diff --git a/lesson_02/middleware/errorHandler.js b/lesson_03_MongoDB/middleware/errorHandler.js
similarity index 100%
rename from lesson_02/middleware/errorHandler.js
rename to lesson_03_MongoDB/middleware/errorHandler.js
diff --git a/lesson_02/middleware/logger.js b/lesson_03_MongoDB/middleware/logger.js
similarity index 100%
rename from lesson_02/middleware/logger.js
rename to lesson_03_MongoDB/middleware/logger.js
diff --git a/lesson_03_MongoDB/models/Note.js b/lesson_03_MongoDB/models/Note.js
new file mode 100644
index 0000000..edf564f
--- /dev/null
+++ b/lesson_03_MongoDB/models/Note.js
@@ -0,0 +1,36 @@
+const mongoose = require("mongoose");
+const AutoIncrement = require("mongoose-sequence")(mongoose);
+
+const noteSchema = new mongoose.Schema(
+ {
+ user: {
+ type: mongoose.Schema.Types.ObjectId,
+ required: true,
+ ref: "User",
+ },
+ title: {
+ type: String,
+ required: true,
+ },
+ text: {
+ type: String,
+ required: true,
+ },
+ completed: {
+ type: Boolean,
+ default: false,
+ },
+ },
+ {
+ timestamps: true,
+ }
+);
+
+// create a ticket field inside of the noteSchema that will get the sequential number starting from 500
+noteSchema.plugin(AutoIncrement, {
+ inc_field: "ticket",
+ id: "ticketNums",
+ start_seq: 500,
+});
+
+module.exports = mongoose.model("Note", noteSchema);
diff --git a/lesson_03/models/User.js b/lesson_03_MongoDB/models/User.js
similarity index 100%
rename from lesson_03/models/User.js
rename to lesson_03_MongoDB/models/User.js
diff --git a/lesson_03/package-lock.json b/lesson_03_MongoDB/package-lock.json
similarity index 100%
rename from lesson_03/package-lock.json
rename to lesson_03_MongoDB/package-lock.json
diff --git a/lesson_03/package.json b/lesson_03_MongoDB/package.json
similarity index 100%
rename from lesson_03/package.json
rename to lesson_03_MongoDB/package.json
diff --git a/lesson_03/public/css/style.css b/lesson_03_MongoDB/public/css/style.css
similarity index 100%
rename from lesson_03/public/css/style.css
rename to lesson_03_MongoDB/public/css/style.css
diff --git a/lesson_01/routes/root.js b/lesson_03_MongoDB/routes/root.js
similarity index 100%
rename from lesson_01/routes/root.js
rename to lesson_03_MongoDB/routes/root.js
diff --git a/lesson_03_MongoDB/server.js b/lesson_03_MongoDB/server.js
new file mode 100644
index 0000000..1f626d3
--- /dev/null
+++ b/lesson_03_MongoDB/server.js
@@ -0,0 +1,56 @@
+require("dotenv").config();
+const express = require("express");
+const app = express();
+const path = require("path");
+const { logger, logEvents } = require("./middleware/logger");
+const errorHandler = require("./middleware/errorHandler");
+const cookieParser = require("cookie-parser");
+const cors = require("cors");
+const corsOptions = require("./config/corsOptions");
+const connectDB = require("./config/dbConn");
+const mongoose = require("mongoose");
+const PORT = process.env.PORT || 3500;
+
+console.log(process.env.NODE_ENV);
+
+connectDB();
+
+app.use(logger);
+
+app.use(cors(corsOptions));
+
+app.use(express.json());
+
+app.use(cookieParser());
+
+app.use("/", express.static(path.join(__dirname, "public")));
+
+app.use("/", require("./routes/root"));
+
+app.all("*", (req, res) => {
+ res.status(404);
+ if (req.accepts("html")) {
+ res.sendFile(path.join(__dirname, "views", "404.html"));
+ } else if (req.accepts("json")) {
+ res.json({ message: "404 Not Found" });
+ } else {
+ res.type("txt").send("404 Not Found");
+ }
+});
+
+app.use(errorHandler);
+
+// An event listener that listens for the "open" event once, which is emitted by Mongoose when the connection to MongoDB is successful.
+mongoose.connection.once("open", () => {
+ console.log("Connected to MongoDB");
+ app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
+});
+
+// An event listener that listens for error events emitted by Mongoose when the connection to MongoDB is unsuccessful.
+mongoose.connection.on("error", (err) => {
+ console.log(err);
+ logEvents(
+ `${err.no}: ${err.code}\t${err.syscall}\t${err.hostname}`,
+ "mongoErrLog.log"
+ );
+});
diff --git a/lesson_03/views/404.html b/lesson_03_MongoDB/views/404.html
similarity index 100%
rename from lesson_03/views/404.html
rename to lesson_03_MongoDB/views/404.html
diff --git a/lesson_03/views/index.html b/lesson_03_MongoDB/views/index.html
similarity index 100%
rename from lesson_03/views/index.html
rename to lesson_03_MongoDB/views/index.html
diff --git a/lesson_04/routes/root.js b/lesson_04/routes/root.js
deleted file mode 100644
index d4d8d79..0000000
--- a/lesson_04/routes/root.js
+++ /dev/null
@@ -1,9 +0,0 @@
-const express = require('express')
-const router = express.Router()
-const path = require('path')
-
-router.get('^/$|/index(.html)?', (req, res) => {
- res.sendFile(path.join(__dirname, '..', 'views', 'index.html'))
-})
-
-module.exports = router
\ No newline at end of file
diff --git a/lesson_04/.gitignore b/lesson_04_Controllers/.gitignore
similarity index 100%
rename from lesson_04/.gitignore
rename to lesson_04_Controllers/.gitignore
diff --git a/lesson_04/UserStories.md b/lesson_04_Controllers/UserStories.md
similarity index 100%
rename from lesson_04/UserStories.md
rename to lesson_04_Controllers/UserStories.md
diff --git a/lesson_04/config/allowedOrigins.js b/lesson_04_Controllers/config/allowedOrigins.js
similarity index 100%
rename from lesson_04/config/allowedOrigins.js
rename to lesson_04_Controllers/config/allowedOrigins.js
diff --git a/lesson_03/config/corsOptions.js b/lesson_04_Controllers/config/corsOptions.js
similarity index 100%
rename from lesson_03/config/corsOptions.js
rename to lesson_04_Controllers/config/corsOptions.js
diff --git a/lesson_04/config/dbConn.js b/lesson_04_Controllers/config/dbConn.js
similarity index 100%
rename from lesson_04/config/dbConn.js
rename to lesson_04_Controllers/config/dbConn.js
diff --git a/lesson_04/controllers/notesController.js b/lesson_04_Controllers/controllers/notesController.js
similarity index 100%
rename from lesson_04/controllers/notesController.js
rename to lesson_04_Controllers/controllers/notesController.js
diff --git a/lesson_04_Controllers/controllers/usersController.js b/lesson_04_Controllers/controllers/usersController.js
new file mode 100644
index 0000000..bcb2369
--- /dev/null
+++ b/lesson_04_Controllers/controllers/usersController.js
@@ -0,0 +1,150 @@
+const User = require("../models/User");
+const Note = require("../models/Note");
+const asyncHandler = require("express-async-handler"); // prevent us from using a lot of async try/catch blocks
+const bcrypt = require("bcrypt");
+
+// @desc Get all users
+// @route GET /users
+// @access Private
+const getAllUsers = asyncHandler(async (req, res) => {
+ // Get all users from MongoDB
+ const users = await User.find().select("-password").lean(); // return user as a plain JSON as opposed to getting a full Mongoose document with a bunch of methods such as save() ...
+
+ // If no users
+ if (!users?.length) {
+ return res.status(400).json({ message: "No users found" });
+ }
+
+ res.json(users);
+});
+
+// @desc Create new user
+// @route POST /users
+// @access Private
+const createNewUser = asyncHandler(async (req, res) => {
+ const { username, password, roles } = req.body;
+
+ // Confirm data
+ if (!username || !password || !Array.isArray(roles) || !roles.length) {
+ return res.status(400).json({ message: "All fields are required" });
+ }
+
+ // Check for duplicate username
+ const duplicate = await User.findOne({ username }).lean().exec(); // Mongoose queries are not real Promises, instead they return thenable objects, but if you do need real Promises, then you should use exec() on the queries.
+
+ if (duplicate) {
+ return res.status(409).json({ message: "Duplicate username" });
+ }
+
+ // Hash password
+ const hashedPwd = await bcrypt.hash(password, 10); // salt rounds
+
+ const userObject = { username, password: hashedPwd, roles };
+
+ // Create and store new user
+ const user = await User.create(userObject);
+
+ if (user) {
+ //created
+ res.status(201).json({ message: `New user ${username} created` });
+ } else {
+ res.status(400).json({ message: "Invalid user data received" });
+ }
+});
+
+// @desc Update a user
+// @route PATCH /users
+// @access Private
+const updateUser = asyncHandler(async (req, res) => {
+ const { id, username, roles, active, password } = req.body;
+
+ // Confirm data
+ if (
+ !id ||
+ !username ||
+ !Array.isArray(roles) ||
+ !roles.length ||
+ typeof active !== "boolean"
+ ) {
+ return res
+ .status(400)
+ .json({ message: "All fields except password are required" });
+ }
+
+ // Does the user exist to update?
+ /*
+ .exec() vs no exec() :
+ - exec() is used to execute the query and return a promise that resolves to the result of the query
+ - control over when and how the query is executed.
+ - await or .then() directly returns a promise that resolves to the object it found
+ */
+ const user = await User.findById(id).exec();
+
+ if (!user) {
+ return res.status(400).json({ message: "User not found" });
+ }
+
+ // Check for duplicate
+ /*
+ If we change the username of the user that we want to update to a username that already exist
+ - duplicate will exist
+ - duplicate will yield a different user_id than the one we're trying to update
+ */
+ const duplicate = await User.findOne({ username }).lean().exec();
+
+ // Allow updates to the original user
+ if (duplicate && duplicate?._id.toString() !== id) {
+ return res.status(409).json({ message: "Duplicate username" });
+ }
+
+ user.username = username;
+ user.roles = roles;
+ user.active = active;
+
+ if (password) {
+ // Hash password
+ user.password = await bcrypt.hash(password, 10); // salt rounds
+ }
+
+ const updatedUser = await user.save();
+
+ res.json({ message: `${updatedUser.username} updated` });
+});
+
+// @desc Delete a user
+// @route DELETE /users
+// @access Private
+const deleteUser = asyncHandler(async (req, res) => {
+ const { id } = req.body;
+
+ // Confirm data
+ if (!id) {
+ return res.status(400).json({ message: "User ID Required" });
+ }
+
+ // Does the user still have assigned notes?
+ const note = await Note.findOne({ user: id }).lean().exec();
+ if (note) {
+ return res.status(400).json({ message: "User has assigned notes" });
+ }
+
+ // Does the user exist to delete?
+ const user = await User.findById(id).exec();
+
+ if (!user) {
+ return res.status(400).json({ message: "User not found" });
+ }
+
+ const result = await user.deleteOne();
+ r;
+ const reply = `Username ${result.username} with ID ${result._id} deleted`;
+
+ res.json(reply);
+});
+
+module.exports = {
+ getAllUsers,
+ createNewUser,
+ updateUser,
+ deleteUser,
+};
diff --git a/lesson_03/middleware/errorHandler.js b/lesson_04_Controllers/middleware/errorHandler.js
similarity index 100%
rename from lesson_03/middleware/errorHandler.js
rename to lesson_04_Controllers/middleware/errorHandler.js
diff --git a/lesson_03/middleware/logger.js b/lesson_04_Controllers/middleware/logger.js
similarity index 100%
rename from lesson_03/middleware/logger.js
rename to lesson_04_Controllers/middleware/logger.js
diff --git a/lesson_03/models/Note.js b/lesson_04_Controllers/models/Note.js
similarity index 100%
rename from lesson_03/models/Note.js
rename to lesson_04_Controllers/models/Note.js
diff --git a/lesson_04/models/User.js b/lesson_04_Controllers/models/User.js
similarity index 100%
rename from lesson_04/models/User.js
rename to lesson_04_Controllers/models/User.js
diff --git a/lesson_04/package-lock.json b/lesson_04_Controllers/package-lock.json
similarity index 100%
rename from lesson_04/package-lock.json
rename to lesson_04_Controllers/package-lock.json
diff --git a/lesson_04/package.json b/lesson_04_Controllers/package.json
similarity index 100%
rename from lesson_04/package.json
rename to lesson_04_Controllers/package.json
diff --git a/lesson_04/public/css/style.css b/lesson_04_Controllers/public/css/style.css
similarity index 100%
rename from lesson_04/public/css/style.css
rename to lesson_04_Controllers/public/css/style.css
diff --git a/lesson_04/routes/noteRoutes.js b/lesson_04_Controllers/routes/noteRoutes.js
similarity index 100%
rename from lesson_04/routes/noteRoutes.js
rename to lesson_04_Controllers/routes/noteRoutes.js
diff --git a/lesson_02/routes/root.js b/lesson_04_Controllers/routes/root.js
similarity index 100%
rename from lesson_02/routes/root.js
rename to lesson_04_Controllers/routes/root.js
diff --git a/lesson_04/routes/userRoutes.js b/lesson_04_Controllers/routes/userRoutes.js
similarity index 100%
rename from lesson_04/routes/userRoutes.js
rename to lesson_04_Controllers/routes/userRoutes.js
diff --git a/lesson_04/server.js b/lesson_04_Controllers/server.js
similarity index 100%
rename from lesson_04/server.js
rename to lesson_04_Controllers/server.js
diff --git a/lesson_04/views/404.html b/lesson_04_Controllers/views/404.html
similarity index 100%
rename from lesson_04/views/404.html
rename to lesson_04_Controllers/views/404.html
diff --git a/lesson_04/views/index.html b/lesson_04_Controllers/views/index.html
similarity index 100%
rename from lesson_04/views/index.html
rename to lesson_04_Controllers/views/index.html
diff --git a/lesson_05-frontend/.gitignore b/lesson_05-frontend_ReactJs/.gitignore
similarity index 100%
rename from lesson_05-frontend/.gitignore
rename to lesson_05-frontend_ReactJs/.gitignore
diff --git a/lesson_05-frontend/UserStories.md b/lesson_05-frontend_ReactJs/UserStories.md
similarity index 100%
rename from lesson_05-frontend/UserStories.md
rename to lesson_05-frontend_ReactJs/UserStories.md
diff --git a/lesson_05-frontend/package-lock.json b/lesson_05-frontend_ReactJs/package-lock.json
similarity index 100%
rename from lesson_05-frontend/package-lock.json
rename to lesson_05-frontend_ReactJs/package-lock.json
diff --git a/lesson_05-frontend/package.json b/lesson_05-frontend_ReactJs/package.json
similarity index 100%
rename from lesson_05-frontend/package.json
rename to lesson_05-frontend_ReactJs/package.json
diff --git a/lesson_05-frontend/public/favicon.ico b/lesson_05-frontend_ReactJs/public/favicon.ico
similarity index 100%
rename from lesson_05-frontend/public/favicon.ico
rename to lesson_05-frontend_ReactJs/public/favicon.ico
diff --git a/lesson_05-frontend/public/img/background.jpg b/lesson_05-frontend_ReactJs/public/img/background.jpg
similarity index 100%
rename from lesson_05-frontend/public/img/background.jpg
rename to lesson_05-frontend_ReactJs/public/img/background.jpg
diff --git a/lesson_05-frontend/public/index.html b/lesson_05-frontend_ReactJs/public/index.html
similarity index 100%
rename from lesson_05-frontend/public/index.html
rename to lesson_05-frontend_ReactJs/public/index.html
diff --git a/lesson_05-frontend/public/manifest.json b/lesson_05-frontend_ReactJs/public/manifest.json
similarity index 100%
rename from lesson_05-frontend/public/manifest.json
rename to lesson_05-frontend_ReactJs/public/manifest.json
diff --git a/lesson_05-frontend/public/robots.txt b/lesson_05-frontend_ReactJs/public/robots.txt
similarity index 100%
rename from lesson_05-frontend/public/robots.txt
rename to lesson_05-frontend_ReactJs/public/robots.txt
diff --git a/lesson_05-frontend/src/App.js b/lesson_05-frontend_ReactJs/src/App.js
similarity index 100%
rename from lesson_05-frontend/src/App.js
rename to lesson_05-frontend_ReactJs/src/App.js
diff --git a/lesson_05-frontend/src/components/DashFooter.js b/lesson_05-frontend_ReactJs/src/components/DashFooter.js
similarity index 100%
rename from lesson_05-frontend/src/components/DashFooter.js
rename to lesson_05-frontend_ReactJs/src/components/DashFooter.js
diff --git a/lesson_05-frontend/src/components/DashHeader.js b/lesson_05-frontend_ReactJs/src/components/DashHeader.js
similarity index 100%
rename from lesson_05-frontend/src/components/DashHeader.js
rename to lesson_05-frontend_ReactJs/src/components/DashHeader.js
diff --git a/lesson_05-frontend/src/components/DashLayout.js b/lesson_05-frontend_ReactJs/src/components/DashLayout.js
similarity index 100%
rename from lesson_05-frontend/src/components/DashLayout.js
rename to lesson_05-frontend_ReactJs/src/components/DashLayout.js
diff --git a/lesson_05-frontend/src/components/Layout.js b/lesson_05-frontend_ReactJs/src/components/Layout.js
similarity index 100%
rename from lesson_05-frontend/src/components/Layout.js
rename to lesson_05-frontend_ReactJs/src/components/Layout.js
diff --git a/lesson_05-frontend_ReactJs/src/components/Public.js b/lesson_05-frontend_ReactJs/src/components/Public.js
new file mode 100644
index 0000000..cf9ba28
--- /dev/null
+++ b/lesson_05-frontend_ReactJs/src/components/Public.js
@@ -0,0 +1,35 @@
+import { Link } from "react-router-dom";
+
+const Public = () => {
+ const content = (
+
+
+
+ Welcome to Dan D. Repairs!
+
+
+
+
+ Located in Beautiful Downtown Foo City, Dan D. Repairs provides a
+ trained staff ready to meet your tech repair needs.
+
+
+ Dan D. Repairs
+
+ 555 Foo Drive
+
+ Foo City, CA 12345
+
+ (555) 555-5555
+
+
+
Owner: Dan Davidson
+
+
+
+ );
+ return content;
+};
+export default Public;
diff --git a/lesson_05-frontend/src/features/auth/Login.js b/lesson_05-frontend_ReactJs/src/features/auth/Login.js
similarity index 100%
rename from lesson_05-frontend/src/features/auth/Login.js
rename to lesson_05-frontend_ReactJs/src/features/auth/Login.js
diff --git a/lesson_05-frontend/src/features/auth/Welcome.js b/lesson_05-frontend_ReactJs/src/features/auth/Welcome.js
similarity index 100%
rename from lesson_05-frontend/src/features/auth/Welcome.js
rename to lesson_05-frontend_ReactJs/src/features/auth/Welcome.js
diff --git a/lesson_05-frontend/src/features/notes/NotesList.js b/lesson_05-frontend_ReactJs/src/features/notes/NotesList.js
similarity index 100%
rename from lesson_05-frontend/src/features/notes/NotesList.js
rename to lesson_05-frontend_ReactJs/src/features/notes/NotesList.js
diff --git a/lesson_05-frontend/src/features/users/UsersList.js b/lesson_05-frontend_ReactJs/src/features/users/UsersList.js
similarity index 100%
rename from lesson_05-frontend/src/features/users/UsersList.js
rename to lesson_05-frontend_ReactJs/src/features/users/UsersList.js
diff --git a/lesson_05-frontend/src/img/background.jpg b/lesson_05-frontend_ReactJs/src/img/background.jpg
similarity index 100%
rename from lesson_05-frontend/src/img/background.jpg
rename to lesson_05-frontend_ReactJs/src/img/background.jpg
diff --git a/lesson_05-frontend/src/index.css b/lesson_05-frontend_ReactJs/src/index.css
similarity index 100%
rename from lesson_05-frontend/src/index.css
rename to lesson_05-frontend_ReactJs/src/index.css
diff --git a/lesson_05-frontend/src/index.js b/lesson_05-frontend_ReactJs/src/index.js
similarity index 100%
rename from lesson_05-frontend/src/index.js
rename to lesson_05-frontend_ReactJs/src/index.js
diff --git a/lesson_06-frontend/.gitignore b/lesson_06-frontend_Redux_RTKQuery/.gitignore
similarity index 100%
rename from lesson_06-frontend/.gitignore
rename to lesson_06-frontend_Redux_RTKQuery/.gitignore
diff --git a/lesson_06-frontend/UserStories.md b/lesson_06-frontend_Redux_RTKQuery/UserStories.md
similarity index 100%
rename from lesson_06-frontend/UserStories.md
rename to lesson_06-frontend_Redux_RTKQuery/UserStories.md
diff --git a/lesson_06-frontend/package-lock.json b/lesson_06-frontend_Redux_RTKQuery/package-lock.json
similarity index 100%
rename from lesson_06-frontend/package-lock.json
rename to lesson_06-frontend_Redux_RTKQuery/package-lock.json
diff --git a/lesson_06-frontend/package.json b/lesson_06-frontend_Redux_RTKQuery/package.json
similarity index 100%
rename from lesson_06-frontend/package.json
rename to lesson_06-frontend_Redux_RTKQuery/package.json
diff --git a/lesson_06-frontend/public/favicon.ico b/lesson_06-frontend_Redux_RTKQuery/public/favicon.ico
similarity index 100%
rename from lesson_06-frontend/public/favicon.ico
rename to lesson_06-frontend_Redux_RTKQuery/public/favicon.ico
diff --git a/lesson_06-frontend/public/img/background.jpg b/lesson_06-frontend_Redux_RTKQuery/public/img/background.jpg
similarity index 100%
rename from lesson_06-frontend/public/img/background.jpg
rename to lesson_06-frontend_Redux_RTKQuery/public/img/background.jpg
diff --git a/lesson_06-frontend/public/index.html b/lesson_06-frontend_Redux_RTKQuery/public/index.html
similarity index 100%
rename from lesson_06-frontend/public/index.html
rename to lesson_06-frontend_Redux_RTKQuery/public/index.html
diff --git a/lesson_06-frontend/public/manifest.json b/lesson_06-frontend_Redux_RTKQuery/public/manifest.json
similarity index 100%
rename from lesson_06-frontend/public/manifest.json
rename to lesson_06-frontend_Redux_RTKQuery/public/manifest.json
diff --git a/lesson_06-frontend/public/robots.txt b/lesson_06-frontend_Redux_RTKQuery/public/robots.txt
similarity index 100%
rename from lesson_06-frontend/public/robots.txt
rename to lesson_06-frontend_Redux_RTKQuery/public/robots.txt
diff --git a/lesson_06-frontend/src/App.js b/lesson_06-frontend_Redux_RTKQuery/src/App.js
similarity index 100%
rename from lesson_06-frontend/src/App.js
rename to lesson_06-frontend_Redux_RTKQuery/src/App.js
diff --git a/lesson_06-frontend/src/app/api/apiSlice.js b/lesson_06-frontend_Redux_RTKQuery/src/app/api/apiSlice.js
similarity index 100%
rename from lesson_06-frontend/src/app/api/apiSlice.js
rename to lesson_06-frontend_Redux_RTKQuery/src/app/api/apiSlice.js
diff --git a/lesson_06-frontend/src/app/store.js b/lesson_06-frontend_Redux_RTKQuery/src/app/store.js
similarity index 100%
rename from lesson_06-frontend/src/app/store.js
rename to lesson_06-frontend_Redux_RTKQuery/src/app/store.js
diff --git a/lesson_06-frontend/src/components/DashFooter.js b/lesson_06-frontend_Redux_RTKQuery/src/components/DashFooter.js
similarity index 100%
rename from lesson_06-frontend/src/components/DashFooter.js
rename to lesson_06-frontend_Redux_RTKQuery/src/components/DashFooter.js
diff --git a/lesson_06-frontend/src/components/DashHeader.js b/lesson_06-frontend_Redux_RTKQuery/src/components/DashHeader.js
similarity index 100%
rename from lesson_06-frontend/src/components/DashHeader.js
rename to lesson_06-frontend_Redux_RTKQuery/src/components/DashHeader.js
diff --git a/lesson_06-frontend/src/components/DashLayout.js b/lesson_06-frontend_Redux_RTKQuery/src/components/DashLayout.js
similarity index 100%
rename from lesson_06-frontend/src/components/DashLayout.js
rename to lesson_06-frontend_Redux_RTKQuery/src/components/DashLayout.js
diff --git a/lesson_06-frontend/src/components/Layout.js b/lesson_06-frontend_Redux_RTKQuery/src/components/Layout.js
similarity index 100%
rename from lesson_06-frontend/src/components/Layout.js
rename to lesson_06-frontend_Redux_RTKQuery/src/components/Layout.js
diff --git a/lesson_05-frontend/src/components/Public.js b/lesson_06-frontend_Redux_RTKQuery/src/components/Public.js
similarity index 100%
rename from lesson_05-frontend/src/components/Public.js
rename to lesson_06-frontend_Redux_RTKQuery/src/components/Public.js
diff --git a/lesson_06-frontend/src/features/auth/Login.js b/lesson_06-frontend_Redux_RTKQuery/src/features/auth/Login.js
similarity index 100%
rename from lesson_06-frontend/src/features/auth/Login.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/auth/Login.js
diff --git a/lesson_06-frontend/src/features/auth/Welcome.js b/lesson_06-frontend_Redux_RTKQuery/src/features/auth/Welcome.js
similarity index 100%
rename from lesson_06-frontend/src/features/auth/Welcome.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/auth/Welcome.js
diff --git a/lesson_06-frontend/src/features/notes/Note.js b/lesson_06-frontend_Redux_RTKQuery/src/features/notes/Note.js
similarity index 100%
rename from lesson_06-frontend/src/features/notes/Note.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/notes/Note.js
diff --git a/lesson_06-frontend/src/features/notes/NotesList.js b/lesson_06-frontend_Redux_RTKQuery/src/features/notes/NotesList.js
similarity index 100%
rename from lesson_06-frontend/src/features/notes/NotesList.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/notes/NotesList.js
diff --git a/lesson_06-frontend/src/features/notes/notesApiSlice.js b/lesson_06-frontend_Redux_RTKQuery/src/features/notes/notesApiSlice.js
similarity index 95%
rename from lesson_06-frontend/src/features/notes/notesApiSlice.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/notes/notesApiSlice.js
index d561dfc..c0f6393 100644
--- a/lesson_06-frontend/src/features/notes/notesApiSlice.js
+++ b/lesson_06-frontend_Redux_RTKQuery/src/features/notes/notesApiSlice.js
@@ -5,7 +5,7 @@ import {
import { apiSlice } from "../../app/api/apiSlice"
const notesAdapter = createEntityAdapter({
- sortComparer: (a, b) => (a.completed === b.completed) ? 0 : a.completed ? 1 : -1
+ sortComparer: (a, b) => (a.completed === b.completed) ? 0 : a.completed ? 1 : -1 // putting the completed at the bottom and the yet to be completed at the top
})
const initialState = notesAdapter.getInitialState()
diff --git a/lesson_06-frontend/src/features/users/User.js b/lesson_06-frontend_Redux_RTKQuery/src/features/users/User.js
similarity index 100%
rename from lesson_06-frontend/src/features/users/User.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/users/User.js
diff --git a/lesson_06-frontend/src/features/users/UsersList.js b/lesson_06-frontend_Redux_RTKQuery/src/features/users/UsersList.js
similarity index 100%
rename from lesson_06-frontend/src/features/users/UsersList.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/users/UsersList.js
diff --git a/lesson_06-frontend/src/features/users/usersApiSlice.js b/lesson_06-frontend_Redux_RTKQuery/src/features/users/usersApiSlice.js
similarity index 94%
rename from lesson_06-frontend/src/features/users/usersApiSlice.js
rename to lesson_06-frontend_Redux_RTKQuery/src/features/users/usersApiSlice.js
index addf5a5..b8aaabc 100644
--- a/lesson_06-frontend/src/features/users/usersApiSlice.js
+++ b/lesson_06-frontend_Redux_RTKQuery/src/features/users/usersApiSlice.js
@@ -18,7 +18,7 @@ export const usersApiSlice = apiSlice.injectEndpoints({
keepUnusedDataFor: 5,
transformResponse: responseData => {
const loadedUsers = responseData.map(user => {
- user.id = user._id
+ user.id = user._id // we're using Entity Adapter where the id field takes in id not _id
return user
});
return usersAdapter.setAll(initialState, loadedUsers)
diff --git a/lesson_06-frontend/src/img/background.jpg b/lesson_06-frontend_Redux_RTKQuery/src/img/background.jpg
similarity index 100%
rename from lesson_06-frontend/src/img/background.jpg
rename to lesson_06-frontend_Redux_RTKQuery/src/img/background.jpg
diff --git a/lesson_06-frontend/src/index.css b/lesson_06-frontend_Redux_RTKQuery/src/index.css
similarity index 100%
rename from lesson_06-frontend/src/index.css
rename to lesson_06-frontend_Redux_RTKQuery/src/index.css
diff --git a/lesson_06-frontend/src/index.js b/lesson_06-frontend_Redux_RTKQuery/src/index.js
similarity index 100%
rename from lesson_06-frontend/src/index.js
rename to lesson_06-frontend_Redux_RTKQuery/src/index.js
diff --git a/lesson_07-frontend/.gitignore b/lesson_07-frontend_ReactRedux_Forms/.gitignore
similarity index 100%
rename from lesson_07-frontend/.gitignore
rename to lesson_07-frontend_ReactRedux_Forms/.gitignore
diff --git a/lesson_07-frontend/UserStories.md b/lesson_07-frontend_ReactRedux_Forms/UserStories.md
similarity index 100%
rename from lesson_07-frontend/UserStories.md
rename to lesson_07-frontend_ReactRedux_Forms/UserStories.md
diff --git a/lesson_07-frontend/package-lock.json b/lesson_07-frontend_ReactRedux_Forms/package-lock.json
similarity index 100%
rename from lesson_07-frontend/package-lock.json
rename to lesson_07-frontend_ReactRedux_Forms/package-lock.json
diff --git a/lesson_07-frontend/package.json b/lesson_07-frontend_ReactRedux_Forms/package.json
similarity index 100%
rename from lesson_07-frontend/package.json
rename to lesson_07-frontend_ReactRedux_Forms/package.json
diff --git a/lesson_07-frontend/public/favicon.ico b/lesson_07-frontend_ReactRedux_Forms/public/favicon.ico
similarity index 100%
rename from lesson_07-frontend/public/favicon.ico
rename to lesson_07-frontend_ReactRedux_Forms/public/favicon.ico
diff --git a/lesson_07-frontend/public/img/background.jpg b/lesson_07-frontend_ReactRedux_Forms/public/img/background.jpg
similarity index 100%
rename from lesson_07-frontend/public/img/background.jpg
rename to lesson_07-frontend_ReactRedux_Forms/public/img/background.jpg
diff --git a/lesson_07-frontend/public/index.html b/lesson_07-frontend_ReactRedux_Forms/public/index.html
similarity index 100%
rename from lesson_07-frontend/public/index.html
rename to lesson_07-frontend_ReactRedux_Forms/public/index.html
diff --git a/lesson_07-frontend/public/logo192.png b/lesson_07-frontend_ReactRedux_Forms/public/logo192.png
similarity index 100%
rename from lesson_07-frontend/public/logo192.png
rename to lesson_07-frontend_ReactRedux_Forms/public/logo192.png
diff --git a/lesson_07-frontend/public/logo512.png b/lesson_07-frontend_ReactRedux_Forms/public/logo512.png
similarity index 100%
rename from lesson_07-frontend/public/logo512.png
rename to lesson_07-frontend_ReactRedux_Forms/public/logo512.png
diff --git a/lesson_07-frontend/public/manifest.json b/lesson_07-frontend_ReactRedux_Forms/public/manifest.json
similarity index 100%
rename from lesson_07-frontend/public/manifest.json
rename to lesson_07-frontend_ReactRedux_Forms/public/manifest.json
diff --git a/lesson_07-frontend/public/robots.txt b/lesson_07-frontend_ReactRedux_Forms/public/robots.txt
similarity index 100%
rename from lesson_07-frontend/public/robots.txt
rename to lesson_07-frontend_ReactRedux_Forms/public/robots.txt
diff --git a/lesson_09-frontend/src/App.js b/lesson_07-frontend_ReactRedux_Forms/src/App.js
similarity index 96%
rename from lesson_09-frontend/src/App.js
rename to lesson_07-frontend_ReactRedux_Forms/src/App.js
index a7cc032..b377bba 100644
--- a/lesson_09-frontend/src/App.js
+++ b/lesson_07-frontend_ReactRedux_Forms/src/App.js
@@ -19,6 +19,7 @@ function App() {
} />
} />
+ {/* Wrap the protected routes in Prefetch */}
}>
}>
diff --git a/lesson_07-frontend/src/app/api/apiSlice.js b/lesson_07-frontend_ReactRedux_Forms/src/app/api/apiSlice.js
similarity index 100%
rename from lesson_07-frontend/src/app/api/apiSlice.js
rename to lesson_07-frontend_ReactRedux_Forms/src/app/api/apiSlice.js
diff --git a/lesson_07-frontend/src/app/store.js b/lesson_07-frontend_ReactRedux_Forms/src/app/store.js
similarity index 100%
rename from lesson_07-frontend/src/app/store.js
rename to lesson_07-frontend_ReactRedux_Forms/src/app/store.js
diff --git a/lesson_07-frontend/src/components/DashFooter.js b/lesson_07-frontend_ReactRedux_Forms/src/components/DashFooter.js
similarity index 100%
rename from lesson_07-frontend/src/components/DashFooter.js
rename to lesson_07-frontend_ReactRedux_Forms/src/components/DashFooter.js
diff --git a/lesson_07-frontend/src/components/DashHeader.js b/lesson_07-frontend_ReactRedux_Forms/src/components/DashHeader.js
similarity index 100%
rename from lesson_07-frontend/src/components/DashHeader.js
rename to lesson_07-frontend_ReactRedux_Forms/src/components/DashHeader.js
diff --git a/lesson_07-frontend/src/components/DashLayout.js b/lesson_07-frontend_ReactRedux_Forms/src/components/DashLayout.js
similarity index 100%
rename from lesson_07-frontend/src/components/DashLayout.js
rename to lesson_07-frontend_ReactRedux_Forms/src/components/DashLayout.js
diff --git a/lesson_07-frontend/src/components/Layout.js b/lesson_07-frontend_ReactRedux_Forms/src/components/Layout.js
similarity index 100%
rename from lesson_07-frontend/src/components/Layout.js
rename to lesson_07-frontend_ReactRedux_Forms/src/components/Layout.js
diff --git a/lesson_06-frontend/src/components/Public.js b/lesson_07-frontend_ReactRedux_Forms/src/components/Public.js
similarity index 100%
rename from lesson_06-frontend/src/components/Public.js
rename to lesson_07-frontend_ReactRedux_Forms/src/components/Public.js
diff --git a/lesson_07-frontend/src/config/roles.js b/lesson_07-frontend_ReactRedux_Forms/src/config/roles.js
similarity index 100%
rename from lesson_07-frontend/src/config/roles.js
rename to lesson_07-frontend_ReactRedux_Forms/src/config/roles.js
diff --git a/lesson_07-frontend/src/features/auth/Login.js b/lesson_07-frontend_ReactRedux_Forms/src/features/auth/Login.js
similarity index 100%
rename from lesson_07-frontend/src/features/auth/Login.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/auth/Login.js
diff --git a/lesson_10-frontend/src/features/auth/Prefetch.js b/lesson_07-frontend_ReactRedux_Forms/src/features/auth/Prefetch.js
similarity index 76%
rename from lesson_10-frontend/src/features/auth/Prefetch.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/auth/Prefetch.js
index da8882f..58be5c1 100644
--- a/lesson_10-frontend/src/features/auth/Prefetch.js
+++ b/lesson_07-frontend_ReactRedux_Forms/src/features/auth/Prefetch.js
@@ -7,9 +7,11 @@ import { Outlet } from 'react-router-dom';
const Prefetch = () => {
useEffect(() => {
console.log('subscribing')
+ // manual subcription to notes and users that will remain active
const notes = store.dispatch(notesApiSlice.endpoints.getNotes.initiate())
const users = store.dispatch(usersApiSlice.endpoints.getUsers.initiate())
+ // until they are unsubscribe when gone to unprotected pages
return () => {
console.log('unsubscribing')
notes.unsubscribe()
@@ -17,6 +19,7 @@ const Prefetch = () => {
}
}, [])
+ // wrap our protected pages in this Prefetch component
return
}
export default Prefetch
diff --git a/lesson_07-frontend/src/features/auth/Welcome.js b/lesson_07-frontend_ReactRedux_Forms/src/features/auth/Welcome.js
similarity index 100%
rename from lesson_07-frontend/src/features/auth/Welcome.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/auth/Welcome.js
diff --git a/lesson_07-frontend/src/features/notes/EditNote.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/EditNote.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/EditNote.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/EditNote.js
diff --git a/lesson_07-frontend/src/features/notes/EditNoteForm.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/EditNoteForm.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/EditNoteForm.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/EditNoteForm.js
diff --git a/lesson_07-frontend/src/features/notes/NewNote.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/NewNote.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/NewNote.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/NewNote.js
diff --git a/lesson_07-frontend/src/features/notes/NewNoteForm.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/NewNoteForm.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/NewNoteForm.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/NewNoteForm.js
diff --git a/lesson_07-frontend/src/features/notes/Note.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/Note.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/Note.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/Note.js
diff --git a/lesson_07-frontend/src/features/notes/NotesList.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/NotesList.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/NotesList.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/NotesList.js
diff --git a/lesson_07-frontend/src/features/notes/notesApiSlice.js b/lesson_07-frontend_ReactRedux_Forms/src/features/notes/notesApiSlice.js
similarity index 100%
rename from lesson_07-frontend/src/features/notes/notesApiSlice.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/notes/notesApiSlice.js
diff --git a/lesson_07-frontend/src/features/users/EditUser.js b/lesson_07-frontend_ReactRedux_Forms/src/features/users/EditUser.js
similarity index 100%
rename from lesson_07-frontend/src/features/users/EditUser.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/users/EditUser.js
diff --git a/lesson_11-frontend/src/features/users/EditUserForm.js b/lesson_07-frontend_ReactRedux_Forms/src/features/users/EditUserForm.js
similarity index 98%
rename from lesson_11-frontend/src/features/users/EditUserForm.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/users/EditUserForm.js
index d157d3b..b124411 100644
--- a/lesson_11-frontend/src/features/users/EditUserForm.js
+++ b/lesson_07-frontend_ReactRedux_Forms/src/features/users/EditUserForm.js
@@ -65,6 +65,7 @@ const EditUserForm = ({ user }) => {
const onActiveChanged = () => setActive(prev => !prev)
const onSaveUserClicked = async (e) => {
+ // Present 2 cases where the user wants to update the password vs the user doesn't want to update the password
if (password) {
await updateUser({ id: user.id, username, password, roles, active })
} else {
diff --git a/lesson_07-frontend/src/features/users/NewUserForm.js b/lesson_07-frontend_ReactRedux_Forms/src/features/users/NewUserForm.js
similarity index 100%
rename from lesson_07-frontend/src/features/users/NewUserForm.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/users/NewUserForm.js
diff --git a/lesson_07-frontend/src/features/users/User.js b/lesson_07-frontend_ReactRedux_Forms/src/features/users/User.js
similarity index 100%
rename from lesson_07-frontend/src/features/users/User.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/users/User.js
diff --git a/lesson_09-frontend/src/features/users/UsersList.js b/lesson_07-frontend_ReactRedux_Forms/src/features/users/UsersList.js
similarity index 81%
rename from lesson_09-frontend/src/features/users/UsersList.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/users/UsersList.js
index c6b1967..63af11a 100644
--- a/lesson_09-frontend/src/features/users/UsersList.js
+++ b/lesson_07-frontend_ReactRedux_Forms/src/features/users/UsersList.js
@@ -9,7 +9,13 @@ const UsersList = () => {
isSuccess,
isError,
error
- } = useGetUsersQuery(undefined, {
+ } = useGetUsersQuery(undefined, {
+ // to prevent stale data, refetch the data based on the options below :
+ /*
+ - After 1 min
+ - If we go to another page and then come back to the this page
+ - If the mount or argument changes
+ */
pollingInterval: 60000,
refetchOnFocus: true,
refetchOnMountOrArgChange: true
diff --git a/lesson_07-frontend/src/features/users/usersApiSlice.js b/lesson_07-frontend_ReactRedux_Forms/src/features/users/usersApiSlice.js
similarity index 100%
rename from lesson_07-frontend/src/features/users/usersApiSlice.js
rename to lesson_07-frontend_ReactRedux_Forms/src/features/users/usersApiSlice.js
diff --git a/lesson_07-frontend/src/img/background.jpg b/lesson_07-frontend_ReactRedux_Forms/src/img/background.jpg
similarity index 100%
rename from lesson_07-frontend/src/img/background.jpg
rename to lesson_07-frontend_ReactRedux_Forms/src/img/background.jpg
diff --git a/lesson_07-frontend/src/index.css b/lesson_07-frontend_ReactRedux_Forms/src/index.css
similarity index 100%
rename from lesson_07-frontend/src/index.css
rename to lesson_07-frontend_ReactRedux_Forms/src/index.css
diff --git a/lesson_07-frontend/src/index.js b/lesson_07-frontend_ReactRedux_Forms/src/index.js
similarity index 100%
rename from lesson_07-frontend/src/index.js
rename to lesson_07-frontend_ReactRedux_Forms/src/index.js
diff --git a/lesson_08-backend/config/corsOptions.js b/lesson_08-backend/config/corsOptions.js
deleted file mode 100644
index 715cb85..0000000
--- a/lesson_08-backend/config/corsOptions.js
+++ /dev/null
@@ -1,15 +0,0 @@
-const allowedOrigins = require('./allowedOrigins')
-
-const corsOptions = {
- origin: (origin, callback) => {
- if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
- callback(null, true)
- } else {
- callback(new Error('Not allowed by CORS'))
- }
- },
- credentials: true,
- optionsSuccessStatus: 200
-}
-
-module.exports = corsOptions
\ No newline at end of file
diff --git a/lesson_08-backend/controllers/usersController.js b/lesson_08-backend/controllers/usersController.js
deleted file mode 100644
index 0fb3976..0000000
--- a/lesson_08-backend/controllers/usersController.js
+++ /dev/null
@@ -1,130 +0,0 @@
-const User = require('../models/User')
-const Note = require('../models/Note')
-const asyncHandler = require('express-async-handler')
-const bcrypt = require('bcrypt')
-
-// @desc Get all users
-// @route GET /users
-// @access Private
-const getAllUsers = asyncHandler(async (req, res) => {
- // Get all users from MongoDB
- const users = await User.find().select('-password').lean()
-
- // If no users
- if (!users?.length) {
- return res.status(400).json({ message: 'No users found' })
- }
-
- res.json(users)
-})
-
-// @desc Create new user
-// @route POST /users
-// @access Private
-const createNewUser = asyncHandler(async (req, res) => {
- const { username, password, roles } = req.body
-
- // Confirm data
- if (!username || !password || !Array.isArray(roles) || !roles.length) {
- return res.status(400).json({ message: 'All fields are required' })
- }
-
- // Check for duplicate username
- const duplicate = await User.findOne({ username }).lean().exec()
-
- if (duplicate) {
- return res.status(409).json({ message: 'Duplicate username' })
- }
-
- // Hash password
- const hashedPwd = await bcrypt.hash(password, 10) // salt rounds
-
- const userObject = { username, "password": hashedPwd, roles }
-
- // Create and store new user
- const user = await User.create(userObject)
-
- if (user) { //created
- res.status(201).json({ message: `New user ${username} created` })
- } else {
- res.status(400).json({ message: 'Invalid user data received' })
- }
-})
-
-// @desc Update a user
-// @route PATCH /users
-// @access Private
-const updateUser = asyncHandler(async (req, res) => {
- const { id, username, roles, active, password } = req.body
-
- // Confirm data
- if (!id || !username || !Array.isArray(roles) || !roles.length || typeof active !== 'boolean') {
- return res.status(400).json({ message: 'All fields except password are required' })
- }
-
- // Does the user exist to update?
- const user = await User.findById(id).exec()
-
- if (!user) {
- return res.status(400).json({ message: 'User not found' })
- }
-
- // Check for duplicate
- const duplicate = await User.findOne({ username }).lean().exec()
-
- // Allow updates to the original user
- if (duplicate && duplicate?._id.toString() !== id) {
- return res.status(409).json({ message: 'Duplicate username' })
- }
-
- user.username = username
- user.roles = roles
- user.active = active
-
- if (password) {
- // Hash password
- user.password = await bcrypt.hash(password, 10) // salt rounds
- }
-
- const updatedUser = await user.save()
-
- res.json({ message: `${updatedUser.username} updated` })
-})
-
-// @desc Delete a user
-// @route DELETE /users
-// @access Private
-const deleteUser = asyncHandler(async (req, res) => {
- const { id } = req.body
-
- // Confirm data
- if (!id) {
- return res.status(400).json({ message: 'User ID Required' })
- }
-
- // Does the user still have assigned notes?
- const note = await Note.findOne({ user: id }).lean().exec()
- if (note) {
- return res.status(400).json({ message: 'User has assigned notes' })
- }
-
- // Does the user exist to delete?
- const user = await User.findById(id).exec()
-
- if (!user) {
- return res.status(400).json({ message: 'User not found' })
- }
-
- const result = await user.deleteOne()
-
- const reply = `Username ${result.username} with ID ${result._id} deleted`
-
- res.json(reply)
-})
-
-module.exports = {
- getAllUsers,
- createNewUser,
- updateUser,
- deleteUser
-}
\ No newline at end of file
diff --git a/lesson_08-backend/middleware/errorHandler.js b/lesson_08-backend/middleware/errorHandler.js
deleted file mode 100644
index d002af5..0000000
--- a/lesson_08-backend/middleware/errorHandler.js
+++ /dev/null
@@ -1,14 +0,0 @@
-const { logEvents } = require('./logger')
-
-const errorHandler = (err, req, res, next) => {
- logEvents(`${err.name}: ${err.message}\t${req.method}\t${req.url}\t${req.headers.origin}`, 'errLog.log')
- console.log(err.stack)
-
- const status = res.statusCode ? res.statusCode : 500 // server error
-
- res.status(status)
-
- res.json({ message: err.message })
-}
-
-module.exports = errorHandler
\ No newline at end of file
diff --git a/lesson_08-backend/middleware/logger.js b/lesson_08-backend/middleware/logger.js
deleted file mode 100644
index 25c8240..0000000
--- a/lesson_08-backend/middleware/logger.js
+++ /dev/null
@@ -1,27 +0,0 @@
-const { format } = require('date-fns')
-const { v4: uuid } = require('uuid')
-const fs = require('fs')
-const fsPromises = require('fs').promises
-const path = require('path')
-
-const logEvents = async (message, logFileName) => {
- const dateTime = format(new Date(), 'yyyyMMdd\tHH:mm:ss')
- const logItem = `${dateTime}\t${uuid()}\t${message}\n`
-
- try {
- if (!fs.existsSync(path.join(__dirname, '..', 'logs'))) {
- await fsPromises.mkdir(path.join(__dirname, '..', 'logs'))
- }
- await fsPromises.appendFile(path.join(__dirname, '..', 'logs', logFileName), logItem)
- } catch (err) {
- console.log(err)
- }
-}
-
-const logger = (req, res, next) => {
- logEvents(`${req.method}\t${req.url}\t${req.headers.origin}`, 'reqLog.log')
- console.log(`${req.method} ${req.path}`)
- next()
-}
-
-module.exports = { logEvents, logger }
\ No newline at end of file
diff --git a/lesson_08-backend/models/Note.js b/lesson_08-backend/models/Note.js
deleted file mode 100644
index 7e94735..0000000
--- a/lesson_08-backend/models/Note.js
+++ /dev/null
@@ -1,35 +0,0 @@
-const mongoose = require('mongoose')
-const AutoIncrement = require('mongoose-sequence')(mongoose)
-
-const noteSchema = new mongoose.Schema(
- {
- user: {
- type: mongoose.Schema.Types.ObjectId,
- required: true,
- ref: 'User'
- },
- title: {
- type: String,
- required: true
- },
- text: {
- type: String,
- required: true
- },
- completed: {
- type: Boolean,
- default: false
- }
- },
- {
- timestamps: true
- }
-)
-
-noteSchema.plugin(AutoIncrement, {
- inc_field: 'ticket',
- id: 'ticketNums',
- start_seq: 500
-})
-
-module.exports = mongoose.model('Note', noteSchema)
\ No newline at end of file
diff --git a/lesson_08-backend/routes/root.js b/lesson_08-backend/routes/root.js
deleted file mode 100644
index d4d8d79..0000000
--- a/lesson_08-backend/routes/root.js
+++ /dev/null
@@ -1,9 +0,0 @@
-const express = require('express')
-const router = express.Router()
-const path = require('path')
-
-router.get('^/$|/index(.html)?', (req, res) => {
- res.sendFile(path.join(__dirname, '..', 'views', 'index.html'))
-})
-
-module.exports = router
\ No newline at end of file
diff --git a/lesson_08-backend/.gitignore b/lesson_08-backend_Authentication&Authorisation_JWT/.gitignore
similarity index 100%
rename from lesson_08-backend/.gitignore
rename to lesson_08-backend_Authentication&Authorisation_JWT/.gitignore
diff --git a/lesson_08-backend/UserStories.md b/lesson_08-backend_Authentication&Authorisation_JWT/UserStories.md
similarity index 100%
rename from lesson_08-backend/UserStories.md
rename to lesson_08-backend_Authentication&Authorisation_JWT/UserStories.md
diff --git a/lesson_08-backend/config/allowedOrigins.js b/lesson_08-backend_Authentication&Authorisation_JWT/config/allowedOrigins.js
similarity index 100%
rename from lesson_08-backend/config/allowedOrigins.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/config/allowedOrigins.js
diff --git a/lesson_04/config/corsOptions.js b/lesson_08-backend_Authentication&Authorisation_JWT/config/corsOptions.js
similarity index 100%
rename from lesson_04/config/corsOptions.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/config/corsOptions.js
diff --git a/lesson_08-backend/config/dbConn.js b/lesson_08-backend_Authentication&Authorisation_JWT/config/dbConn.js
similarity index 100%
rename from lesson_08-backend/config/dbConn.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/config/dbConn.js
diff --git a/lesson_08-backend/controllers/authController.js b/lesson_08-backend_Authentication&Authorisation_JWT/controllers/authController.js
similarity index 92%
rename from lesson_08-backend/controllers/authController.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/controllers/authController.js
index 2f5cc02..49dc39e 100644
--- a/lesson_08-backend/controllers/authController.js
+++ b/lesson_08-backend_Authentication&Authorisation_JWT/controllers/authController.js
@@ -6,6 +6,9 @@ const asyncHandler = require('express-async-handler')
// @desc Login
// @route POST /auth
// @access Public
+/*
+ Authenticates registered users and generates them 2 JWT tokens namely an Access Token and a Refresh Token, and returns a cookie with the Refresh Token in it.
+*/
const login = asyncHandler(async (req, res) => {
const { username, password } = req.body
@@ -55,6 +58,9 @@ const login = asyncHandler(async (req, res) => {
// @desc Refresh
// @route GET /auth/refresh
// @access Public - because access token has expired
+/*
+ Allows user with Refresh Token to get a new Access Token after it has expired.
+*/
const refresh = (req, res) => {
const cookies = req.cookies
diff --git a/lesson_08-backend/controllers/notesController.js b/lesson_08-backend_Authentication&Authorisation_JWT/controllers/notesController.js
similarity index 100%
rename from lesson_08-backend/controllers/notesController.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/controllers/notesController.js
diff --git a/lesson_04/controllers/usersController.js b/lesson_08-backend_Authentication&Authorisation_JWT/controllers/usersController.js
similarity index 100%
rename from lesson_04/controllers/usersController.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/controllers/usersController.js
diff --git a/lesson_04/middleware/errorHandler.js b/lesson_08-backend_Authentication&Authorisation_JWT/middleware/errorHandler.js
similarity index 100%
rename from lesson_04/middleware/errorHandler.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/middleware/errorHandler.js
diff --git a/lesson_04/middleware/logger.js b/lesson_08-backend_Authentication&Authorisation_JWT/middleware/logger.js
similarity index 100%
rename from lesson_04/middleware/logger.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/middleware/logger.js
diff --git a/lesson_08-backend/middleware/loginLimiter.js b/lesson_08-backend_Authentication&Authorisation_JWT/middleware/loginLimiter.js
similarity index 100%
rename from lesson_08-backend/middleware/loginLimiter.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/middleware/loginLimiter.js
diff --git a/lesson_08-backend/middleware/verifyJWT.js b/lesson_08-backend_Authentication&Authorisation_JWT/middleware/verifyJWT.js
similarity index 100%
rename from lesson_08-backend/middleware/verifyJWT.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/middleware/verifyJWT.js
diff --git a/lesson_04/models/Note.js b/lesson_08-backend_Authentication&Authorisation_JWT/models/Note.js
similarity index 100%
rename from lesson_04/models/Note.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/models/Note.js
diff --git a/lesson_08-backend/models/User.js b/lesson_08-backend_Authentication&Authorisation_JWT/models/User.js
similarity index 100%
rename from lesson_08-backend/models/User.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/models/User.js
diff --git a/lesson_08-backend/package-lock.json b/lesson_08-backend_Authentication&Authorisation_JWT/package-lock.json
similarity index 100%
rename from lesson_08-backend/package-lock.json
rename to lesson_08-backend_Authentication&Authorisation_JWT/package-lock.json
diff --git a/lesson_08-backend/package.json b/lesson_08-backend_Authentication&Authorisation_JWT/package.json
similarity index 100%
rename from lesson_08-backend/package.json
rename to lesson_08-backend_Authentication&Authorisation_JWT/package.json
diff --git a/lesson_08-backend/public/css/style.css b/lesson_08-backend_Authentication&Authorisation_JWT/public/css/style.css
similarity index 100%
rename from lesson_08-backend/public/css/style.css
rename to lesson_08-backend_Authentication&Authorisation_JWT/public/css/style.css
diff --git a/lesson_08-backend/routes/authRoutes.js b/lesson_08-backend_Authentication&Authorisation_JWT/routes/authRoutes.js
similarity index 100%
rename from lesson_08-backend/routes/authRoutes.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/routes/authRoutes.js
diff --git a/lesson_08-backend/routes/noteRoutes.js b/lesson_08-backend_Authentication&Authorisation_JWT/routes/noteRoutes.js
similarity index 100%
rename from lesson_08-backend/routes/noteRoutes.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/routes/noteRoutes.js
diff --git a/lesson_03/routes/root.js b/lesson_08-backend_Authentication&Authorisation_JWT/routes/root.js
similarity index 100%
rename from lesson_03/routes/root.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/routes/root.js
diff --git a/lesson_08-backend/routes/userRoutes.js b/lesson_08-backend_Authentication&Authorisation_JWT/routes/userRoutes.js
similarity index 100%
rename from lesson_08-backend/routes/userRoutes.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/routes/userRoutes.js
diff --git a/lesson_08-backend/server.js b/lesson_08-backend_Authentication&Authorisation_JWT/server.js
similarity index 100%
rename from lesson_08-backend/server.js
rename to lesson_08-backend_Authentication&Authorisation_JWT/server.js
diff --git a/lesson_08-backend/views/404.html b/lesson_08-backend_Authentication&Authorisation_JWT/views/404.html
similarity index 100%
rename from lesson_08-backend/views/404.html
rename to lesson_08-backend_Authentication&Authorisation_JWT/views/404.html
diff --git a/lesson_08-backend/views/index.html b/lesson_08-backend_Authentication&Authorisation_JWT/views/index.html
similarity index 100%
rename from lesson_08-backend/views/index.html
rename to lesson_08-backend_Authentication&Authorisation_JWT/views/index.html
diff --git a/lesson_09-frontend/.gitignore b/lesson_09-frontend_LoginAuth/.gitignore
similarity index 100%
rename from lesson_09-frontend/.gitignore
rename to lesson_09-frontend_LoginAuth/.gitignore
diff --git a/lesson_09-frontend/UserStories.md b/lesson_09-frontend_LoginAuth/UserStories.md
similarity index 100%
rename from lesson_09-frontend/UserStories.md
rename to lesson_09-frontend_LoginAuth/UserStories.md
diff --git a/lesson_09-frontend/package-lock.json b/lesson_09-frontend_LoginAuth/package-lock.json
similarity index 99%
rename from lesson_09-frontend/package-lock.json
rename to lesson_09-frontend_LoginAuth/package-lock.json
index dafaa22..a4f5e41 100644
--- a/lesson_09-frontend/package-lock.json
+++ b/lesson_09-frontend_LoginAuth/package-lock.json
@@ -1,11 +1,11 @@
{
- "name": "lesson_06-frontend",
+ "name": "lesson_09-frontend",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
- "name": "lesson_06-frontend",
+ "name": "lesson_09-frontend",
"version": "0.1.0",
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.1.2",
diff --git a/lesson_09-frontend/package.json b/lesson_09-frontend_LoginAuth/package.json
similarity index 100%
rename from lesson_09-frontend/package.json
rename to lesson_09-frontend_LoginAuth/package.json
diff --git a/lesson_09-frontend/public/favicon.ico b/lesson_09-frontend_LoginAuth/public/favicon.ico
similarity index 100%
rename from lesson_09-frontend/public/favicon.ico
rename to lesson_09-frontend_LoginAuth/public/favicon.ico
diff --git a/lesson_09-frontend/public/img/background.jpg b/lesson_09-frontend_LoginAuth/public/img/background.jpg
similarity index 100%
rename from lesson_09-frontend/public/img/background.jpg
rename to lesson_09-frontend_LoginAuth/public/img/background.jpg
diff --git a/lesson_09-frontend/public/index.html b/lesson_09-frontend_LoginAuth/public/index.html
similarity index 100%
rename from lesson_09-frontend/public/index.html
rename to lesson_09-frontend_LoginAuth/public/index.html
diff --git a/lesson_09-frontend/public/logo192.png b/lesson_09-frontend_LoginAuth/public/logo192.png
similarity index 100%
rename from lesson_09-frontend/public/logo192.png
rename to lesson_09-frontend_LoginAuth/public/logo192.png
diff --git a/lesson_09-frontend/public/logo512.png b/lesson_09-frontend_LoginAuth/public/logo512.png
similarity index 100%
rename from lesson_09-frontend/public/logo512.png
rename to lesson_09-frontend_LoginAuth/public/logo512.png
diff --git a/lesson_09-frontend/public/manifest.json b/lesson_09-frontend_LoginAuth/public/manifest.json
similarity index 100%
rename from lesson_09-frontend/public/manifest.json
rename to lesson_09-frontend_LoginAuth/public/manifest.json
diff --git a/lesson_09-frontend/public/robots.txt b/lesson_09-frontend_LoginAuth/public/robots.txt
similarity index 100%
rename from lesson_09-frontend/public/robots.txt
rename to lesson_09-frontend_LoginAuth/public/robots.txt
diff --git a/lesson_07-frontend/src/App.js b/lesson_09-frontend_LoginAuth/src/App.js
similarity index 100%
rename from lesson_07-frontend/src/App.js
rename to lesson_09-frontend_LoginAuth/src/App.js
diff --git a/lesson_09-frontend/src/app/api/apiSlice.js b/lesson_09-frontend_LoginAuth/src/app/api/apiSlice.js
similarity index 100%
rename from lesson_09-frontend/src/app/api/apiSlice.js
rename to lesson_09-frontend_LoginAuth/src/app/api/apiSlice.js
diff --git a/lesson_09-frontend/src/app/store.js b/lesson_09-frontend_LoginAuth/src/app/store.js
similarity index 100%
rename from lesson_09-frontend/src/app/store.js
rename to lesson_09-frontend_LoginAuth/src/app/store.js
diff --git a/lesson_09-frontend/src/components/DashFooter.js b/lesson_09-frontend_LoginAuth/src/components/DashFooter.js
similarity index 100%
rename from lesson_09-frontend/src/components/DashFooter.js
rename to lesson_09-frontend_LoginAuth/src/components/DashFooter.js
diff --git a/lesson_10-frontend/src/components/DashHeader.js b/lesson_09-frontend_LoginAuth/src/components/DashHeader.js
similarity index 91%
rename from lesson_10-frontend/src/components/DashHeader.js
rename to lesson_09-frontend_LoginAuth/src/components/DashHeader.js
index 1a860db..7f5510a 100644
--- a/lesson_10-frontend/src/components/DashHeader.js
+++ b/lesson_09-frontend_LoginAuth/src/components/DashHeader.js
@@ -5,6 +5,7 @@ import { useNavigate, Link, useLocation } from 'react-router-dom'
import { useSendLogoutMutation } from '../features/auth/authApiSlice'
+// use these to compare the location that we are on to decide whether to show a button or not
const DASH_REGEX = /^\/dash(\/)?$/
const NOTES_REGEX = /^\/dash\/notes(\/)?$/
const USERS_REGEX = /^\/dash\/users(\/)?$/
@@ -30,6 +31,7 @@ const DashHeader = () => {
if (isError) return
Error: {error.data?.message}
let dashClass = null
+ // make sure we're not on any of the paths mentioned by REGEX
if (!DASH_REGEX.test(pathname) && !NOTES_REGEX.test(pathname) && !USERS_REGEX.test(pathname)) {
dashClass = "dash-header__container--small"
}
diff --git a/lesson_09-frontend/src/components/DashLayout.js b/lesson_09-frontend_LoginAuth/src/components/DashLayout.js
similarity index 100%
rename from lesson_09-frontend/src/components/DashLayout.js
rename to lesson_09-frontend_LoginAuth/src/components/DashLayout.js
diff --git a/lesson_09-frontend/src/components/Layout.js b/lesson_09-frontend_LoginAuth/src/components/Layout.js
similarity index 100%
rename from lesson_09-frontend/src/components/Layout.js
rename to lesson_09-frontend_LoginAuth/src/components/Layout.js
diff --git a/lesson_07-frontend/src/components/Public.js b/lesson_09-frontend_LoginAuth/src/components/Public.js
similarity index 100%
rename from lesson_07-frontend/src/components/Public.js
rename to lesson_09-frontend_LoginAuth/src/components/Public.js
diff --git a/lesson_09-frontend/src/config/roles.js b/lesson_09-frontend_LoginAuth/src/config/roles.js
similarity index 100%
rename from lesson_09-frontend/src/config/roles.js
rename to lesson_09-frontend_LoginAuth/src/config/roles.js
diff --git a/lesson_09-frontend/src/features/auth/Login.js b/lesson_09-frontend_LoginAuth/src/features/auth/Login.js
similarity index 96%
rename from lesson_09-frontend/src/features/auth/Login.js
rename to lesson_09-frontend_LoginAuth/src/features/auth/Login.js
index 709bcb4..40b0b12 100644
--- a/lesson_09-frontend/src/features/auth/Login.js
+++ b/lesson_09-frontend_LoginAuth/src/features/auth/Login.js
@@ -21,6 +21,7 @@ const Login = () => {
userRef.current.focus()
}, [])
+ // They already process the error on previous attempts, so there is no need for the old errMsg
useEffect(() => {
setErrMsg('');
}, [username, password])
diff --git a/lesson_07-frontend/src/features/auth/Prefetch.js b/lesson_09-frontend_LoginAuth/src/features/auth/Prefetch.js
similarity index 100%
rename from lesson_07-frontend/src/features/auth/Prefetch.js
rename to lesson_09-frontend_LoginAuth/src/features/auth/Prefetch.js
diff --git a/lesson_09-frontend/src/features/auth/Welcome.js b/lesson_09-frontend_LoginAuth/src/features/auth/Welcome.js
similarity index 100%
rename from lesson_09-frontend/src/features/auth/Welcome.js
rename to lesson_09-frontend_LoginAuth/src/features/auth/Welcome.js
diff --git a/lesson_09-frontend/src/features/auth/authApiSlice.js b/lesson_09-frontend_LoginAuth/src/features/auth/authApiSlice.js
similarity index 67%
rename from lesson_09-frontend/src/features/auth/authApiSlice.js
rename to lesson_09-frontend_LoginAuth/src/features/auth/authApiSlice.js
index 4acd362..dda79c1 100644
--- a/lesson_09-frontend/src/features/auth/authApiSlice.js
+++ b/lesson_09-frontend_LoginAuth/src/features/auth/authApiSlice.js
@@ -18,10 +18,10 @@ export const authApiSlice = apiSlice.injectEndpoints({
async onQueryStarted(arg, { dispatch, queryFulfilled }) {
try {
//const { data } =
- await queryFulfilled
+ await queryFulfilled // A promise that resolves once the mutation completes, waits for the "sendLogout" mutation to complete, it pauses the execution until the logout request to the server finishes.
//console.log(data)
- dispatch(logOut())
- dispatch(apiSlice.util.resetApiState())
+ dispatch(logOut()) // dispatch logout to update the auth slice state to indicate that the user is logged out
+ dispatch(apiSlice.util.resetApiState()) // dispatch an action to reset the state of the Api slice, clearing any cached data related to the Api requests
} catch (err) {
console.log(err)
}
diff --git a/lesson_09-frontend/src/features/auth/authSlice.js b/lesson_09-frontend_LoginAuth/src/features/auth/authSlice.js
similarity index 100%
rename from lesson_09-frontend/src/features/auth/authSlice.js
rename to lesson_09-frontend_LoginAuth/src/features/auth/authSlice.js
diff --git a/lesson_09-frontend/src/features/notes/EditNote.js b/lesson_09-frontend_LoginAuth/src/features/notes/EditNote.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/EditNote.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/EditNote.js
diff --git a/lesson_09-frontend/src/features/notes/EditNoteForm.js b/lesson_09-frontend_LoginAuth/src/features/notes/EditNoteForm.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/EditNoteForm.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/EditNoteForm.js
diff --git a/lesson_09-frontend/src/features/notes/NewNote.js b/lesson_09-frontend_LoginAuth/src/features/notes/NewNote.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/NewNote.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/NewNote.js
diff --git a/lesson_09-frontend/src/features/notes/NewNoteForm.js b/lesson_09-frontend_LoginAuth/src/features/notes/NewNoteForm.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/NewNoteForm.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/NewNoteForm.js
diff --git a/lesson_09-frontend/src/features/notes/Note.js b/lesson_09-frontend_LoginAuth/src/features/notes/Note.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/Note.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/Note.js
diff --git a/lesson_09-frontend/src/features/notes/NotesList.js b/lesson_09-frontend_LoginAuth/src/features/notes/NotesList.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/NotesList.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/NotesList.js
diff --git a/lesson_09-frontend/src/features/notes/notesApiSlice.js b/lesson_09-frontend_LoginAuth/src/features/notes/notesApiSlice.js
similarity index 100%
rename from lesson_09-frontend/src/features/notes/notesApiSlice.js
rename to lesson_09-frontend_LoginAuth/src/features/notes/notesApiSlice.js
diff --git a/lesson_09-frontend/src/features/users/EditUser.js b/lesson_09-frontend_LoginAuth/src/features/users/EditUser.js
similarity index 100%
rename from lesson_09-frontend/src/features/users/EditUser.js
rename to lesson_09-frontend_LoginAuth/src/features/users/EditUser.js
diff --git a/lesson_07-frontend/src/features/users/EditUserForm.js b/lesson_09-frontend_LoginAuth/src/features/users/EditUserForm.js
similarity index 100%
rename from lesson_07-frontend/src/features/users/EditUserForm.js
rename to lesson_09-frontend_LoginAuth/src/features/users/EditUserForm.js
diff --git a/lesson_09-frontend/src/features/users/NewUserForm.js b/lesson_09-frontend_LoginAuth/src/features/users/NewUserForm.js
similarity index 100%
rename from lesson_09-frontend/src/features/users/NewUserForm.js
rename to lesson_09-frontend_LoginAuth/src/features/users/NewUserForm.js
diff --git a/lesson_09-frontend/src/features/users/User.js b/lesson_09-frontend_LoginAuth/src/features/users/User.js
similarity index 100%
rename from lesson_09-frontend/src/features/users/User.js
rename to lesson_09-frontend_LoginAuth/src/features/users/User.js
diff --git a/lesson_07-frontend/src/features/users/UsersList.js b/lesson_09-frontend_LoginAuth/src/features/users/UsersList.js
similarity index 100%
rename from lesson_07-frontend/src/features/users/UsersList.js
rename to lesson_09-frontend_LoginAuth/src/features/users/UsersList.js
diff --git a/lesson_09-frontend/src/features/users/usersApiSlice.js b/lesson_09-frontend_LoginAuth/src/features/users/usersApiSlice.js
similarity index 100%
rename from lesson_09-frontend/src/features/users/usersApiSlice.js
rename to lesson_09-frontend_LoginAuth/src/features/users/usersApiSlice.js
diff --git a/lesson_09-frontend/src/img/background.jpg b/lesson_09-frontend_LoginAuth/src/img/background.jpg
similarity index 100%
rename from lesson_09-frontend/src/img/background.jpg
rename to lesson_09-frontend_LoginAuth/src/img/background.jpg
diff --git a/lesson_09-frontend/src/index.css b/lesson_09-frontend_LoginAuth/src/index.css
similarity index 100%
rename from lesson_09-frontend/src/index.css
rename to lesson_09-frontend_LoginAuth/src/index.css
diff --git a/lesson_09-frontend/src/index.js b/lesson_09-frontend_LoginAuth/src/index.js
similarity index 100%
rename from lesson_09-frontend/src/index.js
rename to lesson_09-frontend_LoginAuth/src/index.js
diff --git a/lesson_10-frontend/.gitignore b/lesson_10-frontend_JWT_Auth/.gitignore
similarity index 100%
rename from lesson_10-frontend/.gitignore
rename to lesson_10-frontend_JWT_Auth/.gitignore
diff --git a/lesson_10-frontend/UserStories.md b/lesson_10-frontend_JWT_Auth/UserStories.md
similarity index 100%
rename from lesson_10-frontend/UserStories.md
rename to lesson_10-frontend_JWT_Auth/UserStories.md
diff --git a/lesson_10-frontend/package-lock.json b/lesson_10-frontend_JWT_Auth/package-lock.json
similarity index 99%
rename from lesson_10-frontend/package-lock.json
rename to lesson_10-frontend_JWT_Auth/package-lock.json
index dafaa22..64fce62 100644
--- a/lesson_10-frontend/package-lock.json
+++ b/lesson_10-frontend_JWT_Auth/package-lock.json
@@ -1,11 +1,11 @@
{
- "name": "lesson_06-frontend",
+ "name": "lesson_10-frontend",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
- "name": "lesson_06-frontend",
+ "name": "lesson_10-frontend",
"version": "0.1.0",
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.1.2",
diff --git a/lesson_10-frontend/package.json b/lesson_10-frontend_JWT_Auth/package.json
similarity index 100%
rename from lesson_10-frontend/package.json
rename to lesson_10-frontend_JWT_Auth/package.json
diff --git a/lesson_10-frontend/public/favicon.ico b/lesson_10-frontend_JWT_Auth/public/favicon.ico
similarity index 100%
rename from lesson_10-frontend/public/favicon.ico
rename to lesson_10-frontend_JWT_Auth/public/favicon.ico
diff --git a/lesson_10-frontend/public/img/background.jpg b/lesson_10-frontend_JWT_Auth/public/img/background.jpg
similarity index 100%
rename from lesson_10-frontend/public/img/background.jpg
rename to lesson_10-frontend_JWT_Auth/public/img/background.jpg
diff --git a/lesson_10-frontend/public/index.html b/lesson_10-frontend_JWT_Auth/public/index.html
similarity index 100%
rename from lesson_10-frontend/public/index.html
rename to lesson_10-frontend_JWT_Auth/public/index.html
diff --git a/lesson_10-frontend/public/logo192.png b/lesson_10-frontend_JWT_Auth/public/logo192.png
similarity index 100%
rename from lesson_10-frontend/public/logo192.png
rename to lesson_10-frontend_JWT_Auth/public/logo192.png
diff --git a/lesson_10-frontend/public/logo512.png b/lesson_10-frontend_JWT_Auth/public/logo512.png
similarity index 100%
rename from lesson_10-frontend/public/logo512.png
rename to lesson_10-frontend_JWT_Auth/public/logo512.png
diff --git a/lesson_10-frontend/public/manifest.json b/lesson_10-frontend_JWT_Auth/public/manifest.json
similarity index 100%
rename from lesson_10-frontend/public/manifest.json
rename to lesson_10-frontend_JWT_Auth/public/manifest.json
diff --git a/lesson_10-frontend/public/robots.txt b/lesson_10-frontend_JWT_Auth/public/robots.txt
similarity index 100%
rename from lesson_10-frontend/public/robots.txt
rename to lesson_10-frontend_JWT_Auth/public/robots.txt
diff --git a/lesson_10-frontend/src/App.js b/lesson_10-frontend_JWT_Auth/src/App.js
similarity index 100%
rename from lesson_10-frontend/src/App.js
rename to lesson_10-frontend_JWT_Auth/src/App.js
diff --git a/lesson_10-frontend/src/app/api/apiSlice.js b/lesson_10-frontend_JWT_Auth/src/app/api/apiSlice.js
similarity index 74%
rename from lesson_10-frontend/src/app/api/apiSlice.js
rename to lesson_10-frontend_JWT_Auth/src/app/api/apiSlice.js
index 591a9f3..efc0737 100644
--- a/lesson_10-frontend/src/app/api/apiSlice.js
+++ b/lesson_10-frontend_JWT_Auth/src/app/api/apiSlice.js
@@ -3,9 +3,9 @@ import { setCredentials } from '../../features/auth/authSlice'
const baseQuery = fetchBaseQuery({
baseUrl: 'http://localhost:3500',
- credentials: 'include',
+ credentials: 'include', // ensures that cookies are sent with every request,important when making requests to a server that relies on cookies for maintaining a user's session (e.g., for authentication).
prepareHeaders: (headers, { getState }) => {
- const token = getState().auth.token
+ const token = getState().auth.token // retrieve the current state from the Redux store
if (token) {
headers.set("authorization", `Bearer ${token}`)
@@ -19,7 +19,14 @@ const baseQueryWithReauth = async (args, api, extraOptions) => {
// console.log(api) // signal, dispatch, getState()
// console.log(extraOptions) //custom like {shout: true}
- let result = await baseQuery(args, api, extraOptions)
+ let result = await baseQuery(args, api, extraOptions) // get the access token from the cookie
+ /*
+ fetchBaseQuery returns :
+ {
+ data: ...,
+ error: ...
+ }
+ */
// If you want, handle other status codes, too
if (result?.error?.status === 403) {
diff --git a/lesson_10-frontend/src/app/store.js b/lesson_10-frontend_JWT_Auth/src/app/store.js
similarity index 100%
rename from lesson_10-frontend/src/app/store.js
rename to lesson_10-frontend_JWT_Auth/src/app/store.js
diff --git a/lesson_10-frontend/src/components/DashFooter.js b/lesson_10-frontend_JWT_Auth/src/components/DashFooter.js
similarity index 100%
rename from lesson_10-frontend/src/components/DashFooter.js
rename to lesson_10-frontend_JWT_Auth/src/components/DashFooter.js
diff --git a/lesson_09-frontend/src/components/DashHeader.js b/lesson_10-frontend_JWT_Auth/src/components/DashHeader.js
similarity index 100%
rename from lesson_09-frontend/src/components/DashHeader.js
rename to lesson_10-frontend_JWT_Auth/src/components/DashHeader.js
diff --git a/lesson_10-frontend/src/components/DashLayout.js b/lesson_10-frontend_JWT_Auth/src/components/DashLayout.js
similarity index 100%
rename from lesson_10-frontend/src/components/DashLayout.js
rename to lesson_10-frontend_JWT_Auth/src/components/DashLayout.js
diff --git a/lesson_10-frontend/src/components/Layout.js b/lesson_10-frontend_JWT_Auth/src/components/Layout.js
similarity index 100%
rename from lesson_10-frontend/src/components/Layout.js
rename to lesson_10-frontend_JWT_Auth/src/components/Layout.js
diff --git a/lesson_09-frontend/src/components/Public.js b/lesson_10-frontend_JWT_Auth/src/components/Public.js
similarity index 100%
rename from lesson_09-frontend/src/components/Public.js
rename to lesson_10-frontend_JWT_Auth/src/components/Public.js
diff --git a/lesson_10-frontend/src/config/roles.js b/lesson_10-frontend_JWT_Auth/src/config/roles.js
similarity index 100%
rename from lesson_10-frontend/src/config/roles.js
rename to lesson_10-frontend_JWT_Auth/src/config/roles.js
diff --git a/lesson_10-frontend/src/features/auth/Login.js b/lesson_10-frontend_JWT_Auth/src/features/auth/Login.js
similarity index 100%
rename from lesson_10-frontend/src/features/auth/Login.js
rename to lesson_10-frontend_JWT_Auth/src/features/auth/Login.js
diff --git a/lesson_10-frontend/src/features/auth/PersistLogin.js b/lesson_10-frontend_JWT_Auth/src/features/auth/PersistLogin.js
similarity index 69%
rename from lesson_10-frontend/src/features/auth/PersistLogin.js
rename to lesson_10-frontend_JWT_Auth/src/features/auth/PersistLogin.js
index 3294e60..9e05cfa 100644
--- a/lesson_10-frontend/src/features/auth/PersistLogin.js
+++ b/lesson_10-frontend_JWT_Auth/src/features/auth/PersistLogin.js
@@ -9,12 +9,12 @@ const PersistLogin = () => {
const [persist] = usePersist()
const token = useSelector(selectCurrentToken)
- const effectRan = useRef(false)
+ const effectRan = useRef(false)
const [trueSuccess, setTrueSuccess] = useState(false)
const [refresh, {
- isUninitialized,
+ isUninitialized, // refresh function hasn't been called yet
isLoading,
isSuccess,
isError,
@@ -23,7 +23,10 @@ const PersistLogin = () => {
useEffect(() => {
-
+ // This will run once the component is mounted
+ /*
+ effectRan is to make sure certain operations inside the effect (like verifying the refresh token) do not repeat.
+ */
if (effectRan.current === true || process.env.NODE_ENV !== 'development') { // React 18 Strict Mode
const verifyRefreshToken = async () => {
@@ -32,6 +35,11 @@ const PersistLogin = () => {
//const response =
await refresh()
//const { accessToken } = response.data
+ /*
+ Why do we need the trueSucess state ?
+ - The useRefreshMutation can be in the isSuccess state without setting the credentials first.
+ - We need one more flag to provide that extra little bit of time to set the credentials.
+ */
setTrueSuccess(true)
}
catch (err) {
@@ -41,7 +49,7 @@ const PersistLogin = () => {
if (!token && persist) verifyRefreshToken()
}
-
+ // This will run when the component is unmounted
return () => effectRan.current = true
// eslint-disable-next-line
@@ -49,10 +57,10 @@ const PersistLogin = () => {
let content
- if (!persist) { // persist: no
+ if (!persist) { // persist: no (we do not want to persist the logins)
console.log('no persist')
content =
- } else if (isLoading) { //persist: yes, token: no
+ } else if (isLoading) { //persist: yes, token: no (the only time we're going to be in the loading state)
console.log('loading')
content =
Loading...
} else if (isError) { //persist: yes, token: no
diff --git a/lesson_11-frontend/src/features/auth/Prefetch.js b/lesson_10-frontend_JWT_Auth/src/features/auth/Prefetch.js
similarity index 83%
rename from lesson_11-frontend/src/features/auth/Prefetch.js
rename to lesson_10-frontend_JWT_Auth/src/features/auth/Prefetch.js
index da8882f..fc3ac71 100644
--- a/lesson_11-frontend/src/features/auth/Prefetch.js
+++ b/lesson_10-frontend_JWT_Auth/src/features/auth/Prefetch.js
@@ -6,10 +6,12 @@ import { Outlet } from 'react-router-dom';
const Prefetch = () => {
useEffect(() => {
+ // runs after the Prefetch component is mounted to the DOM
console.log('subscribing')
const notes = store.dispatch(notesApiSlice.endpoints.getNotes.initiate())
const users = store.dispatch(usersApiSlice.endpoints.getUsers.initiate())
+ // runs after the Prefetch component is unmounted from the DOM
return () => {
console.log('unsubscribing')
notes.unsubscribe()
diff --git a/lesson_10-frontend/src/features/auth/Welcome.js b/lesson_10-frontend_JWT_Auth/src/features/auth/Welcome.js
similarity index 100%
rename from lesson_10-frontend/src/features/auth/Welcome.js
rename to lesson_10-frontend_JWT_Auth/src/features/auth/Welcome.js
diff --git a/lesson_10-frontend/src/features/auth/authApiSlice.js b/lesson_10-frontend_JWT_Auth/src/features/auth/authApiSlice.js
similarity index 100%
rename from lesson_10-frontend/src/features/auth/authApiSlice.js
rename to lesson_10-frontend_JWT_Auth/src/features/auth/authApiSlice.js
diff --git a/lesson_10-frontend/src/features/auth/authSlice.js b/lesson_10-frontend_JWT_Auth/src/features/auth/authSlice.js
similarity index 100%
rename from lesson_10-frontend/src/features/auth/authSlice.js
rename to lesson_10-frontend_JWT_Auth/src/features/auth/authSlice.js
diff --git a/lesson_10-frontend/src/features/notes/EditNote.js b/lesson_10-frontend_JWT_Auth/src/features/notes/EditNote.js
similarity index 100%
rename from lesson_10-frontend/src/features/notes/EditNote.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/EditNote.js
diff --git a/lesson_10-frontend/src/features/notes/EditNoteForm.js b/lesson_10-frontend_JWT_Auth/src/features/notes/EditNoteForm.js
similarity index 100%
rename from lesson_10-frontend/src/features/notes/EditNoteForm.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/EditNoteForm.js
diff --git a/lesson_10-frontend/src/features/notes/NewNote.js b/lesson_10-frontend_JWT_Auth/src/features/notes/NewNote.js
similarity index 100%
rename from lesson_10-frontend/src/features/notes/NewNote.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/NewNote.js
diff --git a/lesson_10-frontend/src/features/notes/NewNoteForm.js b/lesson_10-frontend_JWT_Auth/src/features/notes/NewNoteForm.js
similarity index 100%
rename from lesson_10-frontend/src/features/notes/NewNoteForm.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/NewNoteForm.js
diff --git a/lesson_10-frontend/src/features/notes/Note.js b/lesson_10-frontend_JWT_Auth/src/features/notes/Note.js
similarity index 100%
rename from lesson_10-frontend/src/features/notes/Note.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/Note.js
diff --git a/lesson_10-frontend/src/features/notes/NotesList.js b/lesson_10-frontend_JWT_Auth/src/features/notes/NotesList.js
similarity index 97%
rename from lesson_10-frontend/src/features/notes/NotesList.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/NotesList.js
index 775a9a7..e7b4063 100644
--- a/lesson_10-frontend/src/features/notes/NotesList.js
+++ b/lesson_10-frontend_JWT_Auth/src/features/notes/NotesList.js
@@ -9,7 +9,7 @@ const NotesList = () => {
isError,
error
} = useGetNotesQuery('notesList', {
- pollingInterval: 15000,
+ pollingInterval: 60000,
refetchOnFocus: true,
refetchOnMountOrArgChange: true
})
diff --git a/lesson_10-frontend/src/features/notes/notesApiSlice.js b/lesson_10-frontend_JWT_Auth/src/features/notes/notesApiSlice.js
similarity index 100%
rename from lesson_10-frontend/src/features/notes/notesApiSlice.js
rename to lesson_10-frontend_JWT_Auth/src/features/notes/notesApiSlice.js
diff --git a/lesson_10-frontend/src/features/users/EditUser.js b/lesson_10-frontend_JWT_Auth/src/features/users/EditUser.js
similarity index 100%
rename from lesson_10-frontend/src/features/users/EditUser.js
rename to lesson_10-frontend_JWT_Auth/src/features/users/EditUser.js
diff --git a/lesson_09-frontend/src/features/users/EditUserForm.js b/lesson_10-frontend_JWT_Auth/src/features/users/EditUserForm.js
similarity index 100%
rename from lesson_09-frontend/src/features/users/EditUserForm.js
rename to lesson_10-frontend_JWT_Auth/src/features/users/EditUserForm.js
diff --git a/lesson_10-frontend/src/features/users/NewUserForm.js b/lesson_10-frontend_JWT_Auth/src/features/users/NewUserForm.js
similarity index 100%
rename from lesson_10-frontend/src/features/users/NewUserForm.js
rename to lesson_10-frontend_JWT_Auth/src/features/users/NewUserForm.js
diff --git a/lesson_10-frontend/src/features/users/User.js b/lesson_10-frontend_JWT_Auth/src/features/users/User.js
similarity index 100%
rename from lesson_10-frontend/src/features/users/User.js
rename to lesson_10-frontend_JWT_Auth/src/features/users/User.js
diff --git a/lesson_10-frontend/src/features/users/UsersList.js b/lesson_10-frontend_JWT_Auth/src/features/users/UsersList.js
similarity index 100%
rename from lesson_10-frontend/src/features/users/UsersList.js
rename to lesson_10-frontend_JWT_Auth/src/features/users/UsersList.js
diff --git a/lesson_10-frontend/src/features/users/usersApiSlice.js b/lesson_10-frontend_JWT_Auth/src/features/users/usersApiSlice.js
similarity index 100%
rename from lesson_10-frontend/src/features/users/usersApiSlice.js
rename to lesson_10-frontend_JWT_Auth/src/features/users/usersApiSlice.js
diff --git a/lesson_10-frontend/src/hooks/usePersist.js b/lesson_10-frontend_JWT_Auth/src/hooks/usePersist.js
similarity index 100%
rename from lesson_10-frontend/src/hooks/usePersist.js
rename to lesson_10-frontend_JWT_Auth/src/hooks/usePersist.js
diff --git a/lesson_10-frontend/src/img/background.jpg b/lesson_10-frontend_JWT_Auth/src/img/background.jpg
similarity index 100%
rename from lesson_10-frontend/src/img/background.jpg
rename to lesson_10-frontend_JWT_Auth/src/img/background.jpg
diff --git a/lesson_10-frontend/src/index.css b/lesson_10-frontend_JWT_Auth/src/index.css
similarity index 100%
rename from lesson_10-frontend/src/index.css
rename to lesson_10-frontend_JWT_Auth/src/index.css
diff --git a/lesson_10-frontend/src/index.js b/lesson_10-frontend_JWT_Auth/src/index.js
similarity index 100%
rename from lesson_10-frontend/src/index.js
rename to lesson_10-frontend_JWT_Auth/src/index.js
diff --git a/lesson_11-frontend/src/components/Public.js b/lesson_11-frontend/src/components/Public.js
deleted file mode 100644
index f1f0457..0000000
--- a/lesson_11-frontend/src/components/Public.js
+++ /dev/null
@@ -1,28 +0,0 @@
-import { Link } from 'react-router-dom'
-
-const Public = () => {
- const content = (
-
-
-
Welcome to Dan D. Repairs!
-
-
-
Located in Beautiful Downtown Foo City, Dan D. Repairs provides a trained staff ready to meet your tech repair needs.
-
- Dan D. Repairs
- 555 Foo Drive
- Foo City, CA 12345
- (555) 555-5555
-
-
-
Owner: Dan Davidson
-
-
-
-
- )
- return content
-}
-export default Public
\ No newline at end of file
diff --git a/lesson_11-frontend/.gitignore b/lesson_11-frontend_Roles&Permission/.gitignore
similarity index 100%
rename from lesson_11-frontend/.gitignore
rename to lesson_11-frontend_Roles&Permission/.gitignore
diff --git a/lesson_11-frontend/UserStories.md b/lesson_11-frontend_Roles&Permission/UserStories.md
similarity index 100%
rename from lesson_11-frontend/UserStories.md
rename to lesson_11-frontend_Roles&Permission/UserStories.md
diff --git a/lesson_11-frontend/package-lock.json b/lesson_11-frontend_Roles&Permission/package-lock.json
similarity index 100%
rename from lesson_11-frontend/package-lock.json
rename to lesson_11-frontend_Roles&Permission/package-lock.json
diff --git a/lesson_11-frontend/package.json b/lesson_11-frontend_Roles&Permission/package.json
similarity index 100%
rename from lesson_11-frontend/package.json
rename to lesson_11-frontend_Roles&Permission/package.json
diff --git a/lesson_11-frontend/public/favicon.ico b/lesson_11-frontend_Roles&Permission/public/favicon.ico
similarity index 100%
rename from lesson_11-frontend/public/favicon.ico
rename to lesson_11-frontend_Roles&Permission/public/favicon.ico
diff --git a/lesson_11-frontend/public/img/background.jpg b/lesson_11-frontend_Roles&Permission/public/img/background.jpg
similarity index 100%
rename from lesson_11-frontend/public/img/background.jpg
rename to lesson_11-frontend_Roles&Permission/public/img/background.jpg
diff --git a/lesson_11-frontend/public/index.html b/lesson_11-frontend_Roles&Permission/public/index.html
similarity index 100%
rename from lesson_11-frontend/public/index.html
rename to lesson_11-frontend_Roles&Permission/public/index.html
diff --git a/lesson_11-frontend/public/logo192.png b/lesson_11-frontend_Roles&Permission/public/logo192.png
similarity index 100%
rename from lesson_11-frontend/public/logo192.png
rename to lesson_11-frontend_Roles&Permission/public/logo192.png
diff --git a/lesson_11-frontend/public/logo512.png b/lesson_11-frontend_Roles&Permission/public/logo512.png
similarity index 100%
rename from lesson_11-frontend/public/logo512.png
rename to lesson_11-frontend_Roles&Permission/public/logo512.png
diff --git a/lesson_11-frontend/public/manifest.json b/lesson_11-frontend_Roles&Permission/public/manifest.json
similarity index 100%
rename from lesson_11-frontend/public/manifest.json
rename to lesson_11-frontend_Roles&Permission/public/manifest.json
diff --git a/lesson_11-frontend/public/robots.txt b/lesson_11-frontend_Roles&Permission/public/robots.txt
similarity index 100%
rename from lesson_11-frontend/public/robots.txt
rename to lesson_11-frontend_Roles&Permission/public/robots.txt
diff --git a/lesson_11-frontend/src/App.js b/lesson_11-frontend_Roles&Permission/src/App.js
similarity index 100%
rename from lesson_11-frontend/src/App.js
rename to lesson_11-frontend_Roles&Permission/src/App.js
diff --git a/lesson_11-frontend/src/app/api/apiSlice.js b/lesson_11-frontend_Roles&Permission/src/app/api/apiSlice.js
similarity index 100%
rename from lesson_11-frontend/src/app/api/apiSlice.js
rename to lesson_11-frontend_Roles&Permission/src/app/api/apiSlice.js
diff --git a/lesson_11-frontend/src/app/store.js b/lesson_11-frontend_Roles&Permission/src/app/store.js
similarity index 100%
rename from lesson_11-frontend/src/app/store.js
rename to lesson_11-frontend_Roles&Permission/src/app/store.js
diff --git a/lesson_11-frontend/src/components/DashFooter.js b/lesson_11-frontend_Roles&Permission/src/components/DashFooter.js
similarity index 94%
rename from lesson_11-frontend/src/components/DashFooter.js
rename to lesson_11-frontend_Roles&Permission/src/components/DashFooter.js
index f1a7420..0020ea3 100644
--- a/lesson_11-frontend/src/components/DashFooter.js
+++ b/lesson_11-frontend_Roles&Permission/src/components/DashFooter.js
@@ -5,7 +5,7 @@ import useAuth from "../hooks/useAuth"
const DashFooter = () => {
- const { username, status } = useAuth()
+ const { username, status } = useAuth() // **
const navigate = useNavigate()
const { pathname } = useLocation()
@@ -25,6 +25,7 @@ const DashFooter = () => {
)
}
+ // **
const content = (