-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from RicardoGEsteves/settings-page
feat: ✨ feat(settings-page): Update UI components and add settings page
- Loading branch information
Showing
12 changed files
with
924 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"use server"; | ||
|
||
import * as z from "zod"; | ||
import bcrypt from "bcryptjs"; | ||
|
||
import { update } from "@/auth"; | ||
import { db } from "@/lib/db"; | ||
import { SettingsSchema } from "@/schemas"; | ||
import { getUserByEmail, getUserById } from "@/data/user"; | ||
import { currentUser } from "@/lib/auth"; | ||
import { generateVerificationToken } from "@/lib/tokens"; | ||
import { sendVerificationEmail } from "@/lib/mail"; | ||
|
||
export const settings = async (values: z.infer<typeof SettingsSchema>) => { | ||
const user = await currentUser(); | ||
|
||
if (!user) { | ||
return { error: "Unauthorized" }; | ||
} | ||
|
||
const dbUser = await getUserById(user.id); | ||
|
||
if (!dbUser) { | ||
return { error: "Unauthorized" }; | ||
} | ||
|
||
if (user.isOAuth) { | ||
values.email = undefined; | ||
values.password = undefined; | ||
values.newPassword = undefined; | ||
values.isTwoFactorEnabled = undefined; | ||
} | ||
|
||
if (values.email && values.email !== user.email) { | ||
const existingUser = await getUserByEmail(values.email); | ||
|
||
if (existingUser && existingUser.id !== user.id) { | ||
return { error: "Email already in use!" }; | ||
} | ||
|
||
const verificationToken = await generateVerificationToken(values.email); | ||
await sendVerificationEmail( | ||
verificationToken.email, | ||
verificationToken.token | ||
); | ||
|
||
return { success: "Verification email sent!" }; | ||
} | ||
|
||
if (values.password && values.newPassword && dbUser.password) { | ||
const passwordsMatch = await bcrypt.compare( | ||
values.password, | ||
dbUser.password | ||
); | ||
|
||
if (!passwordsMatch) { | ||
return { error: "Incorrect password!" }; | ||
} | ||
|
||
const hashedPassword = await bcrypt.hash(values.newPassword, 10); | ||
values.password = hashedPassword; | ||
values.newPassword = undefined; | ||
} | ||
|
||
const updatedUser = await db.user.update({ | ||
where: { id: dbUser.id }, | ||
data: { | ||
...values, | ||
}, | ||
}); | ||
|
||
update({ | ||
user: { | ||
name: updatedUser.name, | ||
email: updatedUser.email, | ||
isTwoFactorEnabled: updatedUser.isTwoFactorEnabled, | ||
role: updatedUser.role, | ||
}, | ||
}); | ||
|
||
return { success: "Settings Updated!" }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.