Coretex
login.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 import click
19 
20 from ..modules import user, ui
21 from ...configuration import UserConfiguration, InvalidConfiguration, ConfigurationNotFound, utils
22 
23 
24 @click.command()
25 def login() -> None:
26  try:
27  userConfig = UserConfiguration.load()
28  if not ui.clickPrompt(
29  f"User already logged in with username {userConfig.username}.\nWould you like to log in with a different user (Y/n)?",
30  type = bool,
31  default = True,
32  show_default = False
33  ):
34  return
35 
36  except (ConfigurationNotFound, InvalidConfiguration):
37  pass
38 
39  ui.stdEcho("Please enter your credentials:")
40  userConfig = user.configUser()
41 
42  initialData = utils.fetchInitialData()
43  userConfig.frontendUrl = initialData.get("frontend_url", "app.coretex.ai/")
44 
45  userConfig.save()
46  ui.successEcho(f"User {userConfig.username} successfully logged in.")