Skip to content

Commit

Permalink
added create-user functio in flask api.... and unfinished code for lo…
Browse files Browse the repository at this point in the history
…g in
  • Loading branch information
Ben-Sicat committed Dec 9, 2023
1 parent 8d3b9de commit 9d069c7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ WORKDIR /app
COPY requirements.txt /app
RUN pip install -r requirements.txt
COPY app.py /app
CMD python app.py
CMD python app.py
59 changes: 57 additions & 2 deletions backend/app/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from flask import Flask, jsonify
from flask import Flask, jsonify, request
from flask_jwt_extended import JWTManager, create_access_token, jwt_required
import mysql.connector
import bcrypt
import datetime

app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "your-secret-key" # Change this to a secret key
jwt = JWTManager(app)


def get_db_connection():
config = {
Expand Down Expand Up @@ -44,6 +50,20 @@ def Users():
return results
except mysql.connector.Error as err:
print(f"Error: {err}")
def get_user_by_username(username):
connection = get_db_connection()
if connection:
try:
cursor = connection.cursor(dictionary=True)
query = 'SELECT UserID, GoogleID, Username, Password, Email, UName, Birthday, Gender, School FROM Users WHERE Username = %s'
cursor.execute(query, (username,))
result = cursor.fetchone()
cursor.close()
connection.close()
return result
except mysql.connector.Error as err:
print(f"Error: {err}")


def write_to_Reservations(UserID, ReservationDate, ReservationTime, ReservationSite, Status):
connection = get_db_connection()
Expand Down Expand Up @@ -100,6 +120,41 @@ def QR_Codes():
print(f"Error: {err}")

# aight time to do this shit ako naman

def hash_password(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())

def verify_password(hashed_password, password):
return bcrypt.checkpw(password.encode('utf-8'), hashed_password)

@app.route('/api/sign-in', methods=['POST'])
def sign_in():
data = request.get_json()
username = data.get('username')
password = data.get('password')

user_data = get_user_by_username(username)
if user_data and verify_password(user_data.get('Password'), password):
access_token = create_access_token(identity=username, expires_delta=datetime.timedelta(days=1))
return jsonify(access_token=access_token), 200
return jsonify(message='Invalid username or password'), 401

@app.route('/api/create-account', methods=['POST'])
def create_account():
data = request.get_json()
username = data.get('username')
email = data.get('email')
password = data.get('password')

hashed_password = hash_password(password)
write_to_Users('unique_google_id', username, email, 'UName', '2003-03-05', 'Male', 'Sample School')
return jsonify(message='Account created successfully'), 200

@app.route('/test-create-account', methods=['POST'])
def test_create_account():
return jsonify(message='Test route for create account works!')



@app.route('/')
def index():
Expand All @@ -108,4 +163,4 @@ def index():
return jsonify({'User Data': Users()})

if __name__ == '__main__':
app.run(host='0.0.0.0')
app.run(host='0.0.0.0', debug=True)
3 changes: 2 additions & 1 deletion backend/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Flask
mysql-connector-python
Flask-JWT-Extended
Flask-JWT-Extended
bcrypt
7 changes: 7 additions & 0 deletions backend/app/tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@app.route('/test', methods=['GET'])
# def test_route():
# return jsonify(message='Test route works!')

# @app.route('/test-post', methods=['POST'])
# def test_route():
# return jsonify(message='Test route works!')
22 changes: 18 additions & 4 deletions frontend/app/sign_in/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ function Page() {
const handlePasswordChange = (value: string) => {
setPassword(value);
};
const handleLogin = () => {
const handleLogin = async () => {
try{
const response = await fetch('http://localhost:5000/api/sign_in', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
if(response.ok){
const data = await response.json();
console.log("login success")
console.log( "data", data)
console.log("data", data.acces_token)
}
} catch (error) {
console.error(error);

console.log("Logging in...");
console.log("Username:", username);
console.log("Password:", password);
}

};
useEffect(() => {
Expand Down

0 comments on commit 9d069c7

Please sign in to comment.