Skip to content

Commit

Permalink
return plan from jwt
Browse files Browse the repository at this point in the history
- need to fix the url after plan is returned
  • Loading branch information
mariogiampieri committed Feb 12, 2025
1 parent 6d862f9 commit 257a1c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
2 changes: 1 addition & 1 deletion app/src/app/components/Toolbar/ShareMapsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const ShareMapsModal: React.FC<{
return;
}
// copy to clipboard
const shareableLink = `${window.location.origin}${window.location.pathname}?share=${token}`;
const shareableLink = `${window.location.origin}?share=${token}`;
await navigator.clipboard.writeText(shareableLink);

console.log('Copied link to clipboard: ', shareableLink);
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/utils/api/apiHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ export const getSharePlanLink = async ({
export const getLoadPlanFromShare = async (token: string) => {
try {
const res = await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/api/document/load_plan_from_share`,
`${process.env.NEXT_PUBLIC_API_URL}/api/share/load_plan_from_share`,
{
token: token,
user_id: useMapStore.getState().userID,
Expand Down
93 changes: 40 additions & 53 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ async def get_assignments(document_id: str, session: Session = Depends(get_sessi
async def get_document(
document_id: str,
user_id: UserID,
session: Session = Depends(get_session),
session: Session,
):

print("doc id in get doc: ", document_id)
status = check_map_lock(document_id, user_id, session)

stmt = (
Expand Down Expand Up @@ -421,10 +421,7 @@ async def unlock_document(
document_id: str, data: UserID = Body(...), session: Session = Depends(get_session)
):
try:
print("\n")
print("running unlock_document")
print("\n")
stmt = session.execute(
session.execute(
text(
"""DELETE FROM document.map_document_user_session
WHERE document_id = :document_id AND user_id = :user_id"""
Expand All @@ -435,8 +432,7 @@ async def unlock_document(
)
.params(document_id=document_id, user_id=data.user_id)
)
print(stmt)
session.execute(stmt)

session.commit()
return {"status": "unlocked"}
except Exception as e:
Expand Down Expand Up @@ -791,61 +787,52 @@ async def share_districtr_plan(
return {"token": token}


@app.post("/api/document/load_plan_from_share", status_code=status.HTTP_200_OK)
@app.post("/api/share/load_plan_from_share", response_model=DocumentPublic)
async def load_plan_from_share(
data: TokenRequest,
session: Session = Depends(get_session),
):
try:
# try:

token_id = data.token
token_id = data.token
print("token_id: ", token_id)
result = session.execute(
text(
"""
SELECT document_id, password_hash, expiration_date
FROM document.map_document_token
WHERE token_id = :token
"""
),
{"token": token_id},
).fetchone()
# assert result, "No document found for token!"
# assert result.document_id == "foo", "Invalid document_id retrieved!"

result = session.execute(
text(
"""
SELECT document_id, password_hash, expiration_date
FROM document.map_document_token
WHERE token_id = :token
"""
),
{"token": token_id},
).fetchone()
print("\n results:")
print(result.document_id)
print("\n")

if not result:
if not result:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Token not found",
)
if result.password_hash:
if data.password is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Token not found",
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Password required",
)
if not verify_password(data.password, result.password_hash):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid password",
)
if result.password_hash:
if data.password is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Password required",
)
if not verify_password(data.password, result.password_hash):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid password",
)
print("\n")
print(token_id)
print("\n")
# return the document to the user with the password
return await get_document("foo", token_id, data.user_id, session)
# else:
# # return the document to the user
# return get_document(result.document_id, {"user_id": data.user_id}, session)

except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has expired",
)
except jwt.InvalidTokenError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token",
)
# return the document to the user with the password
# status = check_map_lock(result.document_id, data.user_id, session)
return await get_document(str(result.document_id), data.user_id, session)


@app.get("/api/document/{document_id}/export", status_code=status.HTTP_200_OK)
Expand Down

0 comments on commit 257a1c7

Please sign in to comment.