forked from sh-edraft.de/sh_discord_bot
Added discord login & removed discord register #128
This commit is contained in:
@@ -70,6 +70,9 @@ class AuthServiceABC(ABC):
|
||||
@abstractmethod
|
||||
async def login_async(self, user_dto: AuthUserDTO) -> TokenDTO: pass
|
||||
|
||||
@abstractmethod
|
||||
async def login_discord_async(self, oauth_dto: AuthUserDTO) -> TokenDTO: pass
|
||||
|
||||
@abstractmethod
|
||||
async def refresh_async(self, token_dto: TokenDTO) -> TokenDTO: pass
|
||||
|
||||
|
@@ -82,8 +82,18 @@ class AuthDiscordController:
|
||||
), response['id'])
|
||||
return jsonify(result.to_dict())
|
||||
|
||||
@Route.post(f'{BasePath}/register')
|
||||
async def discord_register(self):
|
||||
dto: OAuthDTO = JSONProcessor.process(OAuthDTO, request.get_json(force=True, silent=True))
|
||||
await self._auth_service.add_auth_user_by_oauth_async(dto)
|
||||
return '', 200
|
||||
@Route.get(f'{BasePath}/login')
|
||||
async def discord_login(self) -> Response:
|
||||
response = self._get_user_from_discord_response()
|
||||
dto = AuthUserDTO(
|
||||
0,
|
||||
response['username'],
|
||||
response['discriminator'],
|
||||
response['email'],
|
||||
str(uuid.uuid4()),
|
||||
None,
|
||||
AuthRoleEnum.normal
|
||||
)
|
||||
|
||||
result = await self._auth_service.login_discord_async(dto)
|
||||
return jsonify(result.to_dict())
|
||||
|
@@ -460,6 +460,24 @@ class AuthService(AuthServiceABC):
|
||||
self._db.save_changes()
|
||||
return TokenDTO(token, refresh_token)
|
||||
|
||||
async def login_discord_async(self, user_dto: AuthUserDTO) -> TokenDTO:
|
||||
if user_dto is None:
|
||||
raise ServiceException(ServiceErrorCode.InvalidData, 'User not set')
|
||||
|
||||
db_user = self._auth_users.find_auth_user_by_email(user_dto.email)
|
||||
if db_user is None:
|
||||
await self.add_auth_user_async(user_dto)
|
||||
# raise ServiceException(ServiceErrorCode.InvalidUser, f'User not found')
|
||||
|
||||
db_user = self._auth_users.get_auth_user_by_email(user_dto.email)
|
||||
token = self.generate_token(db_user)
|
||||
refresh_token = self._create_and_save_refresh_token(db_user)
|
||||
if db_user.forgot_password_id is not None:
|
||||
db_user.forgot_password_id = None
|
||||
|
||||
self._db.save_changes()
|
||||
return TokenDTO(token, refresh_token)
|
||||
|
||||
async def refresh_async(self, token_dto: TokenDTO) -> TokenDTO:
|
||||
if token_dto is None:
|
||||
raise ServiceException(ServiceErrorCode.InvalidData, f'Token not set')
|
||||
|
Reference in New Issue
Block a user