Javascript 将值从一个函数传递到另一个函数

Javascript 将值从一个函数传递到另一个函数,javascript,node.js,mongodb,function,express,Javascript,Node.js,Mongodb,Function,Express,因此,我正在开发一个基于角色的访问应用程序,并将用户的角色嵌入到JWT中。当我解码令牌时,我得到id,role…。现在我有了一个函数,可以从x-auth-token头检查令牌。这就是函数的外观 export const decodeToken = (controller) => { return wrapAsync(async (httpRequest) => { c

因此,我正在开发一个基于角色的访问应用程序,并将用户的角色嵌入到JWT中。当我解码令牌时,我得到
id,role…
。现在我有了一个函数,可以从
x-auth-token
头检查令牌。这就是函数的外观

export const decodeToken = (controller) => {                          
  return wrapAsync(async (httpRequest) => {                           
    const token = httpRequest.headers['x-auth-token']                 
    if (!token) {                                                     
      throw new UnauthorizedError('No token, authorization denied.')  
    }                                                                 
    const decoded = jwt.verify(token, process.env.JWT_SECRET)                                                                          
    httpRequest.user = decoded                                        
    return controller(httpRequest)                                    
  })                                                                  
}
export function roleCheck(controller) {
  return wrapAsync(async (httpRequest) => {
    const token = httpRequest.headers['x-auth-token']
    const decoded = jwt.verify(token, process.env.JWT_SECRET)
    if (decoded.role !== 'admin') {
      throw new UnauthorizedError(
        'You do not have the authorization to perform this action'
      )
    }
    return controller(httpRequest)
  })
}
我还有一个检查角色的函数,它类似于
decodeToken
函数,这就是
roleCheck
函数的外观

export const decodeToken = (controller) => {                          
  return wrapAsync(async (httpRequest) => {                           
    const token = httpRequest.headers['x-auth-token']                 
    if (!token) {                                                     
      throw new UnauthorizedError('No token, authorization denied.')  
    }                                                                 
    const decoded = jwt.verify(token, process.env.JWT_SECRET)                                                                          
    httpRequest.user = decoded                                        
    return controller(httpRequest)                                    
  })                                                                  
}
export function roleCheck(controller) {
  return wrapAsync(async (httpRequest) => {
    const token = httpRequest.headers['x-auth-token']
    const decoded = jwt.verify(token, process.env.JWT_SECRET)
    if (decoded.role !== 'admin') {
      throw new UnauthorizedError(
        'You do not have the authorization to perform this action'
      )
    }
    return controller(httpRequest)
  })
}
现在,这里有相当多的代码重复。我本可以解决这个问题的一种方法是检查
decodeToken
函数中的角色,但该方法将导致没有
admin
角色附加到传入负载的用户无法访问所有路由。现在我的问题是我如何通过这个

const token = httpRequest.headers['x-auth-token']
const decoded = jwt.verify(token, process.env.JWT_SECRET)
decodeToken
功能到
roleCheck
功能。另外,这是函数组合的用例吗?我觉得代码可以比这更干净,但我只是不知道如何实现这一点

通常,路线如下所示

export function config(router) {
  router
    .post('/', expressCallback(decodeToken(roleCheck(postProduct))))
    .get('/', expressCallback(decodeToken(getProducts)))
    ...
  return router
}
因此,在请求到达
postProduct
控制器之前,首先调用
decodeToken
,然后调用
roleCheck
。以这种方式,令牌从报头获取并解码,然后控制器可以基于角色进行操作


感谢您的帮助。

您正在对令牌进行两次解码,无需执行此操作。您正在“解码令牌”中解码令牌,并将一个新字段“用户”附加到包含令牌中所有解码信息(id、角色等)的请求对象。在“roleCheck”中,您不需要再次解码令牌,因为您已经将所有解码信息存储在请求对象的“user”字段中。您所要做的就是访问此信息并检查角色

解码令牌:

    export const decodeToken = (controller) => {                          
      return wrapAsync(async (httpRequest) => {                           
        const token = httpRequest.headers['x-auth-token']                 
        if (!token) {                                                     
          throw new UnauthorizedError('No token, authorization denied.')  
        }                                                                 
        const decoded = jwt.verify(token, process.env.JWT_SECRET)                                                                          
        httpRequest.user = decoded   //here you are storing the info in user field of httpRequest                               
        return controller(httpRequest)                                    
     })                                                                  
    }
现在将名称“roleCheck”更改为类似“adminRoleCheck”的名称,以检查用户是否为admin

adminRoleCheck:

    export function adminRoleCheck(controller) {
      return wrapAsync(async (httpRequest) => {
        
        if (httpRequest.user.role !== 'admin') {
           throw new UnauthorizedError(
             'You do not have the authorization to perform this action'
           )
        }
        return controller(httpRequest)
     })
    }

类似地,定义其他函数(如customerRoleCheck)以检查其他角色。您也不必太担心代码重复,因为这些函数只需在应用程序中定义一次。

非常感谢,很明显我没有看到它。这正是我想要达到的。很乐意帮忙!!