Does anyone know how to make it work

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)

with open("users.json","r") as file: #r is reading mode

userdata = json.loads(file.read()) #outputs str

Make sure users.json isnt an empty file. Else you will get an error

It isn’t empty

First change your code as I said

Okay, it works but then I get

ValueError: malformed node or string: {'new': {'name': 'new', 'password': '$pbkdf2-sha256$29000$hdC6917rXWvNGaNUypnzPg$Yz9fGhHEJx8au6JrpqchPtiricLZDY87GeCnODJwnrQ', 'admin': False}}

Remove ast.literal_eval line and tell me

TypeError: hash must be unicode or bytes, not None

See more replies

I think it has to do with passlib

Hey!!! It works

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
  with open("users.json","r") as file: #r is reading mode
    userdata = json.loads(file.read()) #outputs str
  if username in userdata:
    userpwdhash = userdata.get(username).get('password')
  else:
    return {'error': 'user not found'}
  pbkdf2_sha256.verify(password, userpwdhash)
  return {'ok': 'logged in'}
  
See more replies
View all comments