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" class="sidebar-item"
hx-target="#sidebar-content" hx-target="#sidebar-content"
hx-get="/web/components/user/navigate" hx-get="/web/components/user/navigate"
data-section="navigate"
> >
Navigate Navigate
</div> </div>
@ -10,6 +11,7 @@
class="sidebar-item" class="sidebar-item"
hx-target="#sidebar-content" hx-target="#sidebar-content"
hx-get="/web/components/user/change-password" hx-get="/web/components/user/change-password"
data-section="change-password"
> >
Change Password Change Password
</div> </div>
@ -17,6 +19,7 @@
class="sidebar-item" class="sidebar-item"
hx-target="#sidebar-content" hx-target="#sidebar-content"
hx-get="/web/components/user/invite" hx-get="/web/components/user/invite"
data-section="invite"
> >
Invite Invite
</div> </div>
@ -24,6 +27,7 @@
class="sidebar-item" class="sidebar-item"
hx-target="#sidebar-content" hx-target="#sidebar-content"
hx-get="/web/components/user/user-usage" hx-get="/web/components/user/user-usage"
data-section="user-usage"
> >
User Usage User Usage
</div> </div>
@ -31,7 +35,24 @@
class="sidebar-item" class="sidebar-item"
hx-target="#sidebar-content" hx-target="#sidebar-content"
hx-get="/web/components/user/workspace-usage" hx-get="/web/components/user/workspace-usage"
data-section="workspace-usage"
> >
Workspace Usage Workspace Usage
</div> </div>
</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> <script>
// OAuthLogin (This is done because axum doesn't support fragment in the URL) // OAuthLogin (This is done because axum doesn't support fragment in the URL)
// https://github.com/tokio-rs/axum/discussions/2147 // https://github.com/tokio-rs/axum/discussions/2147
// Parse the fragment and extract the parameters
const paramMap = {};
if (window.location.hash) { if (window.location.hash) {
// Extract data from the URL fragment const fragments = window.location.href.split('#').slice(1);
const fragmentData = window.location.hash.substring(1); // Remove the leading # for (let i = 0; i < fragments.length; i++) {
const fragmentParams = new URLSearchParams(fragmentData); // Parse the fragment data as a URLSearchParams object const params = fragments[i].split('&');
const refreshToken = fragmentParams.get("refresh_token"); // Extract the refresh_token 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}`, { fetch(`/web-api/login-refresh/${refreshToken}`, {
// Login in via refresh_token
method: "POST", method: "POST",
}).then((response) => { }).then((response) => {
if (!response.ok) { if (!response.ok) {
displayHttpStatusAndPayload(response); displayHttpStatusAndPayload(response);
} else { } 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> </script>
{% endblock %} {% endblock %}

View File

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