from fastapi import APIRouter, Query from request import RequestLogin from response import ResponseLogin as res from service import LoginService from function import support_function from fastapi import HTTPException from response import ResponseLogin as res router = APIRouter() @router.post('/login', tags=["Login"]) async def login(request: RequestLogin.RequestLoginEmail): email = request.email check = support_function.check_value_email_controller(email) if check is not True: return check password = request.password if password is None or password.strip() == "": return res.ReponseError(status=400, data=res.Message(message="Password is required.")) return LoginService.login(request) @router.post('/login_google', tags=["Login"]) async def login_google(request: RequestLogin.RequestLoginGoogle): email = request.email token_google = request.token_google check = support_function.check_value_email_controller(email) if check is not True: return check if token_google is None or token_google.strip() == "": return res.ReponseError(status=400, data=res.Message(message="token_google oauth2 is required.")) if token_google.isdigit(): return res.ReponseError(status=400, data=res.Message(message="token_google must be a string, not a number.")) return LoginService.login_google(request) @router.post("/update_user_info", tags=["Login"]) async def update_user_info(request: RequestLogin.RequestUpdateUserInfo): user_id = request.user_id check = support_function.check_value_user_id_controller(user_id) if check is not True: return check uid = request.uid email = request.email display_name = request.display_name photo_url = request.photo_url if uid is None or uid.strip() == "": return res.ReponseError(status=400, data=res.Message(message="uid field is required.")) if email is None or email.strip() == "": return res.ReponseError(status=400, data=res.Message(message="email field is required.")) if display_name is None or display_name.strip() == "": return res.ReponseError(status=400, data=res.Message(message="display_name field is required.")) if photo_url is None or photo_url.strip() == "": return res.ReponseError(status=400, data=res.Message(message="photo_url field is required.")) return LoginService.update_user_info(request) @router.get('/check_info_google', tags=["Login"]) async def check_info_google(user_id: str = Query(None)): check = support_function.check_value_user_id_controller(user_id) if check is not True: return check request = RequestLogin.RequestCheckInfoGoogle(user_id=user_id) return LoginService.check_info_google(request) @router.get('/check_info_google_signup', tags=["Login"]) async def check_info_google_signup(email: str = None): check = support_function.check_value_email_controller(email) if check is not True: return check request = RequestLogin.RequestCheckInfoGoogleEmail(email=email) return LoginService.check_info_google_email(request) @router.get('/check_state_login', tags=["Login"]) async def check_state_login(user_id: str = Query(None), session_id_now: str = Query(None)): check = support_function.check_value_user_id_controller(user_id) if check is not True: return check if session_id_now is None or session_id_now.strip() == "": return res.ReponseError(status=400, data=res.Message(message="Session Id is required.")) try: int(session_id_now) return res.ReponseError(status=400, data=res.Message(message="Session Id must be a string, not a number.")) except ValueError: pass request = RequestLogin.RequestCheckStateLogin(user_id=user_id, session_id_now=session_id_now) return LoginService.check_state_login(request) @router.post('/sign_up', tags=["Login"]) async def signup(request: RequestLogin.RequestSignUp): email = request.email check = support_function.check_value_email_controller(email) if check is not True: return check password = request.password if password is None or password.strip( )== "": return res.ReponseError(status=400, data=res.Message(message="Password is required.")) return LoginService.sign_up(request) @router.post('/reset_password', tags=["Login"]) async def reset_password(request: RequestLogin.RequestResetPassword): email = request.email check = support_function.check_value_email_controller(email) if check is not True: return check return LoginService.reset_password(request) @router.put('/change_password', tags=["Login"]) async def reset_password_firebase(request: RequestLogin.RequestChangePassword): user_id = request.user_id check = support_function.check_value_user_id_controller(user_id) if check is not True: return check new_password = request.new_password current_password = request.current_password if new_password is None or new_password.strip() == "": return res.ReponseError(status=400, data=res.Message(message="New password field is required.")) if current_password is None or current_password.strip() == "": return res.ReponseError(status=400, data=res.Message(message="Current password field is required.")) return LoginService.change_password(request) @router.post('/refresh_token/', tags=["Login"]) async def refresh_token_account(request: RequestLogin.RequestRefreshTokenLogin): user_id = request.user_id check = support_function.check_value_user_id_controller(user_id) if check is not True: return check token = request.token if token is None or token.strip() == "": return res.ReponseError(status=400, data=res.Message(message="token is required.")) elif token.isdigit(): return res.ReponseError(status=400, data=res.Message(message="token must be string")) return LoginService.refresh_token(request)