【问题标题】:Using Firebase as an Authenticating Middleware in Express.js在 Express.js 中使用 Firebase 作为身份验证中间件
【发布时间】:2021-01-20 02:39:57
【问题描述】:

我目前正在创建身份验证中间件,以确保请求标头中有有效的 firebase 令牌。代码如下:

auth.ts

import * as firebase from 'firebase-admin';
import { NextFunction, Request, Response } from 'express';

const getAuthToken = (req: Request, res: Response, next: NextFunction) => {
  if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
    req.authToken = req.headers.authorization.split(' ')[1];
  } else {
    req.authToken = null;
  }
  next();
};

export const checkIfAuthenticated = (req: Request, res: Response, next: NextFunction) => {
  getAuthToken(req, res, async () => {
    try {
      const { authToken } = req;
      const userInfo = await firebase.auth().verifyIdToken(authToken);
      req.authId = userInfo.uid;
      return next();
    } catch (e) {
      return res.status(401).send({ error: 'You are not authorized to make this request' });
    }
  });
};

export const checkIfAdmin = (req: Request, res: Response, next: NextFunction) => {
  getAuthToken(req, res, async () => {
    try {
      const authToken = req;
      const userInfo = await firebase.auth().verifyIdToken(authToken);
      if (userInfo.admin === true) {
        req.authId = userInfo.uid;
        return next();
      }
    } catch (e) {
      return res.status(401).send({ error: 'You are not authorized to make this request' });
    }
  });
};

app.ts

app.get('/api/users', checkIfAuthenticated, (req, res) => {
  executeQuery('SELECT * from user')
    .then(function (results: unknown) {
      res.send(results);
    })
    .catch(function (err: unknown) {
      res.status(400).send(err);
    });
});

但是,在 auth.ts 中我不断收到以下错误,我该如何修复它们:

1.在 getAuthToken

错误:(6, 9) TS2339:“请求”类型上不存在属性“authToken”。

错误:(8, 9) TS2339:“请求”类型上不存在属性“authToken”。

2。在检查IfAuthenticated

错误:(16, 15) TS2339:“请求”类型上不存在属性“authToken”。

错误:(18, 11) TS2339:“请求”类型上不存在属性“authId”。

3.在检查IfAdmin

错误:(30, 60) TS2345: 'Request' 类型的参数不可分配给'string' 类型的参数。

错误:(32, 13) TS2339:“请求”类型上不存在属性“authId”。

【问题讨论】:

    标签: node.js typescript express firebase-authentication firebase-admin


    【解决方案1】:

    修复如下:

    import * as firebase from 'firebase-admin';
    import { NextFunction, Request, Response } from 'express';
    
    export interface IGetAuthTokenRequest extends Request {
      authToken: string;
      authId: string;
    }
    
    const getAuthToken = (req: IGetAuthTokenRequest, res: Response, next: NextFunction) => {
      if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
        req.authToken = req.headers.authorization.split(' ')[1];
      } else {
        req.authToken = null;
      }
      next();
    };
    
    export const checkIfAuthenticated = (
      req: IGetAuthTokenRequest,
      res: Response,
      next: NextFunction
    ) => {
      getAuthToken(req, res, async () => {
        try {
          const { authToken } = req;
          const userInfo = await firebase.auth().verifyIdToken(authToken);
          req.authId = userInfo.uid;
          return next();
        } catch (e) {
          return res.status(401).send({ error: 'You are not authorized to make this request' });
        }
      });
    };
    
    export const checkIfAdmin = (req: IGetAuthTokenRequest, res: Response, next: NextFunction) => {
      getAuthToken(req, res, async () => {
        try {
          const userInfo = await firebase.auth().verifyIdToken(req.authToken);
          if (userInfo.admin === true) {
            req.authId = userInfo.uid;
            return next();
          }
        } catch (e) {
          return res.status(401).send({ error: 'You are not authorized to make this request' });
        }
      });
    

    【讨论】:

    • 在特定路线上应用时就像一个魅力。直接通过 app.use(middleware) 在 express 实例上使用成功了吗?它对我不起作用。
    • 完全没有,我没有做到那么远。很高兴看到它有所帮助!
    猜你喜欢
    • 2021-08-30
    • 2019-02-27
    • 2016-07-26
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多