user13716143
user13716143

Reputation:

FastAPI Retrieving User Data

I'm pretty new at creating APIs, so I'm confused on what to search to get my desired outcome. So, essentially, I am creating an application where when users sign up, they have to input their name, email, and password. I want to store their data into a database (which I have as an array right now). I am able to create a Post request where I can add a new user. However, I feel like this isn't very applicable as I am manually inserting these values. What should I be searching/potential resources to look at so that I can input the user's responses of their name, email, and password? Thank you for your help.

This is my main.py thus, far for context:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

data = []
class User(BaseModel):
  userID: int
  name: str
  email: str
  password: str

# get every user
@app.get("/users")
async def get_users():
  return data

#get specific user
@app.get("/users/{user_id}")
async def get_a_user(user_id: int):
  return data[user_id - 1]

#add user
@app.post("/users")
async def add_user(user: User):
  data.append(user.dict())
  return data[-1]

Upvotes: 0

Views: 4512

Answers (2)

William
William

Reputation: 542

If you want to implement a user management system, it is important to pay attention into CSRF and XSS attacks:

CSRF is an attack against a web application in which the attacker attempts to trick an authenticated user into performing a malicious action.

XSS attacks are a type of injection where malicious scripts are injected into the client-side, usually to bypass the browser's same-origin policy.

From testdrinve.io

There are some good examples if you want to create an auth function with good pratices:

  • If you want to store user`s credentials on Non-relational database like MongoDB, you can clone this project from testdriven.io author or this one from FARM creator

  • If you want to combine Mongodb with a frontend framework like react, follow this one

  • If you want to store user credentials on relational Databases, you can check Full Stack FastAPI and PostgreSQL - Base Project Generator - Full stack, modern web application generator, which includes FastAPI, PostgreSQL, Docker, Celery, Vue frontend, automatic HTTPS and more (developed by the creator of FastAPI, Sebastián Ramírez).

Upvotes: 0

Jason Rebelo Neves
Jason Rebelo Neves

Reputation: 1271

You can read, study, and re-use the templated fullstack project from the developer of FastAPI here.

You can follow ALL of the tutorials over at the FastAPI docs.

Your question is a bit too broad to answer, other than handing you the two main resources I used myself.

To sum up the process (more or less) in a few steps:

  • your fastapi endpoint receives a schema (pydantic) from the frontend
  • the endpoint sends that schema to the database layer
  • the database layer deals with modifying that data into database-acceptable data (essentially pydantic <-> sqlalchemy)
  • sqlalchemy then sends that data to the database

Pretty much the same for retrieving data from the database:

  • fastapi endpoint receives a request for data
  • the query is passed onto sqlalchemy
  • sqlalchemy returns an sqlalchemy model, that is transformed into a pydantic schema
  • this schema is then returned to the frontend that knows how to display it to the user

Upvotes: 1

Related Questions