web:framework:nextjs:nextauth

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
web:framework:nextjs:nextauth [2025/03/19 11:00] – [LoginForm] jcheronweb:framework:nextjs:nextauth [2025/08/12 02:35] (Version actuelle) – modification externe 127.0.0.1
Ligne 222: Ligne 222:
 } }
 </sxh> </sxh>
 +
 +===== Configuration =====
 +
 +Dans le fichier ''.env'' ou ''.env.local'', définir la variable ''NEXTAUTH_URL'':
 +
 +<sxh env>
 +NEXTAUTH_URL=http://127.0.0.1:3000
 +</sxh>
 +
 +
 +Génération du secret pour encodage/décodage JWT :
 +
 +<sxh bash>
 +npx auth secret
 +</sxh>
 +
 +Le **secret** est généré et inséré dans le fichier ''.env.local''.
 +===== Usage =====
 +Accès à la session nextAuth :
 +
 +==== Côté serveur ====
 +
 +<sxh ts;gutter:false>
 +import {auth} from "@/auth";
 +
 +const session= await auth();
 +console.log(session.user); //user connecté
 +</sxh>
 +
 +==== Côté client ====
 +
 +<sxh ts;gutter:false>
 +'use client';
 +import {useSession} from "next-auth/react";
 +
 +const userData=useSession();
 +console.log(userData.data?.user); //user connecté
 +</sxh>
 +
 +
  
  • web/framework/nextjs/nextauth.1742378428.txt.gz
  • Dernière modification : il y a 8 semaines
  • (modification externe)