繁体   English   中英

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

[英]Passing values from one function to another function

所以我在做一个基于角色的访问应用程序,我将用户的角色嵌入到 JWT 中。 当我解码令牌时,我得到了id, role... 现在我有一个函数可以检查来自x-auth-token标头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)                                    
  })                                                                  
}

我也有一个检查角色的函数,它类似于decodeToken函数,这就是roleCheck函数的样子。

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
}

所以decodeToken首先被调用,那么roleCheck请求之前打postProduct控制器。 通过这种方式,令牌从标头中获取并解码,然后控制器可以根据角色采取行动。

任何帮助将不胜感激,谢谢。

您正在对令牌进行两次解码,无需这样做。您正在对“decodeToken”中的令牌进行解码,并将新字段“user”附加到包含来自令牌的所有解码信息的请求对象(id、role 等) )。 在“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   //here you are storing the info in user field of httpRequest                               
        return controller(httpRequest)                                    
     })                                                                  
    }

现在将名称“roleCheck”更改为“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)来检查其他角色。您也不必担心代码重复,因为这些函数只需要在您的应用程序中定义一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM