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"; import * as process from "node:process";
export default defineConfig({ export default defineConfig({
schema: './src/drizzle/schema.ts', schema: './src/schema.ts',
out: './drizzle', out: './drizzle',
dialect: 'postgresql', dialect: 'postgresql',
dbCredentials: { dbCredentials: {

View File

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

View File

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