Skip to content

Instantly share code, notes, and snippets.

@bmax
Created June 9, 2020 14:09
Show Gist options
  • Save bmax/463ca911e5ffc39bee7db4c32d0d0b43 to your computer and use it in GitHub Desktop.
Save bmax/463ca911e5ffc39bee7db4c32d0d0b43 to your computer and use it in GitHub Desktop.
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import * as Services from './services'
import { BookModule } from './books/book.module'
import { SequelizeModule } from '@nestjs/sequelize'
import { UsersModule } from './users/user.module'
import configuration from './config/configuration'
import { AuthModule } from './auth/auth.module'
import { ClubModule } from './clubs/club.module'
const services = Object.keys(Services).map(k => Services[k])
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, load: [configuration] }),
UsersModule,
BookModule,
ClubModule,
AuthModule,
SequelizeModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
dialect: 'postgres',
host: configService.get('database.host'),
port: configService.get('database.port'),
username: configService.get('database.user'),
password: configService.get('database.password'),
database: configService.get('database.db'),
autoLoadModels: true,
synchronize: true,
define: { underscored: true },
}),
inject: [ConfigService],
}),
],
providers: services,
})
export class AppModule {}
import { Module, forwardRef } from '@nestjs/common'
import { SequelizeModule } from '@nestjs/sequelize'
import { User } from './user.model'
import { UsersController } from './user.controller'
import { UserService } from './user.service'
import { AuthModule } from '../auth/auth.module'
import { UserClub } from './user.club.model'
@Module({
imports: [
SequelizeModule.forFeature([User]),
SequelizeModule.forFeature([UserClub]),
forwardRef(() => AuthModule),
],
exports: [UserService],
providers: [UserService],
controllers: [UsersController],
})
export class UsersModule {}
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/sequelize'
import { User, CreateUserDTO } from './user.model'
import { Op } from 'sequelize'
import bcrypt from 'bcrypt'
import { UserClub } from './user.club.model'
import { Club } from '../clubs/club.model'
@Injectable()
export class UserService {
constructor(
@InjectModel(User) private user: typeof User,
@InjectModel(UserClub) private user_club: typeof UserClub
) {}
async login(username: string, password: string) {
const user = await this.user.findOne({
where: {
[Op.or]: [{ email: username }, { username }],
},
attributes: {
include: ['password'],
},
})
if (!user) {
return null
}
const userPassword = user.password
delete user.password
return this.hashCompare(password, userPassword) ? user : null
}
async findById(id: number) {
return this.user.findByPk(id)
}
async create(userToCreate: CreateUserDTO) {
userToCreate.password = this.hashPassword(userToCreate.password)
return this.user.create(userToCreate)
}
async joinClub(user: User, club: Club) {
return this.user_club.create({ user_id: user.id, club: club.id })
}
async getClubs(user: User) {
return user.clubs
}
hashPassword(password: string) {
return bcrypt.hashSync(password, 10)
}
hashCompare(inputPassword: string, userPassword: string) {
return bcrypt.compareSync(inputPassword, userPassword)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment