non-biased-news — 6/6/2022, 6:26:34 PM

Does anyone know how to make it work

↩ repost
non-biased-news — 6/6/2022, 6:22:57 PM

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

♥ 3 ↩ 1 💬 2 comments
♥ 1 ↩ 0 💬 24 comments

comments

quantum-codes:

Yes I can, wait

6/7/2022, 1:15:46 PM
quantum-codes:

Wait.., `json.loads(“users.json“)`?? I think you forgot to open the file lol

6/7/2022, 1:17:07 PM
quantum-codes:

Around Line 35

6/7/2022, 1:17:22 PM
non-biased-news:

So what to do?

6/7/2022, 2:03:32 PM
non-biased-news:

So how to open file

6/7/2022, 2:13:35 PM
quantum-codes:

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

6/7/2022, 2:57:40 PM
non-biased-news:

It isn’t empty

6/7/2022, 2:57:58 PM
quantum-codes:

First change your code as I said

6/7/2022, 2:58:22 PM
non-biased-news:

Okay, it works but then I get

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

6/7/2022, 3:02:10 PM
quantum-codes:

Remove ast.literal_eval line and tell me

6/7/2022, 3:03:27 PM
non-biased-news:

TypeError: hash must be unicode or bytes, not None

6/7/2022, 3:04:06 PM
quantum-codes:

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

userdata = file.read() #str output

if username in userdata: #checks in str for user

userdata = json.loads(userdata) # convert str to dict

userpwdhash = 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

6/7/2022, 3:07:59 PM
non-biased-news:

I got it working, it had to be

userpwdhash = (userdata.get(username)).get('password')

6/7/2022, 3:09:05 PM
quantum-codes:

;)

6/7/2022, 3:09:23 PM
non-biased-news:

:)

6/7/2022, 3:10:02 PM
non-biased-news:

I think it has to do with passlib

6/7/2022, 3:04:29 PM
non-biased-news:

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

6/7/2022, 3:05:39 PM
non-biased-news:

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'}
  
6/7/2022, 3:06:54 PM
quantum-codes:

Ohhh okay, ignore my long comment lol 😆

6/7/2022, 3:08:51 PM
quantum-codes:

Bye I gtg

6/7/2022, 3:09:02 PM
non-biased-news:

Gtg too

Thanks for the help

6/7/2022, 3:09:28 PM
non-biased-news:

Yeah lol

6/7/2022, 3:09:15 PM
trafika:

I would love to help but I have no idea how to code lol

6/6/2022, 6:28:36 PM
non-biased-news:

lol

6/6/2022, 6:28:46 PM