Skip to content

Commit

Permalink
gg
Browse files Browse the repository at this point in the history
  • Loading branch information
upayanmazumder committed Nov 28, 2024
1 parent 93e5dd5 commit bad066d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
22 changes: 11 additions & 11 deletions api/auth/login.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# FILE: auth/login.py
from flask import Blueprint, request, jsonify
from firebase_admin import auth

Expand All @@ -7,23 +6,24 @@
@login_bp.route('/auth/login', methods=['POST'])
def login():
try:
# Get the email and password from the request
# Get the email from the request (no password needed for Firebase login)
email = request.json.get('email')
password = request.json.get('password')

if not email or not password:
return jsonify({'error': 'Email and password are required'}), 400
if not email:
return jsonify({'error': 'Email is required'}), 400

# Verify the user using Firebase Authentication
# Verify if the user exists in Firebase
user = auth.get_user_by_email(email)

# Here you would typically verify the password and generate a token
# For simplicity, we assume the password is correct and return user info
# Generate a custom token for the user
custom_token = auth.create_custom_token(user.uid)

return jsonify({
'uid': user.uid,
'email': user.email
'customToken': custom_token.decode("utf-8"), # Decode the custom token to a string
}), 200

except auth.UserNotFoundError:
return jsonify({'error': 'User not found'}), 404

except Exception as e:
return jsonify({'error': str(e)}), 400
return jsonify({'error': str(e)}), 400
80 changes: 36 additions & 44 deletions app/src/app/auth/login/page.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,80 @@
"use client"
"use client";

import React, { useState } from 'react';
import axios from 'axios';
import React, { useState } from "react";
import axios from "axios";
import { getAuth, signInWithCustomToken } from "firebase/auth";

const Login = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [message, setMessage] = useState('');
const [error, setError] = useState('');
const [email, setEmail] = useState("");
const [message, setMessage] = useState("");
const [error, setError] = useState("");

const handleLogin = async (e) => {
e.preventDefault();

try {
const response = await axios.post('https://api.cas.upayan.dev/auth/login', {
// Send login request to the backend
const response = await axios.post("https://api.cas.upayan.dev/auth/login", {
email,
password,
});

setMessage(`Login successful! Welcome, ${response.data.email}`);
setError(''); // Clear any previous errors
setEmail('');
setPassword('');
const { customToken } = response.data;

// Sign in with the custom token using Firebase
const auth = getAuth();
const userCredential = await signInWithCustomToken(auth, customToken);

setMessage(`Login successful! Welcome, ${userCredential.user.email}`);
setError(""); // Clear any previous errors
setEmail("");
} catch (err) {
setMessage('');
setError(err.response?.data?.error || 'An error occurred during login.');
setMessage("");
setError(
err.response?.data?.error || "An error occurred during login."
);
}
};

return (
<div style={{ maxWidth: '400px', margin: '50px auto', textAlign: 'center' }}>
<div style={{ maxWidth: "400px", margin: "50px auto", textAlign: "center" }}>
<h2>Login</h2>
<form onSubmit={handleLogin}>
<div style={{ marginBottom: '15px' }}>
<div style={{ marginBottom: "15px" }}>
<input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
style={{
padding: '10px',
width: '100%',
borderRadius: '5px',
border: '1px solid #ccc',
}}
required
/>
</div>
<div style={{ marginBottom: '15px' }}>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
style={{
padding: '10px',
width: '100%',
borderRadius: '5px',
border: '1px solid #ccc',
padding: "10px",
width: "100%",
borderRadius: "5px",
border: "1px solid #ccc",
}}
required
/>
</div>
<button
type="submit"
style={{
padding: '10px 20px',
backgroundColor: '#28a745',
color: '#fff',
border: 'none',
borderRadius: '5px',
cursor: 'pointer',
padding: "10px 20px",
backgroundColor: "#28a745",
color: "#fff",
border: "none",
borderRadius: "5px",
cursor: "pointer",
}}
>
Login
</button>
</form>
{message && (
<div style={{ marginTop: '20px', color: 'green' }}>
<div style={{ marginTop: "20px", color: "green" }}>
<strong>{message}</strong>
</div>
)}
{error && (
<div style={{ marginTop: '20px', color: 'red' }}>
<div style={{ marginTop: "20px", color: "red" }}>
<strong>{error}</strong>
</div>
)}
Expand Down

0 comments on commit bad066d

Please sign in to comment.