微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将Firebase用作Express.js中的身份验证中间件

如何解决将Firebase用作Express.js中的身份验证中间件

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

身份验证

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,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,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:类型'Request '上不存在属性'authToken'。

错误:(8,9)TS2339:类型'Request '上不存在属性'authToken'。

2。在checkIfAuthenticated

错误:(16、15)TS2339:类型'Request '上不存在属性'authToken'。

错误:(18,11)TS2339:类型'Request '上不存在属性'authId'。

3。在checkIfAdmin

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

错误:(32,13)TS2339:类型'Request '上不存在属性'authId'。

解决方法

修复如下:

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,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,next: NextFunction) => {
  getAuthToken(req,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' });
    }
  });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?