Using more pydantic features in the backend and fixing env issues on the website

This commit is contained in:
Keith Stevens
2023-01-28 19:35:40 +09:00
parent 3dc8ff6ddd
commit 3197b6088b
4 changed files with 8 additions and 7 deletions
+3 -6
View File
@@ -1,4 +1,3 @@
import json
from typing import Union
from cryptography.hazmat.primitives import hashes
@@ -7,7 +6,7 @@ from fastapi import APIRouter, Depends, Security
from fastapi.security import APIKeyCookie
from jose import jwe
from oasst_backend.config import settings
from pydantic import BaseModel
from pydantic import BaseModel, EmailStr
router = APIRouter()
@@ -19,7 +18,7 @@ class TokenData(BaseModel):
A minimal re-creation of the web's token type. To be expanded later.
"""
email: Union[str, None] = None
email: Union[EmailStr, None] = None
async def get_current_user(token: str = Security(oauth2_scheme)):
@@ -38,9 +37,7 @@ async def get_current_user(token: str = Security(oauth2_scheme)):
# Next we decrypt the JWE token.
payload = jwe.decrypt(token, key)
# Finally we have the real token JSON payload and can do whatever we want.
content = json.loads(payload)
email = content["email"]
return TokenData(email=email)
return TokenData.parse_raw(payload)
@router.get("/check", response_model=str)
+1
View File
@@ -7,6 +7,7 @@ loguru==0.6.0
numpy==1.22.4
psycopg2-binary==2.9.5
pydantic==1.9.1
pydantic[email]==1.9.1
python-dotenv==0.21.0
python-jose[cryptography]==3.3.0
redis
+3
View File
@@ -7,6 +7,9 @@ DATABASE_URL=postgres://postgres:postgres@localhost:5433/oasst_web
FASTAPI_URL=http://localhost:8080
FASTAPI_KEY=1234
# Used to expose the backend url to the clientside javascript
NEXT_PUBLIC_BACKEND_URL=$FASTAPI_URL
# A dev Auth Secret. Can be exposed if we never use this publicly.
NEXTAUTH_SECRET=O/M2uIbGj+lDD2oyNa8ax4jEOJqCPJzO53UbWShmq98=
+1 -1
View File
@@ -12,7 +12,7 @@ import useSWR from "swr";
const Dashboard = () => {
// Adding a demonstrative call to the backend that includes the web's JWT.
useSWR(`${process.env.FASTAPI_URL}/api/v1/auth/check`, get);
useSWR(`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/v1/auth/check`, get);
const {
t,