Skip to content

Commit e63040c

Browse files
Refactor authentication routes and add login functionality
1 parent 528fcd8 commit e63040c

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.env
33
.venv
44

5-
firebase
5+
firebase
6+
__pycache__

api/auth/login.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# FILE: auth/login.py
2+
from flask import Blueprint, request, jsonify
3+
from firebase_admin import auth
4+
5+
login_bp = Blueprint('login', __name__)
6+
7+
@login_bp.route('/auth/login', methods=['POST'])
8+
def login():
9+
try:
10+
# Get the email and password from the request
11+
email = request.json.get('email')
12+
password = request.json.get('password')
13+
14+
if not email or not password:
15+
return jsonify({'error': 'Email and password are required'}), 400
16+
17+
# Verify the user using Firebase Authentication
18+
user = auth.get_user_by_email(email)
19+
20+
# Here you would typically verify the password and generate a token
21+
# For simplicity, we assume the password is correct and return user info
22+
23+
return jsonify({
24+
'uid': user.uid,
25+
'email': user.email
26+
}), 200
27+
28+
except Exception as e:
29+
return jsonify({'error': str(e)}), 400

api/auth/signup.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# FILE: auth/signup.py
2+
from flask import Blueprint, request, jsonify
3+
from firebase_admin import auth, firestore
4+
5+
signup_bp = Blueprint('signup', __name__)
6+
7+
db = firestore.client()
8+
9+
@signup_bp.route('/auth/signup', methods=['POST'])
10+
def signup():
11+
try:
12+
# Get the email and password from the request
13+
email = request.json.get('email')
14+
password = request.json.get('password')
15+
16+
if not email or not password:
17+
return jsonify({'error': 'Email and password are required'}), 400
18+
19+
# Create a new user using Firebase Authentication
20+
user = auth.create_user(
21+
email=email,
22+
password=password
23+
)
24+
25+
# Store user data in Firestore
26+
user_ref = db.collection('users').document(user.uid)
27+
user_ref.set({
28+
'email': user.email,
29+
'uid': user.uid,
30+
'created_at': firestore.SERVER_TIMESTAMP
31+
})
32+
33+
# Return success response
34+
return jsonify({
35+
'uid': user.uid,
36+
'email': user.email
37+
}), 201
38+
39+
except Exception as e:
40+
return jsonify({'error': str(e)}), 400

api/main.py

+11-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
2-
from flask import Flask, request, jsonify
2+
from flask import Flask
33
from flask_cors import CORS
44
import firebase_admin
5-
from firebase_admin import credentials, auth, firestore
5+
from firebase_admin import credentials, firestore
66
from dotenv import load_dotenv
77

88
# Load environment variables from the .env file
@@ -33,42 +33,16 @@
3333
# Initialize Firestore
3434
db = firestore.client()
3535

36-
@app.route('/')
37-
def home():
38-
return "Wgg"
39-
40-
@app.route('/auth/signup', methods=['POST'])
41-
def signup():
42-
try:
43-
# Get the email and password from the request
44-
email = request.json.get('email')
45-
password = request.json.get('password')
46-
47-
if not email or not password:
48-
return jsonify({'error': 'Email and password are required'}), 400
49-
50-
# Create a new user using Firebase Authentication
51-
user = auth.create_user(
52-
email=email,
53-
password=password
54-
)
36+
# Import and register blueprints
37+
from auth.signup import signup_bp
38+
from auth.login import login_bp
5539

56-
# Store user data in Firestore
57-
user_ref = db.collection('users').document(user.uid)
58-
user_ref.set({
59-
'email': user.email,
60-
'uid': user.uid,
61-
'created_at': firestore.SERVER_TIMESTAMP
62-
})
40+
app.register_blueprint(signup_bp)
41+
app.register_blueprint(login_bp)
6342

64-
# Return success response
65-
return jsonify({
66-
'uid': user.uid,
67-
'email': user.email
68-
}), 201
69-
70-
except Exception as e:
71-
return jsonify({'error': str(e)}), 400
43+
@app.route('/')
44+
def home():
45+
return "Welcome to the Flask App"
7246

7347
if __name__ == '__main__':
74-
app.run(debug=True, port=3000)
48+
app.run(debug=True, port=3000)

0 commit comments

Comments
 (0)