https://github.com/aswinbarath/smart-brain-api
The Face Recognition API for Smart-brain react app
https://github.com/aswinbarath/smart-brain-api
bcrypt-nodejs expressjs nodejs postgresql
Last synced: about 2 months ago
JSON representation
The Face Recognition API for Smart-brain react app
- Host: GitHub
- URL: https://github.com/aswinbarath/smart-brain-api
- Owner: AswinBarath
- Created: 2021-03-11T06:28:42.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-07-05T13:45:32.000Z (11 months ago)
- Last Synced: 2025-07-05T15:00:35.656Z (11 months ago)
- Topics: bcrypt-nodejs, expressjs, nodejs, postgresql
- Language: JavaScript
- Homepage: https://smart-brain-api-one.vercel.app
- Size: 12.9 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Smart Brain API
The Face Recognition API for Smart-brain react app
---
## Table of content
- [Smart Brain API](#smart-brain-api)
- [Table of content](#table-of-content)
- [Demo](#demo)
- [Screenshots](#screenshots)
- [Home Page](#home-page)
- [User Registeration Page](#user-registeration-page)
- [User Login Page](#user-login-page)
- [Technologies](#technologies)
- [What's unique in this Project](#whats-unique-in-this-project)
- [Setup Instructions](#setup-instructions)
- [Prerequisites](#prerequisites)
- [Local Development](#local-development)
- [Database Management](#database-management)
- [Deployment to Vercel](#deployment-to-vercel)
- [1. Database Setup](#1-database-setup)
- [2. Vercel Deployment](#2-vercel-deployment)
- [3. Database Migration (Production)](#3-database-migration-production)
- [API Endpoints](#api-endpoints)
- [Contributors](#contributors)
- [Updates Since Postgres to Prisma Migration](#updates-since-postgres-to-prisma-migration)
- [Smart Brain API](#smart-brain-api-1)
- [๐ Features](#-features)
- [๐ Table of Contents](#-table-of-contents)
- [๐ Tech Stack](#-tech-stack)
- [๐ Database Schema](#-database-schema)
- [User Model](#user-model)
- [Login Model](#login-model)
- [๐ API Endpoints](#-api-endpoints)
- [๐ง Environment Variables](#-environment-variables)
- [๐ฆ Installation](#-installation)
- [๐ Database Migration History](#-database-migration-history)
- [Migration from Knex to Prisma](#migration-from-knex-to-prisma)
- [Key Changes Made:](#key-changes-made)
- [Migration Files:](#migration-files)
- [๐ Recent Updates \& Fixes](#-recent-updates--fixes)
- [1. CORS Configuration Fix](#1-cors-configuration-fix)
- [2. Registration Logic Fix](#2-registration-logic-fix)
- [3. Environment Variable Management](#3-environment-variable-management)
- [4. Enhanced Error Logging](#4-enhanced-error-logging)
- [5. Database Connection Testing](#5-database-connection-testing)
- [๐ Deployment](#-deployment)
- [Vercel Deployment](#vercel-deployment)
- [Build Configuration](#build-configuration)
- [๐งช Testing](#-testing)
- [Local Testing](#local-testing)
- [Test Script](#test-script)
- [๐ Project Structure](#-project-structure)
- [๐ Security Features](#-security-features)
- [๐ค Contributing](#-contributing)
- [๐ License](#-license)
- [๐ Troubleshooting](#-troubleshooting)
- [Common Issues](#common-issues)
- [Debug Endpoints](#debug-endpoints)
---
## Demo
---
## Screenshots
### Home Page
### User Registeration Page
### User Login Page
---
## Technologies

ย

ย

ย

ย

ย

ย
---
## What's unique in this Project
- An Image Recognition app which makes an API request to face recognition machine learning model
- The App has built-in Authentication which takes care of the user login credentials securely through hashing
- User Ranking is provided each time the Smart Brain Web App service is triggered for facial detection of images
- The web app employs custom REST API on the backend with Prisma ORM and PostgreSQL for user data and hashed passwords
---
## Setup Instructions
### Prerequisites
- Node.js (v14 or higher)
- PostgreSQL database
- Clarifai API key
### Local Development
1. **Clone the repository**
```bash
git clone
cd Smart-brain-api
```
2. **Install dependencies**
```bash
npm install
```
3. **Set up environment variables**
```bash
cp env.example .env
```
Edit `.env` with your actual values:
- `DATABASE_URL`: Your PostgreSQL connection string
- `CLARIFAI_API`: Your Clarifai API key
4. **Set up the database**
```bash
# Generate Prisma client
npx prisma generate
# Push schema to database (for development)
npx prisma db push
# Or run migrations (for production)
npx prisma migrate dev
```
5. **Start the development server**
```bash
npm run start:dev
```
### Database Management
- **View database in Prisma Studio:**
```bash
npx prisma studio
```
- **Reset database:**
```bash
npx prisma migrate reset
```
---
## Deployment to Vercel
### 1. Database Setup
- Create a PostgreSQL database (recommended: [Neon](https://neon.tech) or [Supabase](https://supabase.com))
- Get your database connection string
### 2. Vercel Deployment
1. **Install Vercel CLI**
```bash
npm i -g vercel
```
2. **Deploy to Vercel**
```bash
vercel
```
3. **Set Environment Variables in Vercel Dashboard**
- Go to your project in Vercel dashboard
- Navigate to Settings โ Environment Variables
- Add:
- `DATABASE_URL`: Your PostgreSQL connection string
- `CLARIFAI_API`: Your Clarifai API key
4. **Deploy with Database**
```bash
# Generate Prisma client for production
npx prisma generate
# Push schema to production database
npx prisma db push --accept-data-loss
# Deploy
vercel --prod
```
### 3. Database Migration (Production)
```bash
# Run migrations on production database
npx prisma migrate deploy
```
---
## API Endpoints
- `POST /register` - Register a new user
- `POST /signin` - Sign in existing user
- `GET /profile/:id` - Get user profile
- `PUT /image` - Update user entries count
- `POST /imageurl` - Process image with Clarifai
---
## Contributors
- T Aswin Barath
---
## Updates Since Postgres to Prisma Migration
# Smart Brain API
A facial recognition web application backend built with Node.js, Express, and Prisma ORM. This API supports user registration, authentication, and image processing using the Clarifai API.
## ๐ Features
- **User Authentication**: Secure registration and login with bcrypt password hashing
- **Facial Recognition**: Image processing using Clarifai API
- **Database Management**: PostgreSQL with Prisma ORM for type-safe database operations
- **CORS Support**: Configured for cross-origin requests from frontend applications
- **Production Ready**: Deployed on Vercel with environment variable management
## ๐ Table of Contents
- [Tech Stack](#tech-stack)
- [Database Schema](#database-schema)
- [API Endpoints](#api-endpoints)
- [Environment Variables](#environment-variables)
- [Installation](#installation)
- [Database Migration History](#database-migration-history)
- [Recent Updates & Fixes](#recent-updates--fixes)
- [Deployment](#deployment)
- [Testing](#testing)
## ๐ Tech Stack
- **Backend**: Node.js, Express.js
- **Database**: PostgreSQL with Prisma ORM
- **Authentication**: bcrypt-nodejs
- **Image Processing**: Clarifai API
- **Deployment**: Vercel
- **CORS**: Express CORS middleware
## ๐ Database Schema
### User Model
```prisma
model User {
id Int @id @default(autoincrement())
name String
email String @unique
joined DateTime @default(now())
entries Int @default(0)
@@map("users")
}
```
### Login Model
```prisma
model Login {
id Int @id @default(autoincrement())
hash String
email String @unique
@@map("login")
}
```
**Security Note**: Passwords are stored as hashed values in the `login` table, not in plain text in the `users` table. This follows security best practices.
## ๐ API Endpoints
| Method | Endpoint | Description | Request Body |
|--------|----------|-------------|--------------|
| GET | `/` | Health check | - |
| GET | `/test-db` | Database connection test | - |
| POST | `/register` | User registration | `{email, password, name}` |
| POST | `/signin` | User authentication | `{email, password}` |
| GET | `/profile/:id` | Get user profile | - |
| PUT | `/image` | Update user entries count | `{id}` |
| POST | `/imageurl` | Process image with Clarifai | `{input}` |
## ๐ง Environment Variables
Create a `.env` file with the following variables:
```env
# Database
DATABASE_URL="postgresql://username:password@host:port/database"
# Clarifai API
CLARIFAI_API="your_clarifai_api_key_here"
# Server
PORT=3000
```
## ๐ฆ Installation
1. **Clone the repository**
```bash
git clone
cd Smart-brain-api
```
2. **Install dependencies**
```bash
npm install
```
3. **Set up environment variables**
```bash
cp env.example .env
# Edit .env with your actual values
```
4. **Generate Prisma client**
```bash
npx prisma generate
```
5. **Run database migrations**
```bash
npx prisma migrate deploy
```
6. **Start the server**
```bash
npm start
```
## ๐ Database Migration History
### Migration from Knex to Prisma
This project was migrated from Knex.js to Prisma ORM for better type safety and developer experience.
#### Key Changes Made:
1. **Schema Definition**: Converted from Knex migrations to Prisma schema
2. **Database Client**: Replaced Knex with Prisma Client
3. **Query Syntax**: Updated all database queries to use Prisma syntax
4. **Type Safety**: Added TypeScript-like type safety with Prisma
#### Migration Files:
- `prisma/migrations/001_initial/migration.sql` - Initial schema creation
## ๐ Recent Updates & Fixes
### 1. CORS Configuration Fix
**Issue**: Frontend application couldn't connect to backend due to CORS policy restrictions.
**Solution**: Updated CORS configuration in `server.js`:
```javascript
app.use(cors({
origin: ['https://smart-brain-app-six.vercel.app', 'http://localhost:3000'],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
```
### 2. Registration Logic Fix
**Issue**: Foreign key constraint violation when creating user records.
**Problem**: The original code tried to create `login` record before `user` record, violating the foreign key constraint.
**Solution**: Updated registration order in `controllers/register.js`:
```javascript
// Create user first, then login to satisfy FK constraint
const result = await prisma.$transaction(async (tx) => {
// 1. Create user record first
const user = await tx.user.create({
data: { email, name, joined: new Date() }
});
// 2. Then create login record
const login = await tx.login.create({
data: { hash, email }
});
return user;
});
```
### 3. Environment Variable Management
**Issue**: Vercel deployment failed due to missing environment variables.
**Solution**:
- Removed secret references from `vercel.json`
- Set up environment variables directly in Vercel dashboard
- Updated build script to include database migrations
### 4. Enhanced Error Logging
**Improvement**: Added detailed error logging for better debugging:
```javascript
console.error('Registration error details:', {
message: err.message,
code: err.code,
meta: err.meta,
stack: err.stack
});
```
### 5. Database Connection Testing
**Addition**: Created `/test-db` endpoint for database connectivity testing:
```javascript
app.get('/test-db', async (req, res) => {
try {
await prisma.$queryRaw`SELECT 1`;
res.json({ status: 'Database connection successful' });
} catch (error) {
res.status(500).json({
status: 'Database connection failed',
error: error.message
});
}
});
```
## ๐ Deployment
### Vercel Deployment
1. **Connect to Git repository**
2. **Set environment variables** in Vercel dashboard:
- `DATABASE_URL`
- `CLARIFAI_API`
3. **Deploy automatically** on git push
### Build Configuration
```json
{
"build": "prisma generate && prisma migrate deploy",
"postinstall": "prisma generate"
}
```
## ๐งช Testing
### Local Testing
1. Start the server: `npm start`
2. Test database connection: `GET http://localhost:3000/test-db`
3. Test registration: Use the provided `test-register.js` script
### Test Script
```bash
node test-register.js
```
## ๐ Project Structure
```
Smart-brain-api/
โโโ controllers/ # Route handlers
โ โโโ register.js # User registration
โ โโโ signin.js # User authentication
โ โโโ profile.js # User profile management
โ โโโ image.js # Image processing
โโโ lib/
โ โโโ prisma.js # Prisma client configuration
โโโ prisma/
โ โโโ schema.prisma # Database schema
โ โโโ migrations/ # Database migrations
โโโ server.js # Express server setup
โโโ package.json # Dependencies and scripts
โโโ vercel.json # Vercel deployment config
```
## ๐ Security Features
- **Password Hashing**: bcrypt for secure password storage
- **CORS Protection**: Configured for specific origins
- **Input Validation**: Server-side validation for all endpoints
- **Database Constraints**: Foreign key constraints for data integrity
## ๐ค Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Test thoroughly
5. Submit a pull request
## ๐ License
This project is licensed under the ISC License.
## ๐ Troubleshooting
### Common Issues
1. **CORS Errors**: Ensure frontend domain is in CORS configuration
2. **Database Connection**: Check `DATABASE_URL` environment variable
3. **Registration Failures**: Verify database schema and foreign key constraints
4. **Migration Issues**: Run `npx prisma migrate deploy` to apply migrations
### Debug Endpoints
- `GET /test-db` - Test database connectivity
- Check server logs for detailed error messages
---
**Last Updated**: June 2025
**Version**: 2.0.0 (PostgreSQL + Prisma Migration)
---
**[โฌ Back to Top](#Smart-Brain-App)**