View on GitHub

reading-notes

CodeFellows Class Reading Notes

Bearer Authentication

Bearer Token is a secondary authentication method that can be used instead of repeatedly using Basic Authentication or OAuth from the same client

  Authorization: Bearer encoded.jsonwebtoken.here

Express Server Example:

app.get('/somethingsecret', bearerToken, (req,res) => {
  res.status(200).send('secret sauce');
});

function bearerToken( req, res, next ) {
  let token = req.headers.authorization.split(' ').pop();
  try {
    if ( tokenIsValid(token) ) { next(); }
  }
  catch(e) { next("Invalid Token") }
}

function tokenIsValid(token) {
  let parsedToken = jwt.verify(token, SECRETKEY);
  return Users.find(parsedToken.id);
}

JSON Web Tokens

JWT What is JWT? (Video)

When should you use JSON Web Tokens?


Home