23 lines
624 B
Python
23 lines
624 B
Python
from repositories.user_repo import UserRepository
|
|
from common.exceptions import BusinessException
|
|
from flask_jwt_extended import create_access_token
|
|
|
|
class AuthService:
|
|
|
|
@staticmethod
|
|
def login(username: str, password: str):
|
|
user = UserRepository.find_by_username(username)
|
|
|
|
if not user:
|
|
raise BusinessException(40101, "User not found")
|
|
|
|
if user.password != password:
|
|
raise BusinessException(40102, "Invalid credentials")
|
|
|
|
token = create_access_token(identity=user.id)
|
|
|
|
return {
|
|
"token": token,
|
|
"user": user.to_dict()
|
|
}
|