Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const bcrypt = require('bcrypt');
const { db, initDb } = require('./database');
const { GoogleGenAI } = require('@google/genai');
const path = require('path');
Expand Down Expand Up @@ -555,7 +556,7 @@ Text: "${text}"
// ================= AUTH =================

// SIGNUP
app.post('/api/auth/signup', (req, res) => {
app.post('/api/auth/signup', async(req, res) => {
const { email, password } = req.body;

if (!email || !password) {
Expand All @@ -566,10 +567,11 @@ app.post('/api/auth/signup', (req, res) => {

const id = 'user_' + Date.now();

const hashedPassword = await bcrypt.hash(password, 10);
db.run(
`INSERT INTO users (id, email, password)
VALUES (?, ?, ?)`,
[id, email, password],
[id, email, hashedPassword],
function(err) {

if (err) {
Expand All @@ -594,7 +596,7 @@ app.post('/api/auth/signup', (req, res) => {
});

// LOGIN
app.post('/api/auth/login', (req, res) => {
app.post('/api/auth/login',async (req, res) => {
const { email, password } = req.body;

if (!email || !password) {
Expand All @@ -606,20 +608,29 @@ app.post('/api/auth/login', (req, res) => {
db.get(
`SELECT * FROM users WHERE email = ?`,
[email],
(err, user) => {
async (err, user) => {

if (err) {
return res.status(500).json({
error: err.message
});
}

if (!user || user.password !== password) {
if (!user) {
return res.status(401).json({
error: 'Invalid email or password'
});
}
const isValid = await bcrypt.compare(
password,
user.password
);

if (!isValid) {
return res.status(401).json({
error: 'Invalid email or password'
});
}
res.json({
success: true,
email: user.email
Expand Down