Exemplo n.º 1
0
    def create_session(self):
        """ Attempts to login and authenticate. """
        DB = GardensDB()
        body = self.decode()
        reqEmail = body['email']
        reqPassword = body['password']

        # check password
        user = DB.get_user(reqEmail)
        if user != None:
            if bcrypt.verify(reqPassword, user['password']):
                self.session_data['uid'] = user['id']
                self.response(201, True)
            else:
                self.no_auth(401)
        else:
            self.no_auth(401)
Exemplo n.º 2
0
    def add_user(self):
        """ Creates a new user with a unique email. """
        DB = GardensDB()
        body = self.decode()
        first_name = body['first_name']
        last_name = body['last_name']
        email = body['email']
        password = body['password']

        # check if email is duplicate
        user = DB.get_user(email)
        if user != None:
            self.response(422, True)
            self.wfile.write(bytes(json.dumps({'message': "No duplicate email"}), "utf-8"))
            return

        # hash password
        hashed = bcrypt.hash(password)
        DB.create_user(first_name, last_name, email, hashed)
        self.response(201, True)