Coder Social home page Coder Social logo

JWT auth about mern-starter HOT 7 CLOSED

hashnode avatar hashnode commented on July 17, 2024
JWT auth

from mern-starter.

Comments (7)

SOSANA avatar SOSANA commented on July 17, 2024

do a PR for this, everyone can chime in and comment on your pr to make improvements if needed. I would image this would need to be created on a new branch as this would be a major change which would include new models and controllers for users on server, as well as new actions, reducers, etc on client.

I am curious how this gets passed to react and redux myself

from mern-starter.

SOSANA avatar SOSANA commented on July 17, 2024

@sandeeppanda92 should we first implement user first in server/client, maybe open new issue? should we create a branch for this? thought once we have user implemented we could apply JWT after, This brings up the bigger question as well social-signin along with caching and sessions πŸ‘

We could use passport with this. From passport-local examples: (with the JWT token added)

// POST /login
//   This is an alternative implementation that uses a custom callback to
//   achieve the same functionality.
app.post('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err) }
    if (!user) {
      return res.json(401, { error: 'message' });
    }

    //user has authenticated correctly thus we create a JWT token 
    var token = jwt.encode({ username: 'somedata'}, tokenSecret);
    res.json({ token : token });

  })(req, res, next);
});

from mern-starter.

HaNdTriX avatar HaNdTriX commented on July 17, 2024

@sylvainlap JWTs don't have to be stored in localStorage.

Did you take a look at https://github.com/lynndylanhurley/redux-auth?
It looks quite promising.

They use cookies as a JWT store and pass them in on the sever side via req.headers.cookies.

from mern-starter.

SOSANA avatar SOSANA commented on July 17, 2024

@HaNdTriX 1+

from mern-starter.

carloscuatin avatar carloscuatin commented on July 17, 2024

if exactly as @HaNdTriX says you have to keep the token in localStorage and I'm moving on how to do this JWT auth feel free to suggest things to make it better

from mern-starter.

zoalst avatar zoalst commented on July 17, 2024

I'm adding auth to my MERN project, using react-cookie to store the token, and ran into a problem when it comes to routing. To limit certain routes to authenicated users I made an onEnter method for the Route which involves accessing the store. Any tips on how to get the store in the routes file?

If anyone's curious, what I've done so far:
In actions.js, save the cookie in a loginSuccess function, load the cookie in a loadAuth function, and set the cookie as the authorization header in the action that I want to limit authorization.
In server.js, use react-cookie's plugToRequest.
In App.js, added a componentWillMount method that checks for auth and calls the loadAuth action if necessary.

Got these ideas from this thread:
erikras/react-redux-universal-hot-example#608

from mern-starter.

somus avatar somus commented on July 17, 2024

Authentication is something which will vary from project to project. Generally, it is not a good idea to have authentication in a boilerplate project. But, we could have the mern-cli clone a different project which has implemented JWT auth using mern for the people who want that. It can look something like this,

mern list

can output something like this

Available variants
--------------------------
passport-auth - MERN with passport.js
jwt-auth - MERN with JWT auth
redux-saga - MERN with redux-saga

You can clone a particular variant using

mern newApp jwt-auth

I've created a separate discussion thread for this at Hashnode/mern-cli#10. I'm closing this in favour of that. We can continue the discussion there.

from mern-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.