If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To check authentication on creating a post, we can use the ensureAuthenticated
middleware that we have previously created. This middleware checks if the user is authenticated or not. If the user is authenticated, it will call the next
function to continue with the request. Otherwise, it will redirect the user to the login page.
Here's an example of how we can use the ensureAuthenticated
middleware to protect the create post route:
javascriptCopy code
const express = require('express');
const router = express.Router();
const { ensureAuthenticated } = require('../config/auth');
// GET request for creating a new post
router.get('/new', ensureAuthenticated, (req, res) => {
res.render('posts/new');
});
// POST request for creating a new post
router.post('/', ensureAuthenticated, (req, res) => {
// Code for saving the new post to the database
});
module.exports = router;
In the above code, we are using the ensureAuthenticated
middleware as the second argument to both the GET
and POST
routes. This ensures that only authenticated users can access these routes. If an unauthenticated user tries to access these routes, they will be redirected to the login page.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.