2022-12-27 20:14:55 +00:00
< script lang = "ts" setup >
2023-01-11 22:24:00 +00:00
/* eslint-disable no-alert */
2022-12-29 19:44:51 +00:00
import { fileOpen } from 'browser-fs-access'
2022-12-27 20:14:55 +00:00
import type { UserLogin } from '~/types'
2023-01-04 13:57:12 +00:00
const { t } = useI18n ( )
useHeadFixed ( {
title : ( ) => ` ${ t ( 'settings.users.label' ) } | ${ t ( 'nav.settings' ) } ` ,
} )
2022-12-27 20:14:55 +00:00
const loggedInUsers = useUsers ( )
async function exportTokens ( ) {
2023-02-06 09:34:50 +00:00
if ( process . server )
return
2022-12-27 20:14:55 +00:00
if ( ! confirm ( 'Please aware that the tokens represent the **full access** to your accounts, and should be treated as sensitive information. Are you sure you want to export the tokens?' ) )
return
const { saveAs } = await import ( 'file-saver' )
const data = {
'/' : 'Generated by Elk, you can import it to Elk to restore the tokens.' ,
'//' : 'This file represents the **full access** to your accounts, and should be treated as sensitive information.' ,
'version' : 1 ,
'origin' : location . origin ,
'users' : loggedInUsers . value ,
}
const blob = new Blob ( [ JSON . stringify ( data , null , 2 ) ] , { type : 'application/json;charset=utf-8' } )
saveAs ( blob , ` elk-users-tokens- ${ new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) } .json ` )
}
async function importTokens ( ) {
2023-02-06 09:34:50 +00:00
if ( process . server )
return
2022-12-29 19:44:51 +00:00
const file = await fileOpen ( {
description : 'Token File' ,
mimeTypes : [ 'application/json' ] ,
} )
2022-12-27 20:14:55 +00:00
2022-12-29 19:44:51 +00:00
try {
const content = await file . text ( )
const data = JSON . parse ( content )
if ( data . version !== 1 )
throw new Error ( 'Invalid version' )
const users = data . users as UserLogin [ ]
const newUsers : UserLogin [ ] = [ ]
for ( const user of users ) {
if ( loggedInUsers . value . some ( u => u . server === user . server && u . account . id === user . account . id ) )
continue
newUsers . push ( user )
2022-12-27 20:14:55 +00:00
}
2022-12-29 19:44:51 +00:00
if ( newUsers . length === 0 ) {
alert ( 'No new users found' )
return
2022-12-27 20:14:55 +00:00
}
2022-12-29 19:44:51 +00:00
if ( ! confirm ( ` Found ${ newUsers . length } new users, are you sure you want to import them? ` ) )
return
loggedInUsers . value = [ ... loggedInUsers . value , ... newUsers ]
}
catch ( e ) {
console . error ( e )
alert ( 'Invalid Elk tokens file' )
}
2022-12-27 20:14:55 +00:00
}
< / script >
< template >
2022-12-28 01:12:56 +00:00
< MainContent back -on -small -screen >
2022-12-27 20:14:55 +00:00
< template # title >
< div text -lg font -bold flex items -center gap -2 @click ="$scrollToTop" >
< span > { { $t ( 'settings.users.label' ) } } < / span >
< / div >
< / template >
2022-12-27 23:03:50 +00:00
< div p6 >
2022-12-27 20:21:53 +00:00
< template v-if = "loggedInUsers.length" >
< div flex = "~ col gap2" >
< div v-for = "user of loggedInUsers" :key="user.account.id" >
< AccountInfo :account = "user.account" :hover-card = "false" / >
< / div >
2022-12-27 20:14:55 +00:00
< / div >
2022-12-27 20:21:53 +00:00
< div my4 border = "t base" / >
< button btn -text flex = "~ gap-2" items -center @click ="exportTokens" >
< div i -ri -download -2 -line / >
2022-12-29 19:27:11 +00:00
{ { $t ( 'settings.users.export' ) } }
2022-12-27 20:21:53 +00:00
< / button >
< / template >
2022-12-27 20:14:55 +00:00
< button btn -text flex = "~ gap-2" items -center @click ="importTokens" >
< div i -ri -upload -2 -line / >
2022-12-29 19:27:11 +00:00
{ { $t ( 'settings.users.import' ) } }
2022-12-27 20:14:55 +00:00
< / button >
< / div >
< / MainContent >
< / template >