谁能解释为什么用户在登录后没有被重定向到另一个页面?

问题描述 投票:0回答:0

我使用 Netlify 身份小部件编写此登录表单,说明用户是否通过身份验证以将他们重定向到我的 index3.html 页面;但是,登录后它不执行该功能。我试着在 Netlify 论坛上发布这个问题,但由于某种原因它不允许粘贴代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- include the widget -->
<script type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-    widget.js"></script>
</head>
<body>
<!-- Add a menu:
Log in / Sign up - when the user is not logged in
Username / Log out - when the user is logged in
-->
<div data-netlify-identity-menu></div>

<!-- Add a simpler button:
 Simple button that will open the modal.
-->
<div data-netlify-identity-button>Login with Netlify Identity</div>
</body>
<script>
// Get the current user:
// Available after on('init') is invoked
const user = netlifyIdentity.currentUser();

// Bind to events
netlifyIdentity.on('init', user => console.log('init', user));
netlifyIdentity.on('login', user => console.log('login', user));
netlifyIdentity.on('logout', () => console.log('Logged out'));
netlifyIdentity.on('error', err => console.error('Error', err));
netlifyIdentity.on('open', () => console.log('Widget opened'));
netlifyIdentity.on('close', () => console.log('Widget closed'));

// Unbind from events
netlifyIdentity.off('login'); // to unbind all registered handlers
netlifyIdentity.off('login', handler); // to unbind a single handler

// Close the modal
netlifyIdentity.close();

// Log out the user
netlifyIdentity.logout();

// Refresh the user's JWT
// Call in on('login') handler to ensure token refreshed after it expires (1hr)  
// Note: this method returns a promise.
netlifyIdentity.refresh().then((jwt)=>console.log(jwt))

// Change language
netlifyIdentity.setLocale('en');

// Check if user is authenticated
if (!user) {
// Redirect to login page or other desired page
window.location.href = 'https://pieconfidential.com/index3.html'; // Replace with your  desired URL

}

// Check if Netlify Identity is fully initialized
if (netlifyIdentity.isInitialized()) {
const user = netlifyIdentity.currentUser();
// Your redirect logic here
} else {
// If not fully initialized, wait for the 'init' event
netlifyIdentity.on('init', () => {
  const user = netlifyIdentity.currentUser();
  // Your redirect logic here
});
}

</script>

</html>

我尝试复制并粘贴 Netlify 在其支持论坛上为简单的 Netlify 小部件注册/登录代码提供的确切代码,然后我添加了一些额外的代码以查看它是否可以解决问题但无济于事。我希望重定向到所需页面会起作用。有一次我让函数执行,但它跳过了登录索引页面,而是直接转到重定向页面。使用当前状态的代码,我登录,页面就停留在那里,显示“用户已登录”。

authentication redirect indexing widget netlify
© www.soinside.com 2019 - 2024. All rights reserved.