Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(register): password confirmation #85

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/poor-chefs-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/api': minor
'@hyperdx/app': minor
---

feat(register): password confirmation
9 changes: 8 additions & 1 deletion packages/api/src/routers/api/__tests__/team.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ describe('team router', () => {
const login = async () => {
const agent = getAgent(server);

await agent.post('/register/password').send(MOCK_USER).expect(200);
await agent
.post('/register/password')
.send({ ...MOCK_USER, confirmPassword: 'wrong-password' })
.expect(400);
await agent
.post('/register/password')
.send({ ...MOCK_USER, confirmPassword: MOCK_USER.password })
.expect(200);

const user = await findUserByEmail(MOCK_USER.email);
const team = await getTeam(user?.team as any);
Expand Down
42 changes: 24 additions & 18 deletions packages/api/src/routers/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@ import {
handleAuthError,
} from '../../middleware/auth';

const registrationSchema = z.object({
email: z.string().email(),
password: z
.string()
.min(12, 'Password must have at least 12 characters')
.refine(
pass => /[a-z]/.test(pass) && /[A-Z]/.test(pass),
'Password must include both lower and upper case characters',
)
.refine(
pass => /\d/.test(pass),
'Password must include at least one number',
)
.refine(
pass => /[!@#$%^&*(),.?":{}|<>]/.test(pass),
'Password must include at least one special character',
),
});
const registrationSchema = z
.object({
email: z.string().email(),
password: z
.string()
.min(12, 'Password must have at least 12 characters')
.refine(
pass => /[a-z]/.test(pass) && /[A-Z]/.test(pass),
'Password must include both lower and upper case characters',
)
.refine(
pass => /\d/.test(pass),
'Password must include at least one number',
)
.refine(
pass => /[!@#$%^&*(),.?":{}|<>]/.test(pass),
'Password must include at least one special character',
),
confirmPassword: z.string(),
})
.refine(data => data.password === data.confirmPassword, {
message: "Passwords don't match",
path: ['confirmPassword'],
});

const router = express.Router();

Expand Down
30 changes: 28 additions & 2 deletions packages/app/src/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { API_SERVER_URL } from './config';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import Link from 'next/link';
import cx from 'classnames';

import LandingHeader from './LandingHeader';
import * as config from './config';
Expand All @@ -13,6 +14,7 @@ import api from './api';
type FormData = {
email: string;
password: string;
confirmPassword: string;
};

export default function AuthPage({ action }: { action: 'register' | 'login' }) {
Expand Down Expand Up @@ -45,7 +47,11 @@ export default function AuthPage({ action }: { action: 'register' | 'login' }) {

const onSubmit: SubmitHandler<FormData> = data =>
registerPassword.mutate(
{ email: data.email, password: data.password },
{
email: data.email,
password: data.password,
confirmPassword: data.confirmPassword,
},
{
onSuccess: () => router.push('/search'),
onError: async error => {
Expand Down Expand Up @@ -73,6 +79,7 @@ export default function AuthPage({ action }: { action: 'register' | 'login' }) {
controller: { onSubmit: handleSubmit(onSubmit) },
email: register('email', { required: true }),
password: register('password', { required: true }),
confirmPassword: register('confirmPassword', { required: true }),
}
: {
controller: {
Expand Down Expand Up @@ -135,9 +142,28 @@ export default function AuthPage({ action }: { action: 'register' | 'login' }) {
data-test-id="form-password"
id="password"
type="password"
className="border-0"
className={cx('border-0', {
'mb-3': isRegister,
})}
{...form.password}
/>
{isRegister && (
<>
<Form.Label
htmlFor="password"
mark-omarov marked this conversation as resolved.
Show resolved Hide resolved
className="text-start text-muted fs-7.5 mb-1"
>
Confirm Password
</Form.Label>
<Form.Control
data-test-id="form-confirm-password"
id="confirmPassword"
type="password"
className="border-0"
{...form.confirmPassword}
/>
</>
)}
{isRegister && Object.keys(errors).length > 0 && (
<div className="text-danger mt-2">
{Object.values(errors).map((error, index) => (
Expand Down
22 changes: 13 additions & 9 deletions packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,15 +545,19 @@ const api = {
);
},
useRegisterPassword() {
return useMutation<any, HTTPError, { email: string; password: string }>(
async ({ email, password }) =>
server(`register/password`, {
method: 'POST',
json: {
email,
password,
},
}).json(),
return useMutation<
any,
HTTPError,
{ email: string; password: string; confirmPassword: string }
>(async ({ email, password, confirmPassword }) =>
server(`register/password`, {
method: 'POST',
json: {
email,
password,
confirmPassword,
},
}).json(),
);
},
};
Expand Down
Loading