feat(auth): add user update and delete methods in auth service
This commit adds methods for updating and deleting users in the auth service. Additionally, it includes corresponding validation schemas in the auth schema file. The auth controller code has also been cleaned up for better readability.
This commit is contained in:
parent
6fb474172a
commit
f681dd77bd
@ -2,23 +2,20 @@ import { Body, Controller, HttpCode, HttpStatus, Post } from "@nestjs/common";
|
|||||||
import { SignUpDto } from "src/auth/auth.dto";
|
import { SignUpDto } from "src/auth/auth.dto";
|
||||||
import { AuthService } from "src/auth/auth.service";
|
import { AuthService } from "src/auth/auth.service";
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller("auth")
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) {
|
constructor(private readonly authService: AuthService) {}
|
||||||
}
|
|
||||||
|
|
||||||
//POST signup
|
//POST signup
|
||||||
@HttpCode(HttpStatus.CREATED)
|
@HttpCode(HttpStatus.CREATED)
|
||||||
@Post("signup")
|
@Post("signup")
|
||||||
async signUp(@Body() dto: SignUpDto) {
|
async signUp(@Body() dto: SignUpDto) {
|
||||||
console.log(dto)
|
console.log(dto);
|
||||||
return this.authService.doRegister(dto)
|
return this.authService.doRegister(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//POST signin
|
||||||
|
//GET me -- Get current user data via jwt
|
||||||
//POST signin
|
//DELETE me
|
||||||
//GET me -- Get current user data via jwt
|
//PATCH me
|
||||||
//DELETE me
|
|
||||||
//PATCH me
|
|
||||||
}
|
}
|
||||||
|
@ -47,4 +47,4 @@ export class SignInDto {
|
|||||||
minSymbols: 1,
|
minSymbols: 1,
|
||||||
})
|
})
|
||||||
password: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
@ -29,3 +29,9 @@ export const SignInBodySchema = z.object({
|
|||||||
.min(6)
|
.min(6)
|
||||||
.max(32),
|
.max(32),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const UserUpdateSchema = z.object({
|
||||||
|
firstName: z.string({ message: "'firstName' should be a string." }).max(24),
|
||||||
|
|
||||||
|
lastName: z.string({ message: "'lastName' should be a string." }).max(24),
|
||||||
|
});
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import * as console from "node:console";
|
|
||||||
import {
|
import {
|
||||||
Injectable,
|
Injectable,
|
||||||
OnModuleInit,
|
OnModuleInit,
|
||||||
UnauthorizedException,
|
UnauthorizedException,
|
||||||
} from "@nestjs/common";
|
} from "@nestjs/common";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { SignUpBodySchema } from "src/auth/auth.schema";
|
import { SignInDto, SignUpDto } from "src/auth/auth.dto";
|
||||||
|
import { UserUpdateSchema } from "src/auth/auth.schema";
|
||||||
import { CredentialsService } from "src/credentials/credentials.service";
|
import { CredentialsService } from "src/credentials/credentials.service";
|
||||||
import { DrizzleService } from "src/drizzle/drizzle.service";
|
import { DrizzleService } from "src/drizzle/drizzle.service";
|
||||||
import { UsersTable } from "src/schema";
|
import { UsersTable } from "src/schema";
|
||||||
import { SignInDto, SignUpDto } from "src/auth/auth.dto";
|
import { IUserUpdateData } from "src/auth/auth.types";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService implements OnModuleInit {
|
export class AuthService implements OnModuleInit {
|
||||||
@ -49,32 +49,91 @@ export class AuthService implements OnModuleInit {
|
|||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
message: "User created, check your email for validation.",
|
message: "User created, check your email for validation.",
|
||||||
token: await this.credentials.signAuthToken({sub: query[0].uuid})
|
token: await this.credentials.signAuthToken({ sub: query[0].uuid }),
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
doLogin(data: SignInDto) {}
|
async doLogin(data: SignInDto) {
|
||||||
|
const user = await this.db
|
||||||
|
.use()
|
||||||
|
.select()
|
||||||
|
.from(UsersTable)
|
||||||
|
.where(eq(UsersTable.email, data.email))
|
||||||
|
.prepare("userByEmail")
|
||||||
|
.execute();
|
||||||
|
if (user.length !== 1)
|
||||||
|
throw new UnauthorizedException("Invalid credentials");
|
||||||
|
const passwordMatch = await this.credentials.check(
|
||||||
|
data.password,
|
||||||
|
user[0].hash,
|
||||||
|
);
|
||||||
|
if (!passwordMatch) throw new UnauthorizedException("Invalid credentials");
|
||||||
|
const token = await this.credentials.signAuthToken({ sub: user[0].uuid });
|
||||||
|
return {
|
||||||
|
message: "Login successful",
|
||||||
|
token: token,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async fetchUser(userId: string) {
|
async fetchUser(userId: string) {
|
||||||
//TODO Pagination
|
//TODO Pagination
|
||||||
const usersInDb = await this.db.use().select().from(UsersTable);
|
const usersInDb = await this.db.use().select().from(UsersTable);
|
||||||
const result = {
|
const result = {
|
||||||
total: usersInDb.length,
|
total: usersInDb.length,
|
||||||
users: usersInDb.map((user)=>{
|
users: usersInDb.map((user) => {
|
||||||
delete user.hash
|
delete user.hash;
|
||||||
return {
|
return {
|
||||||
...user
|
...user,
|
||||||
}
|
};
|
||||||
})
|
}),
|
||||||
}
|
};
|
||||||
console.log(result)
|
console.log(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateUser(targetId: string, userData: IUserUpdateData) {
|
||||||
|
const validationResult = UserUpdateSchema.safeParse(userData);
|
||||||
|
if (!validationResult.success) {
|
||||||
|
throw new UnauthorizedException(validationResult.error);
|
||||||
|
}
|
||||||
|
const updateQuery = await this.db
|
||||||
|
.use()
|
||||||
|
.update(UsersTable)
|
||||||
|
.set({
|
||||||
|
...userData
|
||||||
|
})
|
||||||
|
.where(eq(UsersTable.uuid, targetId))
|
||||||
|
.prepare("updateUserById")
|
||||||
|
.execute()
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
throw new UnauthorizedException(
|
||||||
|
"Error occurred while updating user",
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteUser(targetId: string) {
|
||||||
|
await this.db
|
||||||
|
.use()
|
||||||
|
.delete(UsersTable)
|
||||||
|
.where(eq(UsersTable.uuid, targetId))
|
||||||
|
.prepare("deleteUserById")
|
||||||
|
.execute()
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
throw new UnauthorizedException(
|
||||||
|
"Error occurred while deleting user",
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
updateUser() {}
|
|
||||||
deleteUser() {}
|
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
setTimeout(()=>{
|
setTimeout(() => {
|
||||||
this.fetchUser("ee");
|
this.fetchUser("ee");
|
||||||
}, 2000)
|
}, 2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
src/auth/auth.types.ts
Normal file
4
src/auth/auth.types.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export interface IUserUpdateData {
|
||||||
|
firstName?: string;
|
||||||
|
lastName?: string;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user