【问题标题】:Cross-Origin Auth0 authentication跨域Auth0认证
【发布时间】:2021-11-24 00:16:52
【问题描述】:

我的后端使用 Auth0 验证了以下端点

api.mysite.com/auth/login
api.mysite.com/auth/logout
api.mysite.com/ → returns “logged in” or “logged out”

我的后端是一个快速服务器

另一方面,如果我打开前端的控制台 mysite.com, fetch(‘api.mysite.com’) 返回“已注销” 即使在新标签页中导航到 api.mysite.com 会让我“登录”。

我将 https://*.mysite.com 列入“允许的 Web 来源”和“允许的来源”下的白名单,但我无法弄清楚为什么我无法访问我的 API。

提前致谢

需要注意的是,我在 api.mysite.com/auth/login 使用通用登录

【问题讨论】:

    标签: express auth0


    【解决方案1】:

    这是我的 cors 中间件

    import { NextFunction, Request, Response } from "express";
    
    const corsMiddleware = (req: Request, res: Response, next: NextFunction) => {
      const allowedDomains = [
        "http://localhost:4200",
        "https://sample.ir",
        "https://sample.ir"
      ];
      if (allowedDomains.indexOf(req.headers.origin) !== -1) {
        const index: number = allowedDomains.indexOf(req.headers.origin);
        res.header("Access-Control-Allow-Origin", allowedDomains[index]);
      }
    
      res.header("Access-Control-Allow-Headers", "Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token");
      res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
      res.header("Access-Control-Allow-Credentials", "true");
      res.header("Access-Control-Max-Age", "3600");
    
      // if (req.url.search("public") == -1) {
      //   res.header("content-type", "application/json; charset=utf-8");
      // }
    
      const method = req.method;
      if (method == "OPTIONS") {
        // res.header("HTTP/1.1 200 OK CORS");
        return res.status(200).json({});
      }
      next();
    
    };
    
    export default corsMiddleware;
    
    
    

    【讨论】:

    • 我的中间件错了
    猜你喜欢
    • 2018-02-11
    • 2010-11-06
    • 2012-06-01
    • 2017-01-14
    • 2021-12-01
    • 2016-02-08
    相关资源
    最近更新 更多