Skip to content

Commit

Permalink
feat : add new features
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedrinvits committed Sep 27, 2024
1 parent 1e55f9a commit 8d19d70
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
32 changes: 32 additions & 0 deletions data/deleteUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server"
import { db } from "@/lib/db";
import { auth } from "../auth";
import { getUserByEmail,getUserById } from "./user";

export const deleteUser = async (
) => {
try {
const session = await auth();
const authorId = session?.id;

// Obtenção do usuário pelo e-mail
const user = await getUserById(parseInt(authorId));

if (!user) {
throw new Error('Usuário não encontrado.');
}

// Atualizando as informações do usuário no banco de dados
const updatedUser = await db.user.delete({
where: { id: user.id } // Atualiza o usuário baseado no ID
});

// console.log('Informações atualizadas:', updatedUser);

return { success: "Conta apagada com sucesso!", error: null };

} catch (error) {
console.error('Erro ao atualizar as informações:', error);
return { success: null, error: "Erro ao atualizar as informações." };
}
};
43 changes: 43 additions & 0 deletions data/updateUserEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use server"
import { db } from "@/lib/db";
import { auth } from "../auth";
import { getUserByEmail,getUserById } from "./user";

export const updateUserEmail = async (
email : string,
) => {
try {
// Autenticação para obter a sessão e o ID do usuário
const session = await auth();
const authorId = session?.id;

// Obtenção do usuário pelo e-mail
const user = await getUserById(parseInt(authorId));

const existingUser = await getUserByEmail(email);

if (!user) {
throw new Error('Usuário não encontrado.');
}

if(existingUser) {
return {
error : "Esse email já está sendo usado!"
};
}
// Atualizando as informações do usuário no banco de dados
const updateUserEmail = await db.user.update({
where: { id: user.id }, // Atualiza o usuário baseado no ID
data: {
email : email
},
});

// console.log('Informações atualizadas:', updatedUser);

return { success: "Email com sucesso!", error: null };
} catch (error) {
console.error('Erro ao atualizar as informações:', error);
return { success: null, error: "Erro ao atualizar as informações." };
}
};
37 changes: 37 additions & 0 deletions data/updateUserName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use server"
import { db } from "@/lib/db";
import { auth } from "../auth";
import { getUserByEmail,getUserById } from "./user";

export const updateUserName = async (
name : string,
) => {
try {
// Autenticação para obter a sessão e o ID do usuário
const session = await auth();
const authorId = session?.id;

// Obtenção do usuário pelo e-mail
const user = await getUserById(parseInt(authorId));

if (!user) {
throw new Error('Usuário não encontrado.');
}

// Atualizando as informações do usuário no banco de dados
const updatedUser = await db.user.update({
where: { id: user.id }, // Atualiza o usuário baseado no ID
data: {
name : name
},
});

// console.log('Informações atualizadas:', updatedUser);

return { success: "Nome alterado com sucesso!", error: null };

} catch (error) {
console.error('Erro ao atualizar as informações:', error);
return { success: null, error: "Erro ao atualizar as informações." };
}
};
40 changes: 40 additions & 0 deletions data/updateUserPassword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use server"
import { db } from "@/lib/db";
import { auth } from "../auth";
import { getUserByEmail,getUserById } from "./user";
import * as bcrypt from "bcrypt-ts";

export const updateUserPassword = async (
currentPassword : string,
newPassword : string,
) => {
try {
const session = await auth();
const authorId = session?.id;
const user = await getUserById(parseInt(authorId));
const IsSamePassword = await bcrypt.compare(currentPassword,user?.password)
const hashPassword = await bcrypt.hash(newPassword,10)
if (!user) {
throw new Error('Usuário não encontrado.');
}

if(!IsSamePassword){
return { error: "Senha atual inválida" };
}

const updatedUser = await db.user.update({
where: { id: user.id }, // Atualiza o usuário baseado no ID
data: {
password : hashPassword
},
});

// console.log('Informações atualizadas:', updatedUser);

return { success: "Senha alterada com sucesso!", error: null };

} catch (error) {
console.error('Erro ao atualizar as informações:', error);
return { success: null, error: "Erro ao atualizar as informações." };
}
};
2 changes: 1 addition & 1 deletion data/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getUserByEmail = async (email : string) => {
}


export const getUserById = async (id : string) => {
export const getUserById = async (id : number) => {
try {

const user = db.user.findUnique({
Expand Down

0 comments on commit 8d19d70

Please sign in to comment.