這是我第一次在StackOverflow發帖。感謝你們這個了不起的社區。 我想問一下如何將Auth0部署到Heroku。
在我的 "HTTP://localhost:3000 "上的正常行為是重定向到Auth0登錄頁面,當用戶成功登錄后,它會重定向到該頁面。 我的帶有Auth0的React應用程式在localhost中作業非常正常,然而,當它在Heroku上時,它說。"沒有找到認證令牌"。它顯示如下:
[heroku訊息][1] 。 [1]: https://i.stack.imgur.com/CDO3O.png
我嘗試了使用這個鏈接的Auth0教程。https://auth0.com/docs/quickstart/spa/react/01-login,然而,盡管我在Heroku中添加了Auth0插件并在Auth0中整合了Heroku的SSO,但它只在本地主機上作業。
我的React應用程式中的代碼:
import React from "react";
import { useHistory } from "react-router-dom";
import { Auth0Provider } from "@auth0/auth0-react" 。
require('dotenv').config()。
const Auth0ProviderWithHistory = ({ children }) => {
const domain = process.env.REACT_APP_AUTH0_DOMAIN>
const clientId = process.env.REACT_APP_AUTH0_CLIENT_ID。
const history = useHistory()
const onRedirectCallback = (appState)=> {
history.push(appState?.returnTo|window.location.pathname)
}
return (
<Auth0Provider。
domain={domain}。
clientId={clientId}。
redirectUri={window.location.origin}。
onRedirectCallback={onRedirectCallback}
audience={process.env.REACT_APP_AUTH0_AUDIENCE}
scope={process.env.REACT_APP_AUTH0_SCOPE}
useRefreshTokens={true}。
>
{子}
</Auth0Provider>
)
}
export default Auth0ProviderWithHistory
在我的Express服務器中:
const jwt = require('express-jwt')
const jwks = require('jwks-rsa')
var jwtCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: process.env.JWKS_URI。
}),
audience: process.env.JWT_AUDIENCE。
issuer: process.env.JWT_ISSUER。
algorithms: ['RS256']
})
app.use(jwtCheck)
對于托管到Heroku的生產模式:
//生產模式。Heroku
if (process.env.NODE_ENV == "production"){
app.use(express.static(path. join(__dirname,"client", "build"))
app.get("*", (req, res) => /span> {
res.sendFile(path. join(__dirname,"client", "build", "index.html") )。)
})
}
一個使用JWTcheck的正常API將是:
router.route('/delete/:id'/span>) 。 delete(jwtCheck,async (req, res) => {
await Queue.findByIdAndDelete(req.params. id).exec((err, inventory) =>/span> {
if (err) return (err)
const response = {
message: "成功洗掉"。
id: req.params.id.
}
return res.status(200).send( response)。
})
})
誰能幫幫我:(
)非常感謝你。
uj5u.com熱心網友回復:
解決方案是從server.js中洗掉,因為jwtCheck函式是用用戶會話密鑰來保護API的,因此,在server.js中包括這個,對于沒有會話密鑰的GET請求會顯示上述錯誤。 只在API保護中使用上述jwtCheck。 不要犯我的愚蠢錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/319748.html
標籤:
