I’m trying to add loggin in the the CodeComm API.

It isn’t working…

help

from fastapi import FastAPI
import json
import  re
from passlib.hash import pbkdf2_sha256
import ast

app = FastAPI()
accregex = r"^[a-z0-9_\\-]{0,20}$"

@app.get('/')
def main():
  return {'codecomm': 'v1'}

@app.get('/accounts/join')
def join(username: str, password: str):
  if re.fullmatch(accregex, username):
    #hash password
    password = pbkdf2_sha256.hash(password)
    accjson = {username: {"name": username, "password": password, "admin": False}}

    #write user data to users.json
    with open('users.json') as f:
      data = json.load(f)
      data.update(accjson)
    with open('users.json', 'w') as f:
      json.dump(data, f)

    return {'ok': 'made account'}
  else:
    return {'error': 'invalid username'}

@app.get('/accounts/login')
def login(username: str, password: str):
  global userdata, userpwdhash
  userdata = json.loads('users.json')
  userdata = ast.literal_eval(userdata)
  if username in userdata:
    userdata = json.loads(userdata)
    userpwdhash = userdata.get('password')
  else:
    return {'error': 'user not found'}
  pbkdf2_sha256.verify(password, userpwdhash)
  return {'ok': 'logged in'}
  

@codecomm

comments (single view)

What error do you receiv?

https://codecomm-api--amorogos.repl.co/docs

try this

clickt eh login dropdown

the details are

account and password is account

error:

File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 366, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in call
    return await self.app(scope, receive, send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/fastapi/applications.py", line 269, in call
    await super().__call__(scope, receive, send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/applications.py", line 124, in call
    await self.middleware_stack(scope, receive, send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 184, in call
    raise exc
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 162, in call
    await self.app(scope, receive, _send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 93, in call
    raise exc
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in call
    await self.app(scope, receive, sender)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call
    raise e
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call
    await self.app(scope, receive, send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/routing.py", line 670, in call
    await route.handle(scope, receive, send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/routing.py", line 266, in handle
    await self.app(scope, receive, send)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/routing.py", line 65, in app
    response = await func(request)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/fastapi/routing.py", line 227, in app
    raw_response = await run_endpoint_function(
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/fastapi/routing.py", line 162, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/runner/CodeComm-API/venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/home/runner/CodeComm-API/./main.py", line 35, in login
    userdata = json.loads('users.json')
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/json/__init__.py", line 357, in loads
    return defaultdecoder.decode(s)
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
View all comments