adição de recuperação de senha e limpeza de remanescentes
This commit is contained in:
@@ -255,6 +255,50 @@ export const criarConta = createServerFn({ method: "POST" })
|
||||
};
|
||||
});
|
||||
|
||||
export const recuperarSenha = createServerFn({ method: "POST" })
|
||||
.validator(loginSchema)
|
||||
.handler(async ({ data }) => {
|
||||
const nome = normalizarNome(data.nome);
|
||||
const [{ getPool, sql }, bcrypt] = await Promise.all([
|
||||
import("@/lib/db.server"),
|
||||
import("bcryptjs"),
|
||||
]);
|
||||
const pool = await getPool();
|
||||
|
||||
const existente = await pool
|
||||
.request()
|
||||
.input("nome", sql.NVarChar(150), nome)
|
||||
.query<{ id: string; ativo: boolean }>(`
|
||||
SELECT TOP 1 id, ativo
|
||||
FROM slalog.usuarios
|
||||
WHERE LOWER(nome) = LOWER(@nome)
|
||||
`);
|
||||
|
||||
const usuario = existente.recordset[0];
|
||||
if (!usuario || !usuario.ativo) {
|
||||
throw new Error("Nome de usuário não encontrado.");
|
||||
}
|
||||
|
||||
const senhaHash = await bcrypt.hash(data.senha, 10);
|
||||
|
||||
await pool
|
||||
.request()
|
||||
.input("id", sql.UniqueIdentifier, usuario.id)
|
||||
.input("senhaHash", sql.NVarChar(255), senhaHash)
|
||||
.query(`
|
||||
UPDATE slalog.usuarios
|
||||
SET senha_hash = @senhaHash,
|
||||
aprovado = 0,
|
||||
updated_at = SYSDATETIME()
|
||||
WHERE id = @id
|
||||
`);
|
||||
|
||||
const { clearAuthSession } = await import("@/lib/auth.server");
|
||||
await clearAuthSession();
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
export const sair = createServerFn({ method: "POST" }).handler(async () => {
|
||||
const { clearAuthSession } = await import("@/lib/auth.server");
|
||||
await clearAuthSession();
|
||||
|
||||
Reference in New Issue
Block a user