mirror of
https://github.com/rustfs/console.git
synced 2026-09-19 09:52:25 +08:00
- Added comprehensive TypeScript type definitions for app configuration - Fixed type safety issues in sidebar component with proper type casting - Improved code organization with better file naming conventions - Enhanced error handling with centralized error management - Added performance optimizations with caching and code splitting - Improved development experience with better tooling configuration - Updated project documentation with comprehensive README - Added proper type definitions for navigation items and app config - Fixed licensing information and added proper Apache 2.0 license 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from "@aws-sdk/types";
|
|
import { getStsToken } from "~/lib/sts";
|
|
import type { SiteConfig } from "~/types/config";
|
|
|
|
interface Credentials {
|
|
AccessKeyId?: string;
|
|
SecretAccessKey?: string;
|
|
SessionToken?: string;
|
|
Expiration?: string;
|
|
}
|
|
|
|
export function useAuth() {
|
|
const store = useLocalStorage('auth.credentials', {})
|
|
|
|
const setCredentials = (credentials: Credentials) => {
|
|
store.value = credentials
|
|
}
|
|
|
|
const getCredentials = () => {
|
|
if (!isValidCredentials(store.value)) {
|
|
return
|
|
}
|
|
|
|
return store.value
|
|
}
|
|
|
|
const isExpired = (expiration: string) => expiration ? new Date(expiration) < new Date() : false
|
|
|
|
const isValidCredentials = (credentials: Credentials) => {
|
|
return !!credentials?.AccessKeyId && !!credentials?.SecretAccessKey && !!credentials?.SessionToken && credentials?.Expiration && !isExpired(credentials.Expiration)
|
|
}
|
|
|
|
const login = async (
|
|
credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider,
|
|
customConfig?: SiteConfig
|
|
) => {
|
|
const credentialsResponse = await getStsToken(credentials, 'arn:aws:iam::*:role/Admin', customConfig)
|
|
|
|
setCredentials({
|
|
...credentialsResponse,
|
|
Expiration: credentialsResponse.Expiration?.toISOString()
|
|
})
|
|
|
|
return credentialsResponse
|
|
}
|
|
|
|
const logout = () => {
|
|
store.value = {}
|
|
}
|
|
|
|
const logoutAndRedirect = () => {
|
|
logout()
|
|
window.location.href = '/auth/login'
|
|
}
|
|
|
|
return {
|
|
login,
|
|
logout,
|
|
logoutAndRedirect,
|
|
credentials: ref<Credentials | undefined>(getCredentials()),
|
|
isAuthenticated: ref(isValidCredentials(store.value)),
|
|
}
|
|
}
|