login-auth-services
Version:
Authentication services for Google, GitHub, Microsoft, okta and multi-factor authentication using OTP.
47 lines (37 loc) • 1.41 kB
text/typescript
import { AppDataSource } from "../config/database.config";
import { Repository } from "typeorm";
import { User } from "../entities/User";
export class UserRepository {
private repository: Repository<User>;
constructor() {
this.repository = AppDataSource.getRepository(User);
}
// Create a new user
async createUser(username: string, email: string, password: string): Promise<User> {
const user = this.repository.create({ username, email, password });
return await this.repository.save(user);
}
// Find a user by ID
async findById(id: string | number): Promise<User | null> {
return await this.repository.findOne({ where: { id } });
}
// Find a user by email
async findByEmail(email: string): Promise<User | null> {
return await this.repository.findOne({ where: { email } });
}
// Update a user's details
async updateUser(id: string | number, updateData: Partial<User>): Promise<User | null> {
await this.repository.update(id, updateData);
return this.findById(id);
}
// Delete a user
async deleteUser(id: string | number): Promise<boolean> {
const result = await this.repository.delete(id);
return result.affected !== 0;
}
// Get all users
async getAllUsers(): Promise<User[]> {
return await this.repository.find();
}
}
export const userRepository = new UserRepository();