Sobes.tech
Junior — Middle

Describe how authentication and authorization systems are implemented in your last project.

sobes.tech AI

Answer from AI

In my last project, authentication was implemented using JWT (JSON Web Tokens). The user enters a login and password, the server verifies them, and if the data is correct, it generates a token with a specific expiration time. This token is stored by the client and sent with each request in the Authorization header.

Authorization is based on roles encoded in the token. The server checks the user's role and grants or denies access to certain resources.

An example of token verification in Node.js using express and jsonwebtoken:

const jwt = require('jsonwebtoken');

function authenticateToken(req, res, next) {
  const authHeader = req.headers['authorization'];
  const token = authHeader && authHeader.split(' ')[1];

  if (!token) return res.sendStatus(401);

  jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) => {
    if (err) return res.sendStatus(403);
    req.user = user;
    next();
  });
}

Thus, security is ensured by verifying authenticity and access rights on each request.

Describe how authentication and authorization systems… - sobes.tech