completed pw change, TESTNG REQUIRED
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
"use client";
|
'use client';
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { DiscordAccount, User } from "@repo/db";
|
import { DiscordAccount, User } from '@repo/db';
|
||||||
import { useState } from "react";
|
import { useState } from 'react';
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from "zod";
|
import { z } from 'zod';
|
||||||
import { unlinkDiscord, updateUser, changePassword } from "../actions";
|
import { unlinkDiscord, updateUser, changePassword } from '../actions';
|
||||||
import { Toaster, toast } from "react-hot-toast";
|
import { Toaster, toast } from 'react-hot-toast';
|
||||||
import { useSession } from "next-auth/react";
|
import { useSession } from 'next-auth/react';
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from 'next/navigation';
|
||||||
import { Button } from "../../../_components/ui/Button";
|
import { Button } from '../../../_components/ui/Button';
|
||||||
import {
|
import {
|
||||||
PersonIcon,
|
PersonIcon,
|
||||||
EnvelopeClosedIcon,
|
EnvelopeClosedIcon,
|
||||||
@@ -20,14 +20,14 @@ import {
|
|||||||
LockClosedIcon,
|
LockClosedIcon,
|
||||||
LockOpen2Icon,
|
LockOpen2Icon,
|
||||||
LockOpen1Icon,
|
LockOpen1Icon,
|
||||||
} from "@radix-ui/react-icons";
|
} from '@radix-ui/react-icons';
|
||||||
|
|
||||||
export const ProfileForm = ({ user }: { user: User }) => {
|
export const ProfileForm = ({ user }: { user: User }) => {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
firstname: z.string().min(2).max(30),
|
firstname: z.string().min(2).max(30),
|
||||||
lastname: z.string().min(2).max(30),
|
lastname: z.string().min(2).max(30),
|
||||||
email: z.string().email({
|
email: z.string().email({
|
||||||
message: "Bitte gebe eine gültige E-Mail Adresse ein",
|
message: 'Bitte gebe eine gültige E-Mail Adresse ein',
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
@@ -49,12 +49,12 @@ export const ProfileForm = ({ user }: { user: User }) => {
|
|||||||
await updateUser(values);
|
await updateUser(values);
|
||||||
form.reset(values);
|
form.reset(values);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
toast.success("Deine Änderungen wurden gespeichert!", {
|
toast.success('Deine Änderungen wurden gespeichert!', {
|
||||||
style: {
|
style: {
|
||||||
background:
|
background:
|
||||||
"var(--fallback-b1, oklch(var(--b1) / var(--tw-bg-opacity, 1)))",
|
'var(--fallback-b1, oklch(var(--b1) / var(--tw-bg-opacity, 1)))',
|
||||||
color:
|
color:
|
||||||
"var(--fallback-nc, oklch(var(--nc) / var(--tw-text-opacity, 1)))",
|
'var(--fallback-nc, oklch(var(--nc) / var(--tw-text-opacity, 1)))',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})}
|
})}
|
||||||
@@ -70,7 +70,7 @@ export const ProfileForm = ({ user }: { user: User }) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...form.register("firstname")}
|
{...form.register('firstname')}
|
||||||
type="text"
|
type="text"
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
defaultValue={user.firstname}
|
defaultValue={user.firstname}
|
||||||
@@ -89,7 +89,7 @@ export const ProfileForm = ({ user }: { user: User }) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...form.register("lastname")}
|
{...form.register('lastname')}
|
||||||
type="text"
|
type="text"
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
defaultValue={user.lastname}
|
defaultValue={user.lastname}
|
||||||
@@ -108,7 +108,7 @@ export const ProfileForm = ({ user }: { user: User }) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...form.register("email")}
|
{...form.register('email')}
|
||||||
type="text"
|
type="text"
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
defaultValue={user.email}
|
defaultValue={user.email}
|
||||||
@@ -170,12 +170,12 @@ export const SocialForm = ({
|
|||||||
});
|
});
|
||||||
setVatsimLoading(false);
|
setVatsimLoading(false);
|
||||||
form.reset(values);
|
form.reset(values);
|
||||||
toast.success("Deine Änderungen wurden gespeichert!", {
|
toast.success('Deine Änderungen wurden gespeichert!', {
|
||||||
style: {
|
style: {
|
||||||
background:
|
background:
|
||||||
"var(--fallback-b1, oklch(var(--b1) / var(--tw-bg-opacity, 1)))",
|
'var(--fallback-b1, oklch(var(--b1) / var(--tw-bg-opacity, 1)))',
|
||||||
color:
|
color:
|
||||||
"var(--fallback-nc, oklch(var(--nc) / var(--tw-text-opacity, 1)))",
|
'var(--fallback-nc, oklch(var(--nc) / var(--tw-text-opacity, 1)))',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})}
|
})}
|
||||||
@@ -207,7 +207,7 @@ export const SocialForm = ({
|
|||||||
Verbunden mit {discordAccount.username}
|
Verbunden mit {discordAccount.username}
|
||||||
</span>
|
</span>
|
||||||
<span className="hidden group-hover:inline">
|
<span className="hidden group-hover:inline">
|
||||||
Verbindung trennen{isLoading && "..."}
|
Verbindung trennen{isLoading && '...'}
|
||||||
</span>
|
</span>
|
||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
@@ -231,7 +231,7 @@ export const SocialForm = ({
|
|||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
placeholder="1445241"
|
placeholder="1445241"
|
||||||
defaultValue={user.vatsimCid as number | undefined}
|
defaultValue={user.vatsimCid as number | undefined}
|
||||||
{...form.register("vatsimCid", {
|
{...form.register('vatsimCid', {
|
||||||
valueAsNumber: true,
|
valueAsNumber: true,
|
||||||
})}
|
})}
|
||||||
/>
|
/>
|
||||||
@@ -269,22 +269,33 @@ export const PasswordForm = ({ user }: { user: User }) => {
|
|||||||
defaultValues: {},
|
defaultValues: {},
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
});
|
});
|
||||||
|
console.log(form.formState.errors);
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
className="card-body"
|
className="card-body"
|
||||||
onSubmit={form.handleSubmit(async (values) => {
|
onSubmit={form.handleSubmit(async (values) => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
await changePassword(values);
|
const result = await changePassword(
|
||||||
|
values.password,
|
||||||
|
values.newPassword
|
||||||
|
);
|
||||||
form.reset(values);
|
form.reset(values);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
toast.success("Dein Passwort wurde geändert!", {
|
if (result.error) {
|
||||||
style: {
|
form.setError('password', {
|
||||||
background:
|
type: 'manual',
|
||||||
"var(--fallback-b1, oklch(var(--b1) / var(--tw-bg-opacity, 1)))",
|
message: result.error,
|
||||||
color:
|
});
|
||||||
"var(--fallback-nc, oklch(var(--nc) / var(--tw-text-opacity, 1)))",
|
} else if (result.success) {
|
||||||
},
|
toast.success('Dein Passwort wurde geändert!', {
|
||||||
});
|
style: {
|
||||||
|
background:
|
||||||
|
'var(--fallback-b1, oklch(var(--b1) / var(--tw-bg-opacity, 1)))',
|
||||||
|
color:
|
||||||
|
'var(--fallback-nc, oklch(var(--nc) / var(--tw-text-opacity, 1)))',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<h2 className="card-title">
|
<h2 className="card-title">
|
||||||
@@ -298,10 +309,10 @@ export const PasswordForm = ({ user }: { user: User }) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...form.register("password")}
|
{...form.register('password')}
|
||||||
type="text"
|
type="text"
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
defaultValue={""}
|
defaultValue={''}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
{form.formState.errors.password && (
|
{form.formState.errors.password && (
|
||||||
@@ -314,10 +325,10 @@ export const PasswordForm = ({ user }: { user: User }) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...form.register("newPassword")}
|
{...form.register('newPassword')}
|
||||||
type="text"
|
type="text"
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
defaultValue={""}
|
defaultValue={''}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
{form.formState.errors.newPassword && (
|
{form.formState.errors.newPassword && (
|
||||||
@@ -332,10 +343,10 @@ export const PasswordForm = ({ user }: { user: User }) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...form.register("newPasswordConfirm")}
|
{...form.register('newPasswordConfirm')}
|
||||||
type="text"
|
type="text"
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
defaultValue={""}
|
defaultValue={''}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
{form.formState.errors.newPasswordConfirm && (
|
{form.formState.errors.newPasswordConfirm && (
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"use server";
|
'use server';
|
||||||
import { Prisma, PrismaClient } from "@repo/db";
|
import { prisma, Prisma, PrismaClient } from '@repo/db';
|
||||||
import { getServerSession } from "../../api/auth/[...nextauth]/auth";
|
import { getServerSession } from '../../api/auth/[...nextauth]/auth';
|
||||||
|
import bcrypt from 'bcryptjs';
|
||||||
|
|
||||||
export const unlinkDiscord = async (userId: string) => {
|
export const unlinkDiscord = async (userId: string) => {
|
||||||
const client = new PrismaClient();
|
const client = new PrismaClient();
|
||||||
@@ -25,6 +26,31 @@ export const updateUser = async (changes: Prisma.UserUpdateInput) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changePassword = async (changes: Prisma.UserUpdateInput) => {
|
export const changePassword = async (
|
||||||
// TODO: Add password change logic
|
oldPassword: string,
|
||||||
|
newPassword: string
|
||||||
|
) => {
|
||||||
|
const session = await getServerSession();
|
||||||
|
if (!session)
|
||||||
|
return {
|
||||||
|
error: 'User not found',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!(await bcrypt.compare(oldPassword, session.user.password)))
|
||||||
|
return {
|
||||||
|
error: 'Old password is incorrect',
|
||||||
|
};
|
||||||
|
|
||||||
|
const hashedPassword = await bcrypt.hash(newPassword, 15);
|
||||||
|
await prisma.user.update({
|
||||||
|
data: {
|
||||||
|
password: hashedPassword,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: session.user.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user