This commit is contained in:
2024-04-21 14:42:52 +02:00
parent 4b69674ede
commit 8a25f53c99
10700 changed files with 55767 additions and 14201 deletions

View File

@ -0,0 +1,10 @@
import hashids from 'hashids'
import { v4 as uuidv4 } from 'uuid'
export function newHashId(): string {
return new hashids(uuidv4()).encode(Date.now())
}
export function newHyphenlessUuid(): string {
return uuidv4().replaceAll('-', '')
}

View File

@ -0,0 +1,69 @@
import { StorageOptions } from '@/lib/hooks/page-pagination'
export const FILES_PAGINATION_STEP = 21
export function filePaginationSteps() {
return [
FILES_PAGINATION_STEP,
FILES_PAGINATION_STEP * 2,
FILES_PAGINATION_STEP * 4,
FILES_PAGINATION_STEP * 5,
]
}
export function filesPaginationStorage(): StorageOptions {
return {
prefix: 'voltaserve',
namespace: 'files',
enabled: true,
}
}
export function incomingInvitationPaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'incoming_invitation',
}
}
export function groupPaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'group',
}
}
export function groupMemberPaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'group_member',
}
}
export function outgoingInvitationPaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'outgoing_invitation',
}
}
export function organizationPaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'organization',
}
}
export function organizationMemberPaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'organization_member',
}
}
export function workspacePaginationStorage() {
return {
prefix: 'voltaserve',
namespace: 'workspace',
}
}

View File

@ -0,0 +1,66 @@
import TokenAPI, { Token } from '@/client/idp/token'
import {
loadAccessToken,
loadRefreshToken,
loadTokenExpiry,
removeAccessToken,
removeRefreshToken,
removeTokenExpiry,
saveAccessToken,
saveRefreshToken,
saveTokenExpiry,
} from '@/local-storage'
export const COOKIE = 'voltaserve_access_token'
export async function saveToken(token: Token) {
document.cookie = `${COOKIE}=${token.access_token}; Path=/; Max-Age=${token.expires_in}`
saveAccessToken(token.access_token)
saveRefreshToken(token.refresh_token)
const tokenExpiry = new Date()
tokenExpiry.setSeconds(tokenExpiry.getSeconds() + token.expires_in)
saveTokenExpiry(tokenExpiry.toISOString())
}
export async function clearToken() {
document.cookie = `${COOKIE}=; Max-Age=-99999999;`
removeAccessToken()
removeRefreshToken()
removeTokenExpiry()
}
export function getAccessTokenOrRedirect(): string {
const accessToken = getAccessToken()
if (accessToken) {
return accessToken
} else {
window.location.href = '/sign-in'
return ''
}
}
export function getAccessToken() {
const accessToken = loadAccessToken()
const tokenExpiry = loadTokenExpiry()
if (accessToken && tokenExpiry && new Date() < new Date(tokenExpiry)) {
return accessToken
} else {
clearToken()
}
}
setInterval(async () => {
const refreshToken = loadRefreshToken()
const tokenExpiry = loadTokenExpiry()
if (tokenExpiry && refreshToken) {
const earlyExpiry = new Date(tokenExpiry)
earlyExpiry.setMinutes(earlyExpiry.getMinutes() - 1)
if (new Date() >= earlyExpiry) {
const token = await TokenAPI.exchange({
grant_type: 'refresh_token',
refresh_token: refreshToken,
})
saveToken(token)
}
}
}, 5000)