def get(self, *args): username, password, firstname, lastname = getQueryParametes(self, ['username', 'password', 'firstname', 'lastname']) if(not username or not password): self.write({"message:": "username and password are required"}) return if(not firstname): firstname = "" if(not lastname): lastname = "" # check if user already exists in the saved users userExists = mydb.doesThisUserAlreadyExist(username) if not userExists: mydb.createUserInUsersTable(username, password, firstname, lastname) self.write({ "message": "Signed Up Successfully", "code": "200" }) else: self.write( { "message": "user already exists", "code": "400" } )
def getUserLoginToken(username): user = mydb.doesThisUserAlreadyExist(username) if(not user): print("no such user exists for login") else: if(not user["token"] or user["token"] == ""): user['token'] = generateRandomToken() mydb.saveTokenToThisUser(username, user['token']) return user['token']
def get(self, *args): # get username and password that the request contains username, password = getQueryParametes(self, ['username', 'password']) # if username or password are not in the request if(not username or not password): self.write({ "message": "username and password are required.", "code": "400" }) return # find the user if exist user = mydb.doesThisUserAlreadyExist(username) # if user doesnt exist, tell it to response if not user: self.write({ "message": "user doesn't exist in the database.", "code": "400" }) return print("user", user) # if user exist and the password is correct if (user) and user["password"] == password: # the below function, saves the token in the user data token = getUserLoginToken(username=username) self.write({ "message": "Logged in Successfully", "code": "200", "token": token }) else: # if the password is not correct self.write({ "message": "username or password is not correct", "code": "400" })