text: from fastapi import APIRouter, Depends, HTTPException, status
  from fastapi.security import OAuth2PasswordRequestForm
  from mongomapper.errors import DocumentNotFoundError
  
  from app.models import User, Session, LoginResponse
  from app.dependencies import get_current_session
  
  router = APIRouter(prefix='/session')
  
  @router.post('/', status_code=status.HTTP_201_CREATED)
  def login(form_data: OAuth2PasswordRequestForm = Depends()):
    """Logs in"""
    try:
      user = User.query.where(emails=form_data.username).find_one()
    except DocumentNotFoundError:
      raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
    
    # Checks the user password
    if not user.password.check(form_data.password):
      raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
    
    session = Session.create(user=user)
    token   = LoginResponse(access_token=str(session._id))
    
    return token.json()
  
  @router.get('/')
  def get_session(session: Session = Depends(get_current_session)):
    return session.json()
  
  @router.get('/logout')
  def logout(session: Session = Depends(get_current_session)):
    """Logs out"""
    session.closed = True
    session.save()