Ejemplo n.º 1
0
async def login(request: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
	"""
	informe o email e senha para se autenticar, assim poderá usar os recursos da lista de desejos.
	"""
	# user = db.query(models.User).filter(models.User.email == request.username).first()
	query = users.select().where(users.c.email == request.username)
	user = await database.fetch_one(query)

	if not user:
		raise HTTPException(
			status_code=status.HTTP_401_UNAUTHORIZED,
			detail=f"Invalid email or password."
		)

	user = schemas.Login(**user)
	password = f"{request.password}"
	if not Hash.verify(user.password, password):
		raise HTTPException(
			status_code=status.HTTP_401_UNAUTHORIZED,
			detail=f"Invalid email or password."
		)

	access_token = create_access_token(
		data={"sub": user.email}
	)
	return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 2
0
def create_user(request: schemas.User, db: Session):
    new_user = models.User(name=request.name,
                           email=request.email,
                           password=Hash.bcrypt(request.password))
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return new_user
Ejemplo n.º 3
0
def create(request: User, db: Session = Depends(get_db)):
    request.password = Hash.bcrypt(request.password)
    new_user = Users(**request.dict())
    print(new_user)
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return new_user
Ejemplo n.º 4
0
 def create(user: schemas.User, db: Session) -> schemas.User:
     novo_user = models.User(name=user.name,
                             email=user.email,
                             password=Hash.bcrypt(user.password))
     db.add(novo_user)
     db.commit()
     db.refresh(novo_user)
     return novo_user
Ejemplo n.º 5
0
def create(request: schema.User, db: Session = Depends(databases.get_db)):
    new_user = model.User(name=request.name,
                          email=request.email,
                          password=Hash.bcrypt(request.password))
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return new_user
Ejemplo n.º 6
0
def createuser(request: schemas.User,
               db: Session = Depends(get_db),
               current_user: schemas.User = Depends(oauth2.get_current_user)):
    new_user = models.User(name=request.name,
                           email=request.email,
                           password=Hash.bcrypt(request.password))
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return new_user
Ejemplo n.º 7
0
def login(request, db: Session = Depends(get_db)):
    user = db.query(models.User).filter(models.User.email == request.username).first()
    if not user:
        raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, detail = 'Invalid Credentials')

    if not Hash.verify(request.password, user.password):
        raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, detail = 'Invalid Password')

    access_token = JWTtoken.create_access_token(data={"email": user.email, "user_id": user.id})
    return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 8
0
def login(request: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(database.get_db)):
    user= db.query(model.User).filter(model.User.email == request.username).first() #searching db for user
    if not user:
        raise HTTPException(status_code= 404, detail=f'Invalid Credentials')
    if not Hash.verify(user.password, request.password): #comparing hashed and input password
        raise HTTPException(status_code= 404, detail=f'Incorrect Password Credentials')
    #generating access token
    access_token = token.create_access_token(
        data={"sub": user.email}
    )
    return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 9
0
def login(request: OAuth2PasswordRequestForm = Depends(),
          db: Session = Depends(get_db)):
    user = db.query(
        models.User).filter(models.User.email == request.username).first()
    if not user:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail='user not found')
    if not Hash.verify(user.password, request.password):
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail='Incorrect password')
    access_token = JWT_tokens.create_access_token(
        data={"sub": user.email}, expires_delta=access_token_expires)
    return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 10
0
def login(request: OAuth2PasswordRequestForm = Depends(),
          db: Session = Depends(database.get_db)):
    user = db.query(
        models.User).filter(models.User.email == request.username).first()
    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f'There is no username with such name: {request.username}')
    if not Hash.verify(user.password, request.password):
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                            detail=f'Invalid password for {request.username}')

    access_token = JWTtoken.create_access_token(data={"sub": user.email})
    return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 11
0
def login(request: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
    user = db.query(Users).filter(Users.email == request.username).first()
    if not user:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Invalid Username or Password')
    if not Hash.verify(request.password, user.password):
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Invalid Username or Password')
    data = {
        'id': user.id,
        'username': user.email,
    }
    access_token = Token.create_access_token(data)
    return {
        'access_token': access_token,
        'token_type': 'bearer'
    }
Ejemplo n.º 12
0
def login(request: OAuth2PasswordRequestForm = Depends(),
          db: Session = Depends(get_db)):
    user = db.query(
        models.User).filter(models.User.email == request.username).first()
    if not user:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail=f"Invalid credentials")

    if not Hash.verify(request.password, user.password):
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail=f"Incorrect password")

    access_token = create_access_token(data={"sub": user.email})

    return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 13
0
def login(request: schema.Login, db: Session = Depends(databases.get_db)):
    user = db.query(
        model.User).filter(model.User.email == request.username).first()
    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f'invalid username please try with corrcet username')
    if not Hash.verify(
            user.password, request.password
    ):  #here user.password means the hashpassword and request.password the plain passweord
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f'invalid password please try with correct password')

    access_token = token.create_access_token(data={"sub": user.email})
    return {"access_token": access_token, "token_type": "bearer"}
Ejemplo n.º 14
0
 async def create_async(user: schemas.User):
     query = users.insert().values(name=user.name,
                                   email=user.email,
                                   password=Hash.bcrypt(user.password))
     return await database.execute(query)
Ejemplo n.º 15
0
def create(request, db: Session = Depends(get_db)):
    new_user = models.User(name=request.name, email=request.email, password=Hash.bcrypt(request.password))
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return {'data': {'message': 'User created', 'respose': new_user}}