mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-06-09 23:06:01 +00:00
feat(auth): adiciona sessao persistente no login
This commit is contained in:
@@ -4,6 +4,7 @@ import { useRouter } from "next/navigation";
|
|||||||
import { type FormEvent, useEffect, useState } from "react";
|
import { type FormEvent, useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Button } from "@/shared/components/ui/button";
|
import { Button } from "@/shared/components/ui/button";
|
||||||
|
import { Checkbox } from "@/shared/components/ui/checkbox";
|
||||||
import {
|
import {
|
||||||
Field,
|
Field,
|
||||||
FieldDescription,
|
FieldDescription,
|
||||||
@@ -38,6 +39,7 @@ export function LoginForm({
|
|||||||
|
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
|
const [rememberMe, setRememberMe] = useState(false);
|
||||||
|
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [loadingEmail, setLoadingEmail] = useState(false);
|
const [loadingEmail, setLoadingEmail] = useState(false);
|
||||||
@@ -60,7 +62,7 @@ export function LoginForm({
|
|||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
callbackURL: "/dashboard",
|
callbackURL: "/dashboard",
|
||||||
rememberMe: false,
|
rememberMe,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onRequest: () => {
|
onRequest: () => {
|
||||||
@@ -186,6 +188,24 @@ export function LoginForm({
|
|||||||
/>
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
|
<div className="flex items-start gap-3">
|
||||||
|
<Checkbox
|
||||||
|
id="remember-me"
|
||||||
|
checked={rememberMe}
|
||||||
|
onCheckedChange={(checked) => setRememberMe(checked === true)}
|
||||||
|
disabled={loadingEmail || loadingGoogle || loadingPasskey}
|
||||||
|
className="mt-0.5"
|
||||||
|
/>
|
||||||
|
<div className="grid gap-1">
|
||||||
|
<FieldLabel
|
||||||
|
htmlFor="remember-me"
|
||||||
|
className="cursor-pointer font-medium"
|
||||||
|
>
|
||||||
|
Manter conectado neste dispositivo
|
||||||
|
</FieldLabel>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<Field>
|
<Field>
|
||||||
<Button
|
<Button
|
||||||
type="submit"
|
type="submit"
|
||||||
|
|||||||
@@ -14,6 +14,25 @@ import { normalizeNameFromEmail } from "@/shared/lib/payers/utils";
|
|||||||
|
|
||||||
const googleClientId = process.env.GOOGLE_CLIENT_ID;
|
const googleClientId = process.env.GOOGLE_CLIENT_ID;
|
||||||
const googleClientSecret = process.env.GOOGLE_CLIENT_SECRET;
|
const googleClientSecret = process.env.GOOGLE_CLIENT_SECRET;
|
||||||
|
const DEFAULT_SESSION_EXPIRES_IN_DAYS = 30;
|
||||||
|
const DEFAULT_SESSION_UPDATE_AGE_HOURS = 24;
|
||||||
|
|
||||||
|
function parsePositiveIntegerEnv(name: string, fallback: number): number {
|
||||||
|
const value = process.env[name];
|
||||||
|
if (!value) return fallback;
|
||||||
|
|
||||||
|
const parsed = Number.parseInt(value, 10);
|
||||||
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sessionExpiresInDays = parsePositiveIntegerEnv(
|
||||||
|
"AUTH_SESSION_EXPIRES_IN_DAYS",
|
||||||
|
DEFAULT_SESSION_EXPIRES_IN_DAYS,
|
||||||
|
);
|
||||||
|
const sessionUpdateAgeHours = parsePositiveIntegerEnv(
|
||||||
|
"AUTH_SESSION_UPDATE_AGE_HOURS",
|
||||||
|
DEFAULT_SESSION_UPDATE_AGE_HOURS,
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extrai nome do usuário do perfil do Google com fallback hierárquico:
|
* Extrai nome do usuário do perfil do Google com fallback hierárquico:
|
||||||
@@ -77,6 +96,8 @@ export const auth = betterAuth({
|
|||||||
|
|
||||||
// Session configuration - Safari compatibility
|
// Session configuration - Safari compatibility
|
||||||
session: {
|
session: {
|
||||||
|
expiresIn: sessionExpiresInDays * 24 * 60 * 60,
|
||||||
|
updateAge: sessionUpdateAgeHours * 60 * 60,
|
||||||
cookieCache: {
|
cookieCache: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
maxAge: 60 * 5, // 5 minutes
|
maxAge: 60 * 5, // 5 minutes
|
||||||
|
|||||||
Reference in New Issue
Block a user