@noemaresearch/ctf-cli
Version:
A CLI tool for navigating and playing cybersecurity challenges
89 lines (78 loc) • 2.64 kB
text/typescript
import { functions } from '../firebase';
import { httpsCallable } from 'firebase/functions';
import { showErrorBox } from '../auth/emailAuth';
import { input } from '@inquirer/prompts';
import { Auth } from 'firebase/auth';
async function getAuthenticatedUser(auth: Auth) {
const user = auth.currentUser;
if (!user || !user.email) {
throw new Error('Not authenticated');
}
return user;
}
export async function createUser(auth: Auth, myUsername: string, myEmail: string, myCountry: string) {
await getAuthenticatedUser(auth);
let userCreated: boolean = false;
const createUserFunc = httpsCallable(functions, 'createUser');
try {
let result: any = await createUserFunc({ username: myUsername, email: myEmail, country: myCountry });
console.log();
while (userCreated == false) {
if (result.data.status == false && result.data.reason == 'invalid-user') {
console.log();
myUsername = await input({
message: 'Username already taken. Please choose another username:',
})
result = await createUserFunc({ username: myUsername, email: myEmail, country: myCountry });
} else if (result.data.status == true) {
userCreated = true;
} else if (result.data.status == false) {
throw new Error('invalid-username');
}
}
return {
...result.data,
username: myUsername
};
} catch (error: any) {
console.log(error);
if (
(error instanceof Error) && (
error.message.includes('invalid-username') ||
error.message.includes('User force closed')
)
) {
showErrorBox('Invalid username used. please try again.');
}
}
return {
status: false,
reason: 'failed-operation'
};
}
export async function updateUserAutoFeedbackFlag(hasAutoFeedback: boolean) {
const updateAutoFeedbackFlagFunc = httpsCallable(
functions,
'updateUserAutoFeedbackFlag'
)
try {
await updateAutoFeedbackFlagFunc({ hasAutoFeedback })
console.log('Auto feedback preference updated successfully.')
} catch (error) {
console.error('Failed to update auto feedback preference:', error)
}
}
export async function getUserAutoFeedbackFlag(): Promise<boolean | null> {
try {
const getUserAutoFeedbackFlagFunc = httpsCallable<void, { hasAutoFeedback: boolean }>(
functions,
'getUserAutoFeedbackFlag'
)
const result = await getUserAutoFeedbackFlagFunc()
const data = result.data
return data.hasAutoFeedback
} catch (error) {
console.error('Failed to retrieve auto feedback preference:', error)
return null
}
}