gpt4 book ai didi

React-Native Expo 应用程序 Auth0 登录,AuthSession - authURL 失败

转载 作者:行者123 更新时间:2023-12-05 02:59:26 25 4
gpt4 key购买 nike

screenshot of error我正在尝试使用 AuthSession 将我的登录设置为我的 React native Expo 应用程序。

我不断收到此错误:ValidationError: child "authUrl"fails because ["authUrl"must be a valid uri]".

奇怪的是,如果我控制台记录正在传递的 URI,将其复制并粘贴到浏览器中,它就可以工作了! (按预期调出 Auth0 登录屏幕)。

为什么我在尝试从我的 Expo 客户端应用调用它时收到此错误?

这是我的代码:

 function toQueryString(params) {
return '?' + Object.entries(params)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
}

class SignInScreen extends React.Component {

logIn = async () => {
const redirectUrl = AuthSession.getRedirectUrl();
console.log(`Redirect URL: ${redirectUrl}`);

// Structure the auth parameters and URL
const queryParams = toQueryString({
client_id: auth0ClientId,
redirect_uri: redirectUrl,
response_type: 'id_token', // id_token will return a JWT token
scope: 'openid profile', // retrieve the user's profile
nonce: 'nonce', // ideally, this will be a random value
});
const authUrl = `${auth0Domain}/authorize` + queryParams;

// Perform the authentication
const response = await AuthSession.startAsync({ authUrl });
console.log('Authentication response', response);

if (response.type === 'success') {
this.handleResponse(response.params);
}
};

handleResponse = (response) => {
if (response.error) {
Alert('Authentication error', response.error_description || 'something went wrong');
return;
}

console.log(response.id_token)
};

最佳答案

发布后我很快就想通了。这是我为让它发挥作用所做的工作。

我正在使用:

 const auth0Domain = 'myapp.auth0.com'

我应该一直在使用:

const auth0Domain = 'https://myapp.auth0.com'

UPDATE 另外,一定要在你的 app.json 中直接在 'expo' 下添加一个方案,该方案应该是你的 slug 名称全部小写,不要忘记它必须是全部小写。另外,不要忘记这个新的 app.json 不能通过 OTA 更新,必须提交到应用商店。如果您尝试在没有包含该方案的更新的 app.json 的情况下使用此 Auth0,那么您将收到一个 Safari 错误,提示“Safari 无法打开此页面,因为它是无效的 URL”或类似的内容。当您看到此消息时,请记住这不是您的重定向 URL 的问题(假设您使用的是样板 AuthSession.getRedirectURL() 方法,并且您将相同的重定向 URL 放入 Auth0 设置的回调中)。如果您执行了这两件事,那么您会看到此错误,因为您尚未在应用商店或测试飞行构建中更新您的方案。

关于React-Native Expo 应用程序 Auth0 登录,AuthSession - authURL 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com