mirror of
https://github.com/handsomezhuzhu/2fa-tool.git
synced 2026-04-18 22:32:53 +00:00
Merge pull request #5 from handsomezhuzhu/v0/kdaugh14-4907-6f76e312
Enhance token management and localize user interface
This commit is contained in:
21
app/page.tsx
21
app/page.tsx
@@ -617,13 +617,20 @@ export default function TwoFactorAuth() {
|
|||||||
|
|
||||||
const imported = JSON.parse(decrypted)
|
const imported = JSON.parse(decrypted)
|
||||||
if (Array.isArray(imported)) {
|
if (Array.isArray(imported)) {
|
||||||
setTokens([...tokens, ...imported])
|
const existingSecrets = new Set(tokens.map((tk) => tk.secret.toUpperCase()))
|
||||||
|
const newTokens = (imported as TOTPToken[]).filter(
|
||||||
|
(tk) => !existingSecrets.has(tk.secret.toUpperCase())
|
||||||
|
)
|
||||||
|
const skipped = imported.length - newTokens.length
|
||||||
|
setTokens((prev) => [...prev, ...newTokens])
|
||||||
setImportPassword("")
|
setImportPassword("")
|
||||||
setImportFile(null)
|
setImportFile(null)
|
||||||
setShowImportPassword(false)
|
setShowImportPassword(false)
|
||||||
toast({
|
toast({
|
||||||
title: t.importSuccess,
|
title: t.importSuccess,
|
||||||
description: `${t.added} ${imported.length} ${t.importedTokens}`,
|
description: skipped > 0
|
||||||
|
? `${t.added} ${newTokens.length} ${t.importedTokens},已跳过 ${skipped} 个重复令牌`
|
||||||
|
: `${t.added} ${newTokens.length} ${t.importedTokens}`,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -1189,14 +1196,14 @@ export default function TwoFactorAuth() {
|
|||||||
<Dialog open={showExportPassword} onOpenChange={setShowExportPassword}>
|
<Dialog open={showExportPassword} onOpenChange={setShowExportPassword}>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Set Export Password</DialogTitle>
|
<DialogTitle>{t.setExportPassword}</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<div className="space-y-4 py-4">
|
<div className="space-y-4 py-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Password</Label>
|
<Label>Password</Label>
|
||||||
<Input
|
<Input
|
||||||
type="password"
|
type="password"
|
||||||
placeholder="Enter a password to protect your backup"
|
placeholder={t.passwordPlaceholder}
|
||||||
value={exportPassword}
|
value={exportPassword}
|
||||||
onChange={(e) => setExportPassword(e.target.value)}
|
onChange={(e) => setExportPassword(e.target.value)}
|
||||||
/>
|
/>
|
||||||
@@ -1229,11 +1236,11 @@ export default function TwoFactorAuth() {
|
|||||||
}}>
|
}}>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Import Backup</DialogTitle>
|
<DialogTitle>{t.importBackup}</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<div className="space-y-4 py-4">
|
<div className="space-y-4 py-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Select File</Label>
|
<Label>{t.selectFile}</Label>
|
||||||
<Input
|
<Input
|
||||||
type="file"
|
type="file"
|
||||||
accept=".enc"
|
accept=".enc"
|
||||||
@@ -1246,7 +1253,7 @@ export default function TwoFactorAuth() {
|
|||||||
<Label>Password</Label>
|
<Label>Password</Label>
|
||||||
<Input
|
<Input
|
||||||
type="password"
|
type="password"
|
||||||
placeholder="Enter the password for this backup"
|
placeholder={t.passwordInput}
|
||||||
value={importPassword}
|
value={importPassword}
|
||||||
onChange={(e) => setImportPassword(e.target.value)}
|
onChange={(e) => setImportPassword(e.target.value)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,12 +1,101 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import type * as React from "react"
|
import * as React from "react"
|
||||||
import { ThemeProvider as NextThemesProvider } from "next-themes"
|
import { createContext, useContext, useEffect, useState } from "react"
|
||||||
|
|
||||||
export { useTheme } from "next-themes"
|
type Theme = "dark" | "light" | "system"
|
||||||
|
|
||||||
type ThemeProviderProps = React.ComponentProps<typeof NextThemesProvider>
|
type ThemeProviderProps = {
|
||||||
|
children: React.ReactNode
|
||||||
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
|
defaultTheme?: Theme
|
||||||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
|
storageKey?: string
|
||||||
|
attribute?: string
|
||||||
|
enableSystem?: boolean
|
||||||
|
disableTransitionOnChange?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
type ThemeProviderState = {
|
||||||
|
theme: Theme
|
||||||
|
setTheme: (theme: Theme) => void
|
||||||
|
resolvedTheme?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const ThemeProviderContext = createContext<ThemeProviderState | undefined>(undefined)
|
||||||
|
|
||||||
|
export function ThemeProvider({
|
||||||
|
children,
|
||||||
|
defaultTheme = "system",
|
||||||
|
storageKey = "theme",
|
||||||
|
...props
|
||||||
|
}: ThemeProviderProps) {
|
||||||
|
const [theme, setTheme] = useState<Theme>(defaultTheme)
|
||||||
|
const [resolvedTheme, setResolvedTheme] = useState<string | undefined>(undefined)
|
||||||
|
const [mounted, setMounted] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setMounted(true)
|
||||||
|
const stored = localStorage.getItem(storageKey) as Theme | null
|
||||||
|
if (stored) {
|
||||||
|
setTheme(stored)
|
||||||
|
}
|
||||||
|
}, [storageKey])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mounted) return
|
||||||
|
|
||||||
|
const root = window.document.documentElement
|
||||||
|
|
||||||
|
root.classList.remove("light", "dark")
|
||||||
|
|
||||||
|
let resolved: string
|
||||||
|
if (theme === "system") {
|
||||||
|
resolved = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
|
||||||
|
} else {
|
||||||
|
resolved = theme
|
||||||
|
}
|
||||||
|
|
||||||
|
root.classList.add(resolved)
|
||||||
|
setResolvedTheme(resolved)
|
||||||
|
}, [theme, mounted])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mounted || theme !== "system") return
|
||||||
|
|
||||||
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
|
||||||
|
const handleChange = () => {
|
||||||
|
const resolved = mediaQuery.matches ? "dark" : "light"
|
||||||
|
const root = window.document.documentElement
|
||||||
|
root.classList.remove("light", "dark")
|
||||||
|
root.classList.add(resolved)
|
||||||
|
setResolvedTheme(resolved)
|
||||||
|
}
|
||||||
|
|
||||||
|
mediaQuery.addEventListener("change", handleChange)
|
||||||
|
return () => mediaQuery.removeEventListener("change", handleChange)
|
||||||
|
}, [theme, mounted])
|
||||||
|
|
||||||
|
const value = {
|
||||||
|
theme,
|
||||||
|
setTheme: (newTheme: Theme) => {
|
||||||
|
localStorage.setItem(storageKey, newTheme)
|
||||||
|
setTheme(newTheme)
|
||||||
|
},
|
||||||
|
resolvedTheme,
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ThemeProviderContext.Provider value={value}>
|
||||||
|
{children}
|
||||||
|
</ThemeProviderContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTheme() {
|
||||||
|
const context = useContext(ThemeProviderContext)
|
||||||
|
|
||||||
|
if (context === undefined) {
|
||||||
|
throw new Error("useTheme must be used within a ThemeProvider")
|
||||||
|
}
|
||||||
|
|
||||||
|
return context
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,6 +116,10 @@ const translations = {
|
|||||||
imageLoadFailed: "图片加载失败",
|
imageLoadFailed: "图片加载失败",
|
||||||
duplicateToken: "令牌已存在",
|
duplicateToken: "令牌已存在",
|
||||||
duplicateTokenDesc: "该密钥的令牌已添加过",
|
duplicateTokenDesc: "该密钥的令牌已添加过",
|
||||||
|
setExportPassword: "设置导出密码",
|
||||||
|
passwordPlaceholder: "输入密码以保护您的备份",
|
||||||
|
selectFile: "选择文件",
|
||||||
|
passwordInput: "输入备份密码",
|
||||||
},
|
},
|
||||||
en: {
|
en: {
|
||||||
// Header
|
// Header
|
||||||
@@ -227,6 +231,10 @@ const translations = {
|
|||||||
imageLoadFailed: "Failed to load image",
|
imageLoadFailed: "Failed to load image",
|
||||||
duplicateToken: "Token already exists",
|
duplicateToken: "Token already exists",
|
||||||
duplicateTokenDesc: "A token with this secret key has already been added",
|
duplicateTokenDesc: "A token with this secret key has already been added",
|
||||||
|
setExportPassword: "Set Export Password",
|
||||||
|
passwordPlaceholder: "Enter a password to protect your backup",
|
||||||
|
selectFile: "Select File",
|
||||||
|
passwordInput: "Enter backup password",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user