Skip to content

This is user server api starter file that you need to build a user based sites using PassportJS and session.

Notifications You must be signed in to change notification settings

ooo11/userServerAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Login server starting file using PassportJS

I hate to repeat stuff so here is all the shit you need to make your own user based website. 👻

MongoDB | Javascript | NodeJS | PassportJS |

  1. Make your own .env file and add MY_SECRET_KEY=Writeyourownsecretkeyhere
  2. $ npm install
  3. $ npm run serve

Register

Can add more detail if you want to, but here is the basic.

POST: http://localhost:4000/user/register

{
  username: "YOUR_USERNAME";
  email: "YOUR_EMAIL";
  password: "YOUR_PASSWORD";
}

Login

Good Old login

POST: http://localhost:4000/user/login

{
  username: "YOUR_USERNAME";
  password: "YOUR_PASSWORD";
}

Edit your own login redirect as shown.

exports.loginUser = async (req, res) => {
  //LOGIN
  .
  .
  .
  req.login(user, (err) => {
    if (err) {
      console.log(err);
    } else {
      if (!user) {
        return res
          .status(401)
          .json({ error: "Login failed! Check authentication credentials" });
      } else {
        passport.authenticate("local")(req, res, () => {
           ////////////////////////
          //YOUR res.redirect here
          ////////////////////////
          res.send("this works");
        });
      }
    }
  });
};

Logout

GET: http://localhost:4000/user/logout

Get user data

GET: http://localhost:4000/user/

About

This is user server api starter file that you need to build a user based sites using PassportJS and session.

Topics

Resources

Stars

Watchers

Forks