Skip to content

Commit

Permalink
Merge pull request #25 from noisrucer/feat/email-verification
Browse files Browse the repository at this point in the history
[Feat] Email verification
  • Loading branch information
noisrucer committed Mar 25, 2023
2 parents 77f9e2f + f9a5d6c commit 282c40f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
23 changes: 22 additions & 1 deletion girok/api/auth.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
import requests
from girok.config import get_config
import girok.utils.general as general_utils
import girok.utils.display as display_utils

cfg = get_config()

base_url = cfg.base_url
email_base_url = cfg.email_base_url

def register(email, password):
resp = requests.post(base_url + "/register", json={
resp = requests.post(email_base_url + "/register", json={
"email": email,
"password": password
})
return resp

def verify_verification_code(email, verification_code):
resp = requests.post(email_base_url + "/register/verification_code", json={
"email": email,
"verification_code": verification_code
})
if resp.status_code == 200:
return True
elif resp.status_code == 401:
err_msg = general_utils.bytes2dict(resp.content)['detail']
display_utils.center_print(err_msg, type="error")
exit(0)
else:
err_msg = general_utils.bytes2dict(resp.content)['detail'][0]['msg']
print(err_msg)
display_utils.center_print(str(err_msg), type="error")
exit(0)


def login(email, password):
Expand Down
24 changes: 16 additions & 8 deletions girok/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import girok.utils.general as general_utils
import girok.utils.auth as auth_utils
import girok.api.auth as auth_api
import girok.utils.display as display_utils

app = typer.Typer(rich_markup_mode='rich')

Expand All @@ -35,7 +36,10 @@ def login():
access_token = general_utils.bytes2dict(resp.content)['access_token']
general_utils.update_json(cfg.config_path, {"access_token": access_token, "email": email})
print("You're logged in!")

elif resp.status_code == 401:
err_msg = general_utils.bytes2dict(resp.content)['detail']
display_utils.center_print(err_msg, type="error")
exit(0)
else:
print("Login failed. Please try again with [green]girok login[/green]")
exit(0)
Expand All @@ -54,7 +58,6 @@ def logout():
exit(0)



@app.command("register", help="[green]Register[/green] a new account", rich_help_panel=":lock: [bold yellow1]Authentication Commands[/bold yellow1]")
def register():
access_token = auth_utils.get_access_token_from_json(cfg.config_path)
Expand All @@ -73,13 +76,18 @@ def register():
# register a new account
register_resp = auth_api.register(email, password)
if register_resp.status_code == 201:
print("Register complete!")
else:
register_resp_content = general_utils.bytes2dict(register_resp.content)
if register_resp.status_code == 400:
print(register_resp_content['detail'])
verification_code = typer.prompt("Enter the verification code sent to your email")
if auth_api.verify_verification_code(email, verification_code):
pass
else:
exit(0)
elif register_resp.status_code == 400:
err_msg = general_utils.bytes2dict(register_resp.content)['detail']
display_utils.center_print(err_msg, type="error")
exit(0)

else:
err_msg = general_utils.bytes2dict(register_resp.content)['detail']
display_utils.center_print(str(err_msg), type="error")
print("Registration successful! Please login by [green]girok login[/green] command")
exit(0)
else:
Expand Down
3 changes: 2 additions & 1 deletion girok/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"app_name": "girok",
"base_url": "http://girok-server-balancer-1565927748.ap-northeast-1.elb.amazonaws.com"
"base_url": "http://girok-server-balancer-1565927748.ap-northeast-1.elb.amazonaws.com",
"email_base_url": "https://girokemail.onrender.com"
}
1 change: 1 addition & 0 deletions girok/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Config:
def __init__(self):
self.config = general_utils.read_json(os.path.join(cur_file_dir, "config.json"))
self.base_url = self.config['base_url']
self.email_base_url = self.config['email_base_url']
self.app_name = self.config['app_name']
self.app_dir = typer.get_app_dir(self.app_name)
self.config_path: Path = Path(self.app_dir) / "config.json"
Expand Down
2 changes: 2 additions & 0 deletions girok/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def build_jwt_header(fpath):


def is_logged_in(access_token):
if access_token is None:
return False
resp = auth_api.validate_access_token(access_token)
return True if resp.status_code == 200 else False

Expand Down

0 comments on commit 282c40f

Please sign in to comment.