Browse Source

fix password change

master
nicolas-arnaud 2 years ago
parent
commit
a27d579f06
  1. 7
      back/volume/src/chat/chat.controller.ts
  2. 21
      back/volume/src/chat/chat.service.ts
  3. 10
      back/volume/src/chat/entity/channel.entity.ts
  4. 9
      front/volume/src/components/Channels.svelte

7
back/volume/src/chat/chat.controller.ts

@ -211,10 +211,13 @@ export class ChatController {
@Param('id', ParseIntPipe) id: number, @Param('id', ParseIntPipe) id: number,
@Body() data: PasswordDto @Body() data: PasswordDto
): Promise<void> { ): Promise<void> {
if (await this.channelService.isOwner(id, +profile.id)) { if (!(await this.channelService.isOwner(id, +profile.id))) {
throw new BadRequestException('You are not the owner of this channel') throw new BadRequestException('You are not the owner of this channel')
} }
await this.channelService.updatePassword(id, data.password) let channel = (await this.channelService.getChannel(id)) as Channel
channel.password = await this.channelService.hash(data.password)
this.channelService.update(channel)
} }
@Get() @Get()

21
back/volume/src/chat/chat.service.ts

@ -1,13 +1,14 @@
import { BadRequestException, Injectable } from '@nestjs/common' import { BadRequestException, Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm' import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm' import { Repository } from 'typeorm'
import { Cron } from '@nestjs/schedule'
import * as bcrypt from 'bcrypt'
import { type CreateChannelDto } from './dto/create-channel.dto' import { type CreateChannelDto } from './dto/create-channel.dto'
import { UsersService } from 'src/users/users.service' import { UsersService } from 'src/users/users.service'
import type User from 'src/users/entity/user.entity' import type User from 'src/users/entity/user.entity'
import Channel from './entity/channel.entity' import Channel from './entity/channel.entity'
import { Cron } from '@nestjs/schedule'
@Injectable() @Injectable()
export class ChatService { export class ChatService {
@ -58,7 +59,7 @@ export class ChatService {
newChannel.admins = [user] newChannel.admins = [user]
newChannel.name = channel.name newChannel.name = channel.name
newChannel.isPrivate = channel.isPrivate newChannel.isPrivate = channel.isPrivate
newChannel.password = channel.password newChannel.password = await this.hash(channel.password)
console.log("New channel: ", JSON.stringify(newChannel)) console.log("New channel: ", JSON.stringify(newChannel))
} }
return await this.ChannelRepository.save(newChannel) return await this.ChannelRepository.save(newChannel)
@ -75,15 +76,13 @@ export class ChatService {
return newDM return newDM
} }
async updatePassword (id: number, password: string): Promise<void> { async hash(password: string): Promise<string> {
const channel: Channel | null = await this.ChannelRepository.findOneBy({ if (!password) return ''
id password = await bcrypt.hash(
}) password,
if (channel === null) { Number(process.env.HASH_SALT)
throw new BadRequestException(`Channel #${id} not found`) )
} return password
channel.password = password
await this.update(channel)
} }
async getChannelsForUser (ftId: number): Promise<Channel[]> { async getChannelsForUser (ftId: number): Promise<Channel[]> {

10
back/volume/src/chat/entity/channel.entity.ts

@ -11,7 +11,6 @@ import {
} from 'typeorm' } from 'typeorm'
import User from 'src/users/entity/user.entity' import User from 'src/users/entity/user.entity'
import Message from './message.entity' import Message from './message.entity'
import * as bcrypt from 'bcrypt'
@Entity() @Entity()
export default class Channel { export default class Channel {
@ -27,15 +26,6 @@ export default class Channel {
@Column({ default: '' }) @Column({ default: '' })
password: string password: string
@BeforeInsert()
async hashPassword (): Promise<void> {
if (this.password === '') return
this.password = await bcrypt.hash(
this.password,
Number(process.env.HASH_SALT)
)
}
@ManyToMany(() => User) @ManyToMany(() => User)
@JoinTable() @JoinTable()
users: User[] users: User[]

9
front/volume/src/components/Channels.svelte

@ -119,12 +119,14 @@
let channel: ChannelsType; let channel: ChannelsType;
export const selectChat = (id: number) => { export const selectChat = (id: number) => {
console.log("channel: ", id) console.log("channel: ", id)
getChannels().then(() => {
channel = channels.find((c) => c.id === id); channel = channels.find((c) => c.id === id);
if (channel) { if (channel) {
joinChannel(channel); joinChannel(channel);
} else { } else {
show_popup("Did not find channel", false) show_popup("Did not find channel", false)
} }
})
}; };
socket.on("messages", (msgs: Array<chatMessagesType>) => { socket.on("messages", (msgs: Array<chatMessagesType>) => {
@ -248,11 +250,10 @@
password: string, password: string,
}), }),
}); });
if (response.ok) { if (!response.ok) {
channels.push(await response.json());
} else {
await show_popup("Error changing password", false) await show_popup("Error changing password", false)
} } else
getChannels()
}; };
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/

Loading…
Cancel
Save