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
Yes I can, wait
Wait.., `json.loads(“users.json“)`?? I think you forgot to open the file lol
Around Line 35
So what to do?
So how to open file
with open("users.json","r") as file: #r is reading modeuserdata = json.loads(file.read()) #outputs strMake 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
with open("users.json","r") as file: #r is reading modeuserdata = file.read() #str outputif username in userdata: #checks in str for useruserdata = json.loads(userdata) # convert str to dictuserpwdhash = userdata.get('password')else:return {'error': 'user not found'} pbkdf2_sha256.verify(password, userpwdhash)return {'ok': 'logged in'}Please try to fix the indentation here. This should work
I got it working, it had to be
userpwdhash = (userdata.get(username)).get('password');)
:)
I think it has to do with passlib
https://codecomm-api--amorogos.repl.co/docs#/
Hey!!! It works
Ohhh okay, ignore my long comment lol 😆
Bye I gtg
Gtg too
Thanks for the help
Yeah lol