async def forredirect(request: Request, db: Session = Depends(get_db)): if request.query_params["state"] != request.session["state"]: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) code = request.query_params["code"] params = { "code": code, "client_id": osenv.GOOGLE_CLIENT_ID, "client_secret": osenv.GOOGLE_CLIENT_SECRET, 'redirect_uri': f'{request.base_url}forredirect', "grant_type": "authorization_code", } res = requests.post(urls.GOOGLE_GET_TOKEN_URL, data=params) response_json = res.json() id_token = response_json.get('id_token') res_info = jwt.decode(id_token, options={"verify_signature": False}) user_email = res_info.get('email') user_name = res_info.get('given_name') request.session['user_email'] = user_email request.session['user_name'] = user_name request.session['user_token'] = response_json['access_token'] email = schemas.UserCreate(email=user_email) db_user = crud.get_user_by_email(db=db, email=user_email) if db_user: if not crud.get_user_hiragana_score(db=db, user_id=db_user.id): crud.create_user_scoreboard(db=db, user_id=db_user.id) else: current_db_user = crud.create_user(db=db, user=email) crud.create_user_scoreboard(db=db, user_id=current_db_user.id) return RedirectResponse('/')
def create_user(db: Session, user: schemas.UserCreate): hashed_password = get_password_hash(user.password) db_user = models.User(login=user.login, hashed_password=hashed_password) db.add(db_user) db.commit() db.refresh(db_user) return schemas.UserCreate(login=user.login, password=hashed_password)
def create_user_open( *, db: Session = Depends(deps.get_db), password: str = Body(...), email: EmailStr = Body(...), full_name: str = Body(None), ) -> Any: """ Create new user without the need to be logged in. """ if not settings.USERS_OPEN_REGISTRATION: raise HTTPException( status_code=403, detail="Open user registration is forbidden on this server", ) user = crud.user.get_by_email(db, email=email) if user: raise HTTPException( status_code=400, detail="The user with this username already exists in the system", ) user_in = schemas.UserCreate(password=password, email=email, full_name=full_name) user = crud.user.create(db, obj_in=user_in) return user
def signup(credentials: Credentials, db: Session = Depends(get_db)): if get_user_by_email(db, credentials.email): raise HTTPException(402, detail="User with that email already exists") user = create_user( db, schemas.UserCreate(email=credentials.email, password=credentials.password)) send_email(user.email) return {"result_code": 200, "user_id": user.id, "user_email": user.email}
def init_db(db: Session) -> None: # Tables should be created with Alembic migrations # But if you don't want to use migrations, create # the tables un-commenting the next line # Base.metadata.create_all(bind=engine) user = crud.user.get_by_email(db, email=settings.FIRST_USER_EMAIL) if not user: user_in = schemas.UserCreate( email=settings.FIRST_USER_EMAIL, password=settings.FIRST_USER_PASSWORD, ) user = crud.user.create(db, obj_in=user_in) apartment_name = "apartment_1" apartment_1 = crud.apartment.get_by_name( db, owner_id=user.id, apartment_name=apartment_name ) if not apartment_1: apartment_1 = schemas.ApartmentCreate( owner_id=user.id, name=apartment_name, ) crud.apartment.create(db, obj_in=apartment_1) apartment_name = "apartment_2" apartment_2 = crud.apartment.get_by_name( db, owner_id=user.id, apartment_name=apartment_name ) if not apartment_2: apartment_2 = schemas.ApartmentCreate( owner_id=user.id, name=apartment_name, ) crud.apartment.create(db, obj_in=apartment_2) apartment_name = "apartment_3" apartment_3 = crud.apartment.get_by_name( db, owner_id=user.id, apartment_name=apartment_name ) if not apartment_3: apartment_3 = schemas.ApartmentCreate( owner_id=user.id, name=apartment_name, ) crud.apartment.create(db, obj_in=apartment_3) apartment_name = "apartment_4" apartment_4 = crud.apartment.get_by_name( db, owner_id=user.id, apartment_name=apartment_name ) if not apartment_4: apartment_4 = schemas.ApartmentCreate( owner_id=user.id, name=apartment_name, ) crud.apartment.create(db, obj_in=apartment_4)
def init_db(db: Session) -> None: # Tables should be created with Alembic migrations # But if you don't want to use migrations, create # the tables un-commenting the next line # Base.metadata.create_all(bind=engine) user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER) if not user: user_in = schemas.UserCreate( email=settings.FIRST_SUPERUSER, password=settings.FIRST_SUPERUSER_PASSWORD, is_superuser=True, ) user = crud.user.create(db, obj_in=user_in) # noqa: F841
def create_user_open( *, db: Session = Depends(deps.get_db), password: str = Body(...), email: EmailStr = Body(...), company_name: str = Body(...) ) -> Any: """ Create new user without the need to be logged in. """ # TODO to think about removing business logic to services if not settings.USERS_OPEN_REGISTRATION: raise HTTPException( status_code=403, detail="Open user registration is forbidden on this server", ) if crud.user.get_by_email(db, email=email): raise HTTPException( status_code=400, detail="The user with this email already exists in the system", ) company = crud.company.get_by_name(db=db, name=company_name) new_company = False is_superuser = False if not company: company = crud.company.create( db=db, obj_in=schemas.CompanyCreate(name=company_name)) new_company = True is_superuser = True user_in = schemas.UserCreate(password=password, email=email, is_superuser=is_superuser) user = crud.user.create(db, obj_in=user_in) role = Roles.OWNER if new_company else Roles.USER role_in = schemas.RoleCreate(role=role, company_id=company.id, user_id=user.id) crud.role.create(db=db, obj_in=role_in) return user