Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sarpongabasimi/rest-api-with-express

This is a mini project to learn how to user Passport.js. Passport is Express-compatible authentication middleware for Node.js. Its sole purpose is to authenticate requests, which it does through an extensible set of plugins known as strategies.
https://github.com/sarpongabasimi/rest-api-with-express

Last synced: 28 days ago
JSON representation

This is a mini project to learn how to user Passport.js. Passport is Express-compatible authentication middleware for Node.js. Its sole purpose is to authenticate requests, which it does through an extensible set of plugins known as strategies.

Awesome Lists containing this project

README

        


Building Restful API with Node & Express.


To Use application.

- ``Fork`` and ``clone this repo``.
- In your terminal cd into ``Express``.
- To run ``feature test`` type ``npm test``.
- To run application type ``npm dev run ``.


File Structure.

Screen Shot 2019-05-08 at 22 19 52


What App Can Curretly Do.

- A user can register at ``localhost:3000/registration/signup``.

- When there is an error during registration, the app is able to tell the user the errors.

> This is possible because of express validators.

Screen Shot 2019-05-12 at 13 08 25

- The application tells ``users`` when their registration email is ``taken``.

Screen Shot 2019-05-14 at 22 04 26


Update

> 17th May 2019 ( Used passportjs Local strategy to handle user Login ).

- ``npm install passport-local``.
- Inside app.js ``passport = require('passport')``.
- Add this ``app.use(passport.initialize())`` & ``app.use(passport.session())``.
- You will need to ``require('./config/passportSetup')(passport)`` if you setup passport in a config folder.

```javascript

var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));

```
- Running the code at the point will cause this error ``Error: failed to serialize user into session``.
- To avoid the error make sure to add this code.

```javascript

var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}

passport.serializeUser(function(user, done) {
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
));

```

- what does ``passport.serializeUser`` and ``passport.deserializeUser`` actually do ?

```
serializeUser determines which data of the user object should be stored in the session.
The result of the serializeUser method is attached to the session as req.session.passport.user = {}.

deserializeUser corresponds to the key of the user object that was given to the done function.
So that the whole object is retrieved with help of that key.
In my application the key is the user id.
```

- When a user tries to login in to the application, the user submits a post request to ``\registration\login``.
- We need to allow ``passportjs`` to handle this process.
- To do this add this to the ``post route``.

```javascript

exports.postLogin = (req, res, next)=> {
passport.authenticate('local',{
successRedirect: '/dashbord',
failureRedirect: '/registration/login'
})(req, res, next);
};
```
- Note
- To find out if ``login`` worked, you can ``console.log`` the user being returned.
- This is what I get.

Screen Shot 2019-05-17 at 16 50 39

> 22nd May 2019 ( Used passportjs Local strategy to handle user access restriction ).

- Added the ability to not allow users to access certain endpoints if not logged in.


To Do

- Add the ability for users to log in.(Done)
- Render ``error`` messages using flash if error occurs during login.
- Make the ability for a user to ``log out``.
- Restrict users form goind to certain endpoints of the app if they have not logged in.(Done)

> This will be done using Passport.js