なんかもうめっちゃ変えた
This commit is contained in:
parent
d9ab03f086
commit
b75184ec8e
946 changed files with 41219 additions and 28839 deletions
|
@ -1,13 +1,14 @@
|
|||
import rndstr from 'rndstr';
|
||||
import ms from 'ms';
|
||||
import { IsNull } from 'typeorm';
|
||||
import { publishMainStream } from '@/services/stream.js';
|
||||
import config from '@/config/index.js';
|
||||
import { Users, UserProfiles, PasswordResetRequests } from '@/models/index.js';
|
||||
import { sendEmail } from '@/services/send-email.js';
|
||||
import { genId } from '@/misc/gen-id.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PasswordResetRequestsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import { Config } from '@/config.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { EmailService } from '@/core/EmailService.js';
|
||||
import { ApiError } from '../error.js';
|
||||
import define from '../define.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['reset password'],
|
||||
|
@ -36,41 +37,61 @@ export const paramDef = {
|
|||
} as const;
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default define(meta, paramDef, async (ps) => {
|
||||
const user = await Users.findOneBy({
|
||||
usernameLower: ps.username.toLowerCase(),
|
||||
host: IsNull(),
|
||||
});
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.config)
|
||||
private config: Config,
|
||||
|
||||
@Inject(DI.usersRepository)
|
||||
private usersRepository: UsersRepository,
|
||||
|
||||
// 合致するユーザーが登録されていなかったら無視
|
||||
if (user == null) {
|
||||
return;
|
||||
@Inject(DI.userProfilesRepository)
|
||||
private userProfilesRepository: UserProfilesRepository,
|
||||
|
||||
@Inject(DI.passwordResetRequestsRepository)
|
||||
private passwordResetRequestsRepository: PasswordResetRequestsRepository,
|
||||
|
||||
private idService: IdService,
|
||||
private emailService: EmailService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const user = await this.usersRepository.findOneBy({
|
||||
usernameLower: ps.username.toLowerCase(),
|
||||
host: IsNull(),
|
||||
});
|
||||
|
||||
// 合致するユーザーが登録されていなかったら無視
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
||||
|
||||
// 合致するメアドが登録されていなかったら無視
|
||||
if (profile.email !== ps.email) {
|
||||
return;
|
||||
}
|
||||
|
||||
// メアドが認証されていなかったら無視
|
||||
if (!profile.emailVerified) {
|
||||
return;
|
||||
}
|
||||
|
||||
const token = rndstr('a-z0-9', 64);
|
||||
|
||||
await this.passwordResetRequestsRepository.insert({
|
||||
id: this.idService.genId(),
|
||||
createdAt: new Date(),
|
||||
userId: profile.userId,
|
||||
token,
|
||||
});
|
||||
|
||||
const link = `${this.config.url}/reset-password/${token}`;
|
||||
|
||||
this.emailService.sendEmail(ps.email, 'Password reset requested',
|
||||
`To reset password, please click this link:<br><a href="${link}">${link}</a>`,
|
||||
`To reset password, please click this link: ${link}`);
|
||||
});
|
||||
}
|
||||
|
||||
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
||||
|
||||
// 合致するメアドが登録されていなかったら無視
|
||||
if (profile.email !== ps.email) {
|
||||
return;
|
||||
}
|
||||
|
||||
// メアドが認証されていなかったら無視
|
||||
if (!profile.emailVerified) {
|
||||
return;
|
||||
}
|
||||
|
||||
const token = rndstr('a-z0-9', 64);
|
||||
|
||||
await PasswordResetRequests.insert({
|
||||
id: genId(),
|
||||
createdAt: new Date(),
|
||||
userId: profile.userId,
|
||||
token,
|
||||
});
|
||||
|
||||
const link = `${config.url}/reset-password/${token}`;
|
||||
|
||||
sendEmail(ps.email, 'Password reset requested',
|
||||
`To reset password, please click this link:<br><a href="${link}">${link}</a>`,
|
||||
`To reset password, please click this link: ${link}`);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue