-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuser.ts
More file actions
34 lines (32 loc) · 948 Bytes
/
user.ts
File metadata and controls
34 lines (32 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { question } from './utils';
import { post, get, replaceSession, saveSession, closeSession } from './api';
import crypto from 'node:crypto';
import { t } from './utils/i18n';
function md5(str: string) {
return crypto.createHash('md5').update(str).digest('hex');
}
export const commands = {
login: async ({ args }: { args: string[] }) => {
const email = args[0] || (await question('email:'));
const pwd = args[1] || (await question('password:', true));
const { token, info } = await post('/user/login', {
email,
pwd: md5(pwd),
});
replaceSession({ token });
await saveSession();
console.log(t('welcomeMessage', { name: info.name }));
},
logout: async () => {
await closeSession();
console.log(t('loggedOut'));
},
me: async () => {
const me = await get('/user/me');
for (const k in me) {
if (k !== 'ok') {
console.log(`${k}: ${me[k]}`);
}
}
},
};