Creating Sign out

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 309:- Creating Sign out

To create a sign out functionality in our Express.js app, we need to do the following:

  1. Install the express-session middleware if you haven't done so already. This middleware is required for using sessions in your Express.js app.

    Copy code

    npm install express-session

  2. Require express-session and initialize it in your app using the session middleware.

    javascriptCopy code

    const session = require('express-session'); // Initialize session middleware app.use(session({  secret: 'my-secret-key',  resave: false,  saveUninitialized: false, }));

    Here, we are using the default memory store for sessions. However, it's not recommended for production environments. Instead, you should use a database store like connect-mongo or connect-redis.

  3. Create a new route for sign out, and use the req.logout() method to remove the user's session.

    javascriptCopy code

    app.get('/sign-out', (req, res) => {  req.logout();  res.redirect('/'); });

    The req.logout() method is provided by Passport.js, and it removes the user's session from the server. Once the session is removed, the user is no longer considered authenticated.

  4. Add a link or button to your views that points to the sign-out route.

    htmlCopy code

    <a href="/sign-out">Sign Out</a>

That's it! Users can now sign out of your app by clicking on the "Sign Out" link or button, and their session will be removed from the server.

35. Authentication Using Passport js

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?