【发布时间】:2019-11-23 06:58:00
【问题描述】:
我目前正在尝试使用 Auth0 实现一个 React 应用程序进行身份验证,而且我是 typescript 的新手。
在我的Auth.js 中尝试了下面的代码,它工作正常,但是当我尝试将我的Auth.js 文件迁移到Auth.ts 时,我得到了带有错误的红色波浪
Auth 类型上不存在属性“历史记录”
Auth e.t.c 类型上不存在属性“userProfile”
import auth0 from "auth0-js";
const REDIRECT_ON_LOGIN = "redirect_on_login";
export default class Auth {
constructor(history) {
this.history = history;
this.userProfile = null;
this.requestedScopes = "openid profile email";
this.auth0 = new auth0.WebAuth({
domain: process.env.REACT_APP_AUTH0_DOMAIN,
clientID: process.env.REACT_APP_AUTH0_CLIENT_ID,
redirectUri: process.env.REACT_APP_AUTH0_CALLBACK_URL,
audience: process.env.REACT_APP_AUTH0_AUDIENCE,
responseType: "token id_token",
scope: this.requestedScopes
});
}
login = () => {
localStorage.setItem(
REDIRECT_ON_LOGIN,
JSON.stringify(this.history.location)
);
this.auth0.authorize();
};
handleAuthentication = () => {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
this.setSession(authResult);
const redirectLocation =
localStorage.getItem(REDIRECT_ON_LOGIN) === "undefined"
? "/"
: JSON.parse(localStorage.getItem(REDIRECT_ON_LOGIN));
this.history.push(redirectLocation);
} else if (err) {
this.history.push("/");
alert(`Error: ${err.error}. Check the console for details`);
console.log(err);
}
localStorage.removeItem(REDIRECT_ON_LOGIN);
});
};
我希望能够使用 react & typescript 实现 auth0 身份验证
【问题讨论】:
标签: reactjs typescript auth0