-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e55f9a
commit 8d19d70
Showing
5 changed files
with
153 additions
and
1 deletion.
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,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." }; | ||
} | ||
}; |
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,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." }; | ||
} | ||
}; |
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,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." }; | ||
} | ||
}; |
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,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." }; | ||
} | ||
}; |
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