# All files / Backend/Authentication is-auth.js

32.43% Statements 12/37
25% Branches 2/8
33.33% Functions 1/3
32.43% Lines 12/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    844x
    4x

    4x

    8x

    8x

    8x

    8x
    8x

    8x

    8x
    8x

    4x

     const jwt = require('jsonwebtoken');
    const api_key = require('../config/config');

    exports.authentication= (req,res,next)=>{

       let access_token = req.headers['authorization'];
       //console.log(access_token)

       Iif(!access_token){
          const error = new Error("not authenticated")
          error.statusCode=401;
          res.status(401).json({message:"not authenticated"})
       }

       else{
          let access = access_token.split(' ')[1];
          let payload;
          // console.log(access)
          try{
             payload = jwt.verify(access,api_key.accessToken);
          }
          catch(err){
             err.statusCode = 401;
             res.status(401).json({message:"not authenticated"})
             throw err;
          }

          Iif(!payload){
            const error = new Error("Not authenticated.");
            res.status(401).json({messages:"not authenticated"})
            error.statusCode = 401;
            throw error;
          }
          //console.log("this is the payload of access token",payload)
          res.userID=payload['username']
          next();

       }

    }

    // getting access token using refresh token
    exports.GetnewAccessToken = (req,res)=>{

       let refresh_token = req.body.refresh_token;

       if(!refresh_token){
          const error = new Error("not authenticated")
          error.statusCode=401;
          res.status(401).json({message:"not authenticated"})
          throw error;
       }
       else{
             jwt.verify(refresh_token,api_key.refereshToken, function(err, decoded){
                if(err){
                   const error = new Error("Not authenticated.");
                   res.status(401).json({messages:"not authenticated"})
                   error.statusCode = 401;
                   throw error;
                }
                else{
                   const access_token=jwt.sign({email:decoded['email']},api_key.accessToken,{
                      algorithm: "HS256",
                      expiresIn:api_key.accessTokenLife
                   });

                   const referesh_token = jwt.sign({email:decoded['email']}, api_key.refereshToken,{
                      algorithm: "HS256",
                      expiresIn:api_key.refereshTokenLife})

                      return res.status(200).json({message:"Fetched token successfully",  access_token:access_token,
                      refresh_token:referesh_token})
                   }
             })

          }

       }