feat: add fragment redirect for web home

This commit is contained in:
Zack Fu Zi Xiang 2024-03-21 15:25:40 +08:00
parent 528dd6ae5b
commit b43e8c4a61
No known key found for this signature in database
4 changed files with 50 additions and 9 deletions

View File

@ -3,6 +3,7 @@
class="sidebar-item"
hx-target="#sidebar-content"
hx-get="/web/components/user/navigate"
data-section="navigate"
>
Navigate
</div>
@ -10,6 +11,7 @@
class="sidebar-item"
hx-target="#sidebar-content"
hx-get="/web/components/user/change-password"
data-section="change-password"
>
Change Password
</div>
@ -17,6 +19,7 @@
class="sidebar-item"
hx-target="#sidebar-content"
hx-get="/web/components/user/invite"
data-section="invite"
>
Invite
</div>
@ -24,6 +27,7 @@
class="sidebar-item"
hx-target="#sidebar-content"
hx-get="/web/components/user/user-usage"
data-section="user-usage"
>
User Usage
</div>
@ -31,7 +35,24 @@
class="sidebar-item"
hx-target="#sidebar-content"
hx-get="/web/components/user/workspace-usage"
data-section="workspace-usage"
>
Workspace Usage
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const frag = window.location.href.split('#');
if (frag.length > 1) {
const section = frag[1];
const sidebarItems = document.querySelectorAll('.sidebar-item');
sidebarItems.forEach(item => {
if (item.getAttribute('data-section') === section) {
item.click();
}
});
}
});
</script>

View File

View File

@ -109,24 +109,44 @@
<script>
// OAuthLogin (This is done because axum doesn't support fragment in the URL)
// https://github.com/tokio-rs/axum/discussions/2147
// Parse the fragment and extract the parameters
const paramMap = {};
if (window.location.hash) {
// Extract data from the URL fragment
const fragmentData = window.location.hash.substring(1); // Remove the leading #
const fragmentParams = new URLSearchParams(fragmentData); // Parse the fragment data as a URLSearchParams object
const refreshToken = fragmentParams.get("refresh_token"); // Extract the refresh_token
const fragments = window.location.href.split('#').slice(1);
for (let i = 0; i < fragments.length; i++) {
const params = fragments[i].split('&');
for (let j = 0; j < params.length; j++) {
const keyValue = params[j].split('=');
if (keyValue.length === 2) {
const key = keyValue[0];
const value = keyValue[1];
paramMap[key] = value;
}
}
}
// Clear the fragment from the URL
history.replaceState("", document.title, window.location.pathname + window.location.search);
// Login in via refresh_token
const refreshToken = paramMap['refresh_token'];
fetch(`/web-api/login-refresh/${refreshToken}`, {
// Login in via refresh_token
method: "POST",
}).then((response) => {
if (!response.ok) {
displayHttpStatusAndPayload(response);
} else {
window.location.href = "/web/home";
const redirect_to = paramMap['redirect_to'];
if (redirect_to && redirect_to.trim() !== "") {
console.log("redirect_to is not empty");
window.location.href = "/web/home#" + redirect_to;
} else {
window.location.href = "/web/home";
}
}
});
// Remove # from url, prevent access_token from being reused event though refresh token is already invalid
history.replaceState("", document.title, window.location.pathname + window.location.search);
}
</script>
{% endblock %}

View File

@ -181,7 +181,7 @@ pub async fn invite_workspace_members(
email: invitation.email.clone(),
..Default::default()
},
Some("/web/home#invite".to_owned()),
Some("/web/home#redirect_to=invite".to_owned()),
)
.await?;