Différences
Ci-dessous, les différences entre deux révisions de la page.
| 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 09:03] – jcheron | web:framework:nextjs:nextauth [2025/08/12 02:35] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 10: | Ligne 10: | ||
| ===== Configuration ===== | ===== Configuration ===== | ||
| - | < | + | < |
| import NextAuth from " | import NextAuth from " | ||
| import CredentialsProvider from " | import CredentialsProvider from " | ||
| Ligne 130: | Ligne 130: | ||
| export const {handlers, auth, signIn, signOut} = NextAuth(authConfig); | export const {handlers, auth, signIn, signOut} = NextAuth(authConfig); | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Route api ===== | ||
| + | Toutes les requêtes NextAuth passeront par cette route : '' | ||
| + | |||
| + | <sxh typescript> | ||
| + | import {handlers} from " | ||
| + | export const {GET, POST} = handlers; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Lib ===== | ||
| + | Fichier utilitaire pour simplifier connexion et déconnexion dans '' | ||
| + | |||
| + | <sxh typescript> | ||
| + | 'use server'; | ||
| + | import {signIn, signOut} from " | ||
| + | |||
| + | |||
| + | interface LoginFormValues { | ||
| + | username: string; | ||
| + | password: string; | ||
| + | rememberMe?: | ||
| + | }; | ||
| + | export const submitLogin = async (formData: LoginFormValues): | ||
| + | await signIn(' | ||
| + | }; | ||
| + | |||
| + | export const submitLogout = async () => { | ||
| + | await signOut({redirect: | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ===== LoginForm ===== | ||
| + | Exemple de composant pour le login : | ||
| + | <sxh typescript> | ||
| + | 'use client'; | ||
| + | import {Button, Checkbox, Form, Input} from " | ||
| + | import {submitLogin} from " | ||
| + | |||
| + | |||
| + | export default function SignInComponent(){ | ||
| + | const onFinish = async (values: any) => { | ||
| + | await submitLogin(values); | ||
| + | }; | ||
| + | |||
| + | const onFinishFailed = (errorInfo: any) => { | ||
| + | console.log(' | ||
| + | }; | ||
| + | return | ||
| + | < | ||
| + | < | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | > | ||
| + | < | ||
| + | | ||
| + | | ||
| + | | ||
| + | > | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | | ||
| + | | ||
| + | | ||
| + | > | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | < | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Configuration ===== | ||
| + | |||
| + | Dans le fichier '' | ||
| + | |||
| + | <sxh env> | ||
| + | NEXTAUTH_URL=http:// | ||
| + | </ | ||
| + | |||
| + | |||
| + | Génération du secret pour encodage/ | ||
| + | |||
| + | <sxh bash> | ||
| + | npx auth secret | ||
| + | </ | ||
| + | |||
| + | Le **secret** est généré et inséré dans le fichier '' | ||
| + | ===== Usage ===== | ||
| + | Accès à la session nextAuth : | ||
| + | |||
| + | ==== Côté serveur ==== | ||
| + | |||
| + | <sxh ts; | ||
| + | import {auth} from " | ||
| + | |||
| + | const session= await auth(); | ||
| + | console.log(session.user); | ||
| + | </ | ||
| + | |||
| + | ==== Côté client ==== | ||
| + | |||
| + | <sxh ts; | ||
| + | 'use client'; | ||
| + | import {useSession} from " | ||
| + | |||
| + | const userData=useSession(); | ||
| + | console.log(userData.data? | ||
| </ | </ | ||