使用身份验证方法POST的Auth0 SPA登录

问题描述

是否可以使用Auth0中的auth0-spa-js以身份验证方法POST登录

我必须使用客户端凭据从后端删除用户。我的前端代码运行正常。当我将方法从无更改为POST时,抛出403错误。有什么方法可以将SPA与身份验证方法POST一起使用?

import React,{ useState,useEffect,useContext } from "react";
import createAuth0Client from "@auth0/auth0-spa-js";

const DEFAULT_REDIRECT_CALLBACK = () =>
  window.history.replaceState({},document.title,window.location.pathname);

export const Auth0Context = React.createContext();
export const useAuth0 = () => useContext(Auth0Context);
export const Auth0Provider = ({
  children,onRedirectCallback = DEFAULT_REDIRECT_CALLBACK,...initOptions
}) => {
  const [isAuthenticated,setIsAuthenticated] = useState();
  const [user,setUser] = useState();
  const [auth0Client,setAuth0] = useState();
  const [loading,setLoading] = useState(true);
  const [popupOpen,setPopupOpen] = useState(false);

  useEffect(() => {
    const initAuth0 = async () => {
      const auth0FromHook = await createAuth0Client(initOptions);
      setAuth0(auth0FromHook);

      if (
        window.location.search.includes("code=") &&
        window.location.search.includes("state=")
      ) {
        const { appState } = await auth0FromHook.handleRedirectCallback();
        console.log(appState);
        onRedirectCallback(appState);
      }

      const isAuthenticated = await auth0FromHook.isAuthenticated();

      setIsAuthenticated(isAuthenticated);

      if (isAuthenticated) {
        const user = await auth0FromHook.getUser();
        setUser(user);
      }
      setLoading(false);
    };
    initAuth0();
    // eslint-disable-next-line
  },[]);

  const loginWithPopup = async (params = {}) => {
    setPopupOpen(true);
    try {
      await auth0Client.loginWithPopup(params);
    } catch (error) {
      console.error(error);
    } finally {
      setPopupOpen(false);
    }
    const user = await auth0Client.getUser();
    setUser(user);
    setIsAuthenticated(true);
  };

  const handleRedirectCallback = async () => {
    setLoading(true);
    await auth0Client.handleRedirectCallback();
    const user = await auth0Client.getUser();
    setLoading(false);
    setIsAuthenticated(true);
    setUser(user);
  };
  return (
    <Auth0Context.Provider
      value={{
        isAuthenticated,user,loading,popupOpen,loginWithPopup,handleRedirectCallback,getIdTokenClaims: (...p) => auth0Client.getIdTokenClaims(...p),loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p),getTokenSilently: (...p) => auth0Client.getTokenSilently(...p),getTokenWithPopup: (...p) => auth0Client.getTokenWithPopup(...p),logout: (...p) => auth0Client.logout(...p)
      }}
    >
      {children}
    </Auth0Context.Provider>
  );
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...