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();
|
||||
|
||||
+15
-1
@@ -35,6 +35,7 @@ export const iniciarColeta = createServerFn({ method: "POST" })
|
||||
const sessao = await obterSessaoAprovada();
|
||||
const [{ getPool, sql }] = await Promise.all([import("@/lib/db.server")]);
|
||||
const pool = await getPool();
|
||||
const codigo = data.codigo.trim();
|
||||
|
||||
const dataAberta = await pool
|
||||
.request()
|
||||
@@ -48,10 +49,23 @@ export const iniciarColeta = createServerFn({ method: "POST" })
|
||||
|
||||
if (dataAberta.recordset[0]) throw new Error("Já existe uma coleta em andamento.");
|
||||
|
||||
const duplicado = await pool
|
||||
.request()
|
||||
.input("codigo", sql.NVarChar(100), codigo)
|
||||
.query<{ id: number }>(`
|
||||
SELECT TOP 1 id
|
||||
FROM slalog.coletas
|
||||
WHERE codigo_barras = @codigo
|
||||
`);
|
||||
|
||||
if (duplicado.recordset[0]) {
|
||||
throw new Error("Este número de caixa já foi registrado anteriormente.");
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input("usuario", sql.NVarChar(150), sessao.nome)
|
||||
.input("codigo", sql.NVarChar(100), data.codigo)
|
||||
.input("codigo", sql.NVarChar(100), codigo)
|
||||
.query<ColetaRow>(`
|
||||
INSERT INTO slalog.coletas (usuario, codigo_barras, inicio_coleta, created_at)
|
||||
OUTPUT
|
||||
|
||||
Reference in New Issue
Block a user