예제 #1
0
    def authenticate_user(
        user: schemas.UserAuthenticate, db: Session = Depends(get_db)
    ):
        db_user = crud.get_user_by_username(db, username=user.username)
        if db_user is None:
            raise HTTPException(status_code=400, detail="Username not existed")
        else:
            is_password_correct = crud.check_username_password(db, user)
            if is_password_correct is False:
                raise HTTPException(
                    status_code=400, detail="Password is not correct"
                )  # noqa
            else:
                from datetime import timedelta

                access_token_expires = timedelta(
                    minutes=ACCESS_TOKEN_EXPIRE_MINUTES
                )  # noqa
                from app_utils import create_access_token

                access_token = create_access_token(
                    data={"sub": user.username},
                    expires_delta=access_token_expires,  # noqa
                )
                return {"access_token": access_token, "token_type": "Bearer"}
예제 #2
0
def authenticate_user(user: schemas.UserAuthenticate,
                      db: Session = Depends(get_db)):
    db_user = crud.get_user_by_username(db, username=user.username)
    if db_user is None:
        raise HTTPException(status_code=400, detail="Username not existed")
    else:
        try:
            is_password_correct = crud.check_username_password(db, user)
            if is_password_correct is False:
                raise HTTPException(status_code=400,
                                    detail="Password is not correct")
            else:
                from datetime import timedelta
                access_token_expires = timedelta(minutes=15)
                from crud import create_access_token
                access_token = create_access_token(
                    data={"sub": user.username},
                    expires_delta=access_token_expires)
            return_value = {
                "access_token": access_token,
                "token_type": "Bearer"
            }
            return crud.create_user(db=db, user=user)
        except Exception as e:
            print(e)
            raise HTTPException(status_code=501, detail="Error here")
예제 #3
0
 def mutate(root, info, username, password):
     db_user = crud.get_user_by_username(db, username=username)
     user_authenticate = UserAuthenticate(username=username,
                                          password=password)
     if db_user is None:
         raise GraphQLError("Username not existed")
     else:
         is_password_correct = crud.check_username_password(
             db, user_authenticate)
         if is_password_correct is False:
             raise GraphQLError("Password is not correct")
         else:
             from datetime import timedelta
             access_token_expires = timedelta(
                 minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
             from app_utils import create_access_token
             access_token = create_access_token(
                 data={"sub": username}, expires_delta=access_token_expires)
             return AuthenUser(token=access_token)
예제 #4
0
def authenticate_user(form_data: OAuth2PasswordRequestForm = Depends(),
                      db: Session = Depends(get_db)):
    db_user = crud.get_user_by_username(db, username=form_data.username)
    if db_user is None:
        raise HTTPException(status_code=400, detail="No such Username")
    else:
        user = schemas.UserAuthenticate(username=form_data.username,
                                        password=form_data.password)
        is_password_correct = crud.check_username_password(db, user)
        if is_password_correct is False:
            raise HTTPException(status_code=400,
                                detail="Password is not correct")
        else:
            from datetime import timedelta
            access_token_expires = timedelta(
                minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
            from app_utils import create_access_token
            access_token = create_access_token(
                data={"sub": form_data.username},
                expires_delta=access_token_expires)
            return {"access_token": access_token, "token_type": "Bearer"}