-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_helpers.py
81 lines (70 loc) · 2.3 KB
/
auth_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# author: Zulqar Nain
# date: 12-june-2024
# project SkillQues
############################################################################
import sqlite3
import hashlib
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
except sqlite3.Error as e:
print(e)
return conn
def create_table(conn):
sql_create_users_table = """CREATE TABLE IF NOT EXISTS users (
id integer PRIMARY KEY,
username text NOT NULL,
password text NOT NULL
);"""
try:
c = conn.cursor()
c.execute(sql_create_users_table)
except sqlite3.Error as e:
print(e)
def create_user(conn, username, password):
sql = '''INSERT INTO users(username, password) VALUES(?, ?)'''
cur = conn.cursor()
cur.execute(sql, (username, password))
conn.commit()
return cur.lastrowid
# def activate_user(conn, username):
# """
# Update the is_active status of a user to 0 based on the username.
# :param conn: Connection object
# :param username: username of the user
# :return:
# """
# sql = '''UPDATE users SET is_active = 1 WHERE username = ?'''
# try:
# cur = conn.cursor()
# cur.execute(sql, (username,))
# conn.commit()
# print(f"User with username {username} deactivated.")
# except sqlite3.Error as e:
# print(e)
# def is_user_active(conn, username):
# """
# Check if a user is active based on the username.
# :param conn: Connection object
# :param username: username of the user
# :return: True if user is active, else False
# """
# sql = '''SELECT is_active FROM users WHERE username = ?'''
# try:
# cur = conn.cursor()
# cur.execute(sql, (username,))
# result = cur.fetchone()
# if result and result[0] == 1:
# return True
# else:
# return False
# except sqlite3.Error as e:
# print(e)
# return False
def authenticate_user(conn, username, password):
sql = '''SELECT * FROM users WHERE username=? AND password=?'''
cur = conn.cursor()
cur.execute(sql, (username, password))
rows = cur.fetchall()
return len(rows) > 0