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

Use full name instead of first/last #635

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 38 additions & 25 deletions packages/api-v2/src/student/student.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
UpdateStudentDto,
} from "@graduate/common";
import { Student } from "./entities/student.entity";
import { EmailAlreadyExists, NewPasswordsDontMatch, WeakPassword, WrongPassword } from "./student.errors";
import {
EmailAlreadyExists,
NewPasswordsDontMatch,
WeakPassword,
WrongPassword,
} from "./student.errors";

@Injectable()
export class StudentService {
Expand All @@ -25,14 +30,13 @@ export class StudentService {
constructor(
@InjectRepository(Student)
private studentRepository: Repository<Student>
) { }
) {}

async create(
createStudentDto: SignUpStudentDto
): Promise<Student | EmailAlreadyExists | WeakPassword> {
// make sure the user doesn't already exists
const { email, firstName, lastName, password, passwordConfirm } =
createStudentDto;
const { email, fullName, password, passwordConfirm } = createStudentDto;
const userInDb = await this.studentRepository.findOne({ where: { email } });
if (userInDb) {
this.logger.debug(
Expand All @@ -50,11 +54,6 @@ export class StudentService {
return new WeakPassword();
}

let fullName;
if (firstName && lastName) {
fullName = `${firstName} ${lastName}`;
}

const newStudent = this.studentRepository.create({
fullName,
email,
Expand Down Expand Up @@ -161,49 +160,63 @@ export class StudentService {
return formatServiceCtx(StudentService.name, methodName);
}

async changePassword(uuid: any, changePasswordDto: ChangePasswordDto): Promise<void | WeakPassword | WrongPassword> {
const { currentPassword, newPassword, newPasswordConfirm } = changePasswordDto;
async changePassword(
uuid: any,
changePasswordDto: ChangePasswordDto
): Promise<void | WeakPassword | WrongPassword> {
const { currentPassword, newPassword, newPasswordConfirm } =
changePasswordDto;
const student = await this.findByUuid(uuid);

if (newPassword !== newPasswordConfirm) {
return new NewPasswordsDontMatch();
}

const { password: trueHashedPassword } = student;
const isValidPassword = await bcrypt.compare(currentPassword, trueHashedPassword);
const isValidPassword = await bcrypt.compare(
currentPassword,
trueHashedPassword
);

if (!isValidPassword) {
this.logger.debug(
{ message: "Invalid password", oldPassword: currentPassword },
);
this.logger.debug({
message: "Invalid password",
oldPassword: currentPassword,
});
return new WrongPassword();
}

if (!isStrongPassword(newPassword)) {
this.logger.debug(
{ message: "weak password", oldPassword: currentPassword },
);
this.logger.debug({
message: "weak password",
oldPassword: currentPassword,
});
return new WeakPassword();
}
await this.studentRepository.save(Object.assign(student, { password: newPassword }));
await this.studentRepository.save(
Object.assign(student, { password: newPassword })
);
}

async resetPassword(email, resetPasswordData: ResetPasswordDto): Promise<Student | Error> {
async resetPassword(
email,
resetPasswordData: ResetPasswordDto
): Promise<Student | Error> {
const { password, passwordConfirm } = resetPasswordData;

const student = await this.findByEmail(email)
const student = await this.findByEmail(email);

if (password !== passwordConfirm) {
return new NewPasswordsDontMatch();
}

if (!isStrongPassword(password)) {
this.logger.debug(
{ message: "weak password", password },
);
this.logger.debug({ message: "weak password", password });
return new WeakPassword();
}

return await this.studentRepository.save(Object.assign(student, { password }))
return await this.studentRepository.save(
Object.assign(student, { password })
);
}
}
10 changes: 3 additions & 7 deletions packages/common/src/api-dtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ export class UpdatePlanDto {
export class SignUpStudentDto {
@IsOptional()
@IsString()
firstName: string;

@IsOptional()
@IsString()
lastName: string;
fullName: string;

@IsNotEmpty()
@IsEmail()
Expand Down Expand Up @@ -229,7 +225,7 @@ export class ForgotPasswordDto {
export class ResetPasswordDto {
@IsNotEmpty()
@IsString()
token: string
token: string;

@IsString()
@IsNotEmpty()
Expand All @@ -238,4 +234,4 @@ export class ResetPasswordDto {
@IsString()
@IsNotEmpty()
passwordConfirm: string;
}
}
39 changes: 8 additions & 31 deletions packages/frontend-v2/components/Header/AccountOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ interface AccountOverviewProps {
}

interface UpdateName {
firstName: string;
lastName: string;
fullName: string;
}

export const AccountOverview: React.FC<AccountOverviewProps> = ({
Expand All @@ -45,8 +44,6 @@ export const AccountOverview: React.FC<AccountOverviewProps> = ({
register,
handleSubmit,
formState: { errors, isSubmitting },
watch,
trigger,
reset,
} = useForm<UpdateName>({
mode: "onChange",
Expand All @@ -56,19 +53,16 @@ export const AccountOverview: React.FC<AccountOverviewProps> = ({
useEffect(() => {
if (student.fullName) {
reset({
firstName: student.fullName.split(" ")[0],
lastName: student.fullName.split(" ")[1],
fullName: student.fullName,
});
}
}, [reset, student]);

const firstName = watch("firstName", "");

const onSubmitHandler = async (payload: UpdateName) => {
try {
const newStudent: StudentModel<string> = {
...student,
fullName: `${payload.firstName} ${payload.lastName}`,
fullName: payload.fullName,
};
mutateStudent(async () => {
await API.student.update(newStudent);
Expand Down Expand Up @@ -103,28 +97,11 @@ export const AccountOverview: React.FC<AccountOverviewProps> = ({
<Flex columnGap="md">
<GraduateInput
type="text"
formLabel="First Name"
id="firstName"
placeholder="Cooper"
error={errors.firstName}
{...register("firstName", {
onBlur: () => trigger("lastName"),
pattern: noLeadOrTrailWhitespacePattern,
})}
/>
<GraduateInput
error={errors.lastName}
type="text"
formLabel="Last Name"
id="lastName"
placeholder="The Dog"
{...register("lastName", {
validate: (lastName) => {
if (lastName !== "" && firstName === "") {
return "Please enter your first name along with your last name.";
}
return true;
},
formLabel="Full Name"
id="fullName"
placeholder="Cooper The Dog"
error={errors.fullName}
{...register("fullName", {
pattern: noLeadOrTrailWhitespacePattern,
})}
/>
Expand Down
37 changes: 10 additions & 27 deletions packages/frontend-v2/pages/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const SignUpForm: React.FC = () => {

// Need to keep track of these values for validation
const password = watch("password", "");
const firstName = watch("firstName", "");

const onSubmitHandler = async (payload: SignUpStudentDto) => {
try {
Expand Down Expand Up @@ -76,38 +75,22 @@ const SignUpForm: React.FC = () => {
inputs={
<>
<Flex direction="column" rowGap="xs">
<Flex alignItems="center" columnGap="sm" color="gray">
<InfoOutlineIcon />
<Text color="gray" lineHeight="1">
Name is optional.
</Text>
</Flex>
<Flex columnGap="md">
<GraduateInput
type="text"
id="firstName"
placeholder="First Name"
error={errors.firstName}
{...register("firstName", {
onBlur: () => trigger("lastName"),
id="fullName"
placeholder="Full Name"
error={errors.fullName}
{...register("fullName", {
pattern: noLeadOrTrailWhitespacePattern,
})}
/>
<GraduateInput
type="text"
id="lastName"
placeholder="Last Name"
error={errors.lastName}
{...register("lastName", {
validate: (lastName) => {
if (lastName !== "" && firstName === "") {
return "Please enter your first name along with your last name.";
}
return true;
},
pattern: noLeadOrTrailWhitespacePattern,
})}
/>
</Flex>
<Flex alignItems="center" columnGap="sm" color="gray">
<InfoOutlineIcon />
<Text color="gray" lineHeight="1">
Name is optional. If provided, enter at least your first name.
</Text>
</Flex>
</Flex>
<GraduateInput
Expand Down