feat(auth): add Auth service and module

A new authentication service and module were added to the application. The app.module.ts file was updated to include this newly created AuthModule. Also, an initial structure for the AuthService was set up.
This commit is contained in:
Mathis H (Avnyr) 2024-07-09 15:01:36 +02:00
parent 3c57098bbe
commit bb482fb896
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
3 changed files with 44 additions and 48 deletions

View File

@ -2,7 +2,7 @@ import { defineConfig } from 'drizzle-kit';
import * as process from "node:process";
export default defineConfig({
schema: './src/drizzle/schema.ts',
schema: './src/schema.ts',
out: './drizzle',
dialect: 'postgresql',
dbCredentials: {

View File

@ -1,10 +1,10 @@
import { Module } from '@nestjs/common';
import { DrizzleService } from './drizzle.service';
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { DrizzleService } from "./drizzle.service";
@Module({
imports: [ConfigModule],
providers: [DrizzleService],
exports: [DrizzleService]
imports: [ConfigModule],
providers: [DrizzleService],
exports: [DrizzleService],
})
export class DrizzleModule {}

View File

@ -1,56 +1,52 @@
// biome-ignore lint/style/useImportType: used by Next.js
import { Injectable, OnModuleInit, OnModuleDestroy } from "@nestjs/common";
import { Injectable, OnModuleDestroy, OnModuleInit } from "@nestjs/common";
// biome-ignore lint/style/useImportType: used by Next.js
import { ConfigService } from "@nestjs/config";
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
@Injectable()
export class DrizzleService implements OnModuleInit, OnModuleDestroy {
private migrationClient: postgres.Sql<Record<string, postgres.PostgresType>>;
private standardClient: postgres.Sql<Record<string, postgres.PostgresType>>;
private migrationClient: postgres.Sql<Record<string, postgres.PostgresType>>;
private standardClient: postgres.Sql<Record<string, postgres.PostgresType>>;
constructor(
private configService: ConfigService
) { }
constructor(private configService: ConfigService) {}
async onModuleInit() {
async onModuleInit() {
const dbConfig = {
host: this.configService.get<string>("POSTGRES_HOST"),
port: Number(this.configService.get<string>("POSTGRES_PORT")),
database: this.configService.get<string>("POSTGRES_DATABASE"),
user: this.configService.get<string>("POSTGRES_USER"),
password: this.configService.get<string>("POSTGRES_PASSWORD"),
ssl: Boolean(this.configService.get<string>("POSTGRES_SSL")),
};
const dbConfig = {
host: this.configService.get<string>("POSTGRES_HOST"),
port: Number(this.configService.get<string>("POSTGRES_PORT")),
database: this.configService.get<string>("POSTGRES_DATABASE"),
user: this.configService.get<string>("POSTGRES_USER"),
password: this.configService.get<string>("POSTGRES_PASSWORD"),
ssl: Boolean(this.configService.get<string>("POSTGRES_SSL")),
}
this.migrationClient = postgres({
...dbConfig,
max: 1,
});
this.migrationClient = postgres({
...dbConfig,
max: 1,
})
this.standardClient = postgres({
...dbConfig,
});
}
this.standardClient = postgres({
...dbConfig
})
}
//onModuleDestroy
async onModuleDestroy() {
await this.migrationClient.end();
await this.standardClient.end();
}
//onModuleDestroy
async onModuleDestroy() {
await this.migrationClient.end();
await this.standardClient.end();
}
getMigrationClient() {
return migrate(drizzle(this.migrationClient), {
migrationsFolder: "./migrations",
migrationsSchema: "public",
});
}
getMigrationClient() {
return migrate(drizzle(this.migrationClient), {
migrationsFolder: "./migrations",
migrationsSchema: "public"
});
}
getClient() {
return drizzle(this.standardClient);
}
}
use() {
return drizzle(this.standardClient);
}
}