Skip to content

Commit

Permalink
Merge pull request #380 from SkyAjax/feature-reset-password
Browse files Browse the repository at this point in the history
[#355] Feature reset password
  • Loading branch information
dzencot authored Sep 18, 2023
2 parents f396268 + c2e847a commit adc5e15
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
8 changes: 8 additions & 0 deletions backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ export class UsersController {
return this.usersService.recover(recoverUserDto);
}

@Get('recover/:hash')
@UseFilters(new HttpValidationFilter())
@ApiParam({ name: 'hash', description: 'Hash key for user recovery!' })
@ApiOkResponse({ description: 'Successfully checked recovery hash key' })
async checkHash(@Param('hash') hash: string) {
return this.usersService.checkHash(hash);
}

@Post('recover/:hash')
@UseFilters(new HttpValidationFilter())
@ApiParam({ name: 'hash', description: 'Hash key for user password reset!' })
Expand Down
10 changes: 10 additions & 0 deletions backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ export class UsersService {
}
}

async checkHash(hash: string): Promise<{ id: number | null }> {
const email = await decipher(Buffer.from(hash, 'hex'));
const currentUser = await this.find(email);

if (currentUser && currentUser.recover_hash === hash) {
return { id: currentUser.id };
}
return { id: null };
}

async resetPassword(
{ password }: UpdateUserDto,
hash,
Expand Down
20 changes: 18 additions & 2 deletions frontend/src/pages/reset-password/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useEffect, useState } from 'react';
import axios from 'axios';

import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useParams } from 'react-router-dom';

import Col from 'react-bootstrap/Col';
import Container from 'react-bootstrap/Container';
Expand All @@ -8,12 +11,23 @@ import Row from 'react-bootstrap/Row';
import routes from '../../routes.js';

import ResetPasswordForm from '../../components/Forms/ResetPasswordForm';
import NotFoundPage from '../404';

function ResetPasswordPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const { hash } = useParams();
const [userId, setUserId] = useState(false);

useEffect(() => {
const checkHash = async () => {
const { data } = await axios.get(`${routes.resetPassPath()}/${hash}`);
setUserId(data.id);
};
checkHash();
}, [userId, hash]);

return (
return userId ? (
<div className="page-bg-image">
<Container className="h-100" fluid="sm">
<Row className="justify-content-center align-items-center m-auto py-3 py-sm-5 h-100">
Expand All @@ -30,6 +44,8 @@ function ResetPasswordPage() {
</Row>
</Container>
</div>
) : (
<NotFoundPage />
);
}

Expand Down

0 comments on commit adc5e15

Please sign in to comment.