create user token via cli

This commit is contained in:
MaysWind
2024-11-09 23:43:27 +08:00
parent 79fd9070e4
commit b8253b6dcc
6 changed files with 158 additions and 63 deletions
+47 -5
View File
@@ -99,6 +99,10 @@ export function generateRandomUUID() {
return uuid;
}
export function isSessionUserAgentCreatedByCli(ua) {
return ua === 'ezbookkeeping Cli';
}
export function parseUserAgent(ua) {
const uaParseRet = uaParser(ua);
@@ -119,13 +123,16 @@ export function parseUserAgent(ua) {
};
}
export function parseDeviceInfo(ua) {
const uaInfo = parseUserAgent(ua);
export function parseDeviceInfo(uaInfo) {
if (!uaInfo) {
return '';
}
let result = '';
if (uaInfo.device.model) {
if (uaInfo.device && uaInfo.device.model) {
result = uaInfo.device.model;
} else if (uaInfo.os.name) {
} else if (uaInfo.os && uaInfo.os.name) {
result = uaInfo.os.name;
if (uaInfo.os.version) {
@@ -133,7 +140,7 @@ export function parseDeviceInfo(ua) {
}
}
if (uaInfo.browser.name) {
if (uaInfo.browser && uaInfo.browser.name) {
let browserInfo = uaInfo.browser.name;
if (uaInfo.browser.version) {
@@ -154,6 +161,41 @@ export function parseDeviceInfo(ua) {
return result;
}
export function parseSessionInfo(token) {
const isCreatedByCli = isSessionUserAgentCreatedByCli(token.userAgent);
const uaInfo = parseUserAgent(token.userAgent);
let deviceType = '';
if (isCreatedByCli) {
deviceType = 'cli';
} else {
if (uaInfo && uaInfo.device) {
if (uaInfo.device.type === 'mobile') {
deviceType = 'phone';
} else if (uaInfo.device.type === 'wearable') {
deviceType = 'wearable';
} else if (uaInfo.device.type === 'tablet') {
deviceType = 'tablet';
} else if (uaInfo.device.type === 'smarttv') {
deviceType = 'tv';
} else {
deviceType = 'default';
}
} else {
deviceType = 'default';
}
}
return {
tokenId: token.tokenId,
isCurrent: token.isCurrent,
deviceType: deviceType,
deviceInfo: isCreatedByCli ? token.userAgent : parseDeviceInfo(uaInfo),
createdByCli: isCreatedByCli,
lastSeen: token.lastSeen
}
}
export function makeButtonCopyToClipboard({ text, el, successCallback, errorCallback }) {
const clipboard = new Clipboard(el, {
text: function () {