Skip to content

Commit

Permalink
Feat: signup/signin, wallet address collection and storage (#4)
Browse files Browse the repository at this point in the history
* Feat: signup/signin, wallet address collection and storage

* Feat: restructure entity and made corresponding changes to controller, validation, and services

* feat: add check api that checks if wallet address already signed up
  • Loading branch information
GideonBature authored Jan 26, 2025
1 parent dd3cb32 commit b91453b
Show file tree
Hide file tree
Showing 25 changed files with 2,109 additions and 90 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=skillnet
JWT_SECRET=your_jwt_secret_key
PORT=5000
19 changes: 19 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DataSource } from 'typeorm';
import User from '../entities/user.entity.js';

const AppDataSource = new DataSource({
type: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5432'),
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: true,
dropSchema: false,
logging: false,
entities: [User],
subscribers: [],
migrations: [],
});

export default AppDataSource;
72 changes: 72 additions & 0 deletions controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import AuthService from '../services/auth.service.js';
import validateRegistration from '../validation/auth.validation.js';

class AuthController {
constructor() {
this.authService = new AuthService();
}

async checkWallet(req, res) {
try {
const { walletAddress } = req.params;
const exists = await this.authService.walletExists(walletAddress);
res.json({ exists });
} catch (error) {
this.handleError(res, error);
}
}

async signup(req, res) {
try {
const { error } = validateRegistration(req.body);
if (error) return res.status(400).json({ error: error.details[0].message });

const { user, token } = await this.authService.register(req.body);

res.status(201).json({
success: true,
user: {
id: user.id,
role: user.role,
walletAddress: user.walletAddress
},
token
});
} catch (error) {
this.handleError(res, error);
}
}

async signin(req, res) {
try {
const { walletAddress } = req.body;
if (!walletAddress) return res.status(400).json({ error: 'Wallet address required' });

const { user, token } = await this.authService.login(walletAddress);

res.json({
success: true,
user: {
id: user.id,
role: user.role,
walletAddress: user.walletAddress
},
token
});
} catch (error) {
this.handleError(res, error);
}
}

handleError(res, error) {
console.error('Auth Error:', error);
const statusCode = error.message.includes('not found') ? 404 :
error.message.includes('conflict') ? 409 : 400;
res.status(statusCode).json({
success: false,
error: error.message
});
}
}

export default AuthController;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions entities/user.entity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { EntitySchema } from 'typeorm';

const UserEntity = new EntitySchema({
name: 'User',
tableName: 'users',
columns: {
id: {
primary: true,
type: 'int',
generated: true,
},
walletAddress: {
type: 'varchar',
unique: true,
},
role: {
type: 'varchar',
length: 20,
},
// Generalizes to job seeker/employer/institution name
name: {
type: 'varchar',
length: 100,
},
email: {
type: 'varchar',
unique: true,
},
// Tutor-specific (nullable)
title: {
type: 'varchar',
length: 100,
nullable: true,
},
// Used as bio (Job Seeker, Tutor, Institution)/description (Company)
bio: {
type: 'text',
nullable: true,
},
// Tutor-specific (nullable)
skills: {
type: 'simple-array',
nullable: true,
},
createdAt: {
type: 'timestamp',
createDate: true,
},
},
indices: [
{ columns: ['walletAddress'] },
{ columns: ['email'] },
{ columns: ['role'] }, // Index role if querying by it frequently
],
});

export default UserEntity;
File renamed without changes.
Empty file removed models/Certificate.js
Empty file.
Empty file removed models/Course.js
Empty file.
Empty file removed models/Exam.js
Empty file.
Empty file removed models/Job.js
Empty file.
1 change: 0 additions & 1 deletion models/Transaction.js

This file was deleted.

Empty file removed models/User.js
Empty file.
Loading

0 comments on commit b91453b

Please sign in to comment.