-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth.ts
More file actions
80 lines (63 loc) · 2.25 KB
/
auth.ts
File metadata and controls
80 lines (63 loc) · 2.25 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {isNullish} from '@dfinity/utils';
import {Ed25519KeyIdentity} from '@icp-sdk/core/identity';
import {assertAnswerCtrlC, hasArgs} from '@junobuild/cli-tools';
import {green, red} from 'kleur';
import prompts from 'prompts';
import {DEV} from '../env';
import {loginEmulatorOnly} from '../services/auth/login.emulator.services';
import {login as loginServices} from '../services/auth/login.services';
import {reuseController} from '../services/controllers.services';
import {clearCliConfig, getToken} from '../stores/config.store';
import {confirmAndExitUnlessHeadlessAndDev} from '../utils/prompt.utils';
export const logout = async () => {
await clearCliConfig();
console.log(green('Logged out'));
};
export const login = async (args?: string[]) => {
if (hasArgs({args, options: ['-e', '--emulator']})) {
await emulatorLogin();
return;
}
await consoleLogin(args);
};
const consoleLogin = async (args?: string[]) => {
const token = await getToken();
if (isNullish(token)) {
await loginServices(args);
return;
}
const identity = Ed25519KeyIdentity.fromParsedJson(token);
console.log(`🔐 Your terminal already has access: ${green(identity.getPrincipal().toText())}\n`);
const {action}: {action: string} = await prompts({
type: 'select',
name: 'action',
message: 'What would you like to do?',
choices: [
{title: `Create a new access key by logging in with your browser`, value: `login`},
{title: `Reuse the access key used by your CLI`, value: `reuse`}
]
});
assertAnswerCtrlC(action);
if (action === 'login') {
await loginServices(args);
return;
}
await reuseController(identity.getPrincipal());
};
const emulatorLogin = async () => {
if (!DEV) {
console.log(red('The login option --emulator is only supported in development mode.'));
return;
}
const token = await getToken();
if (isNullish(token)) {
await loginEmulatorOnly();
return;
}
const identity = Ed25519KeyIdentity.fromParsedJson(token);
console.log(`🔐 Your terminal already has access: ${green(identity.getPrincipal().toText())}\n`);
await confirmAndExitUnlessHeadlessAndDev(
'Would you like to overwrite the saved development authentication on this device'
);
await loginEmulatorOnly();
};