1import{r as o,j as e}from"./react-vendor-CS5KQH4z.js";import{s as n,u as O,v as W,C as l,b as d,c as u,d as x,e as K,D as $,be as Q,B as c,A as X,F as U,G as q,H as S,L as I,I as z,R as G,at as Y,au as Z,W as T,bf as ee}from"./index-BLypBI79.js";import{T as se,a as re,b as M,c as m,d as te,e as p}from"./table-CFMSOgdw.js";import{bp as ae,T as R,v as ne,bJ as ie,ay as oe,bG as ce}from"./ui-vendor-DHNdotUn.js";import{f as b}from"./format-BH31uaDc.js";import{f as _}from"./fr-C5MVRnC8.js";import"./query-vendor-D2Qiy2Hc.js";import"./supabase-vendor-Bpw_79gH.js";import"./radix-vendor-DbGz7Psx.js";import"./charts-vendor-CFO84u2G.js";import"./router-vendor-z0xy9EC6.js";const le="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let de=(t=21)=>{let r="",a=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)r+=le[a[t]&63];return r};async function ue(t){try{const{data:{user:r}}=await n.auth.getUser();if(!r)return{success:!1,error:"User not authenticated"};const a=`wat_${de(32)}`,{data:g,error:i}=await n.rpc("create_api_token",{token_name:t,token_plain:a});return i?(console.error("Error creating token:",i),{success:!1,error:"Failed to create token"}):g?{success:!0,token:a}:{success:!1,error:"Token creation failed"}}catch(r){return console.error("Unexpected error generating token:",r),{success:!1,error:"An unexpected error occurred"}}}async function xe(){try{const{data:{user:t}}=await n.auth.getUser();if(!t)return{success:!1,error:"User not authenticated"};const{data:r,error:a}=await n.from("api_tokens").select("*").eq("user_id",t.id).order("created_at",{ascending:!1});return a?(console.error("Error fetching tokens:",a),{success:!1,error:"Failed to fetch tokens"}):{success:!0,tokens:r||[]}}catch(t){return console.error("Unexpected error fetching tokens:",t),{success:!1,error:"An unexpected error occurred"}}}async function he(t){try{const{data:{user:r}}=await n.auth.getUser();if(!r)return{success:!1,error:"User not authenticated"};const{error:a}=await n.from("api_tokens").update({revoked_at:new Date().toISOString()}).eq("id",t).eq("user_id",r.id);return a?(console.error("Error revoking token:",a),{success:!1,error:"Failed to revoke token"}):{success:!0}}catch(r){return console.error("Unexpected error revoking token:",r),{success:!1,error:"An unexpected error occurred"}}}async function me(t){try{const{data:{user:r}}=await n.auth.getUser();if(!r)return{success:!1,error:"User not authenticated"};const{error:a}=await n.from("api_tokens").delete().eq("id",t).eq("user_id",r.id);return a?(console.error("Error deleting token:",a),{success:!1,error:"Failed to delete token"}):{success:!0}}catch(r){return console.error("Unexpected error deleting token:",r),{success:!1,error:"An unexpected error occurred"}}}async function pe(){try{const{data:{user:t}}=await n.auth.getUser();if(!t)return{success:!1,error:"User not authenticated"};const{data:r,error:a}=await n.from("v_user_api_tokens_stats").select("*").eq("user_id",t.id).single();return a&&a.code!=="PGRST116"?(console.error("Error fetching token stats:",a),{success:!1,error:"Failed to fetch statistics"}):{success:!0,stats:r||{total_tokens:0,active_tokens:0,revoked_tokens:0,last_api_usage:null,total_api_calls:0}}}catch(t){return console.error("Unexpected error fetching stats:",t),{success:!1,error:"An unexpected error occurred"}}}function Ce(){const{t}=O("pages");W(t("pageTitles.apiTokens"));const{toast:r}=ee(),[a,g]=o.useState([]),[i,F]=o.useState(null),[L,C]=o.useState(!0),[v,N]=o.useState(""),[j,A]=o.useState(null),[P,y]=o.useState(!1),[D,w]=o.useState(!1);o.useEffect(()=>{f(),k()},[]);const f=async()=>{C(!0);const s=await xe();s.success&&s.tokens?g(s.tokens):r({title:"Erreur",description:s.error||"Impossible de charger les tokens",variant:"destructive"}),C(!1)},k=async()=>{const s=await pe();s.success&&s.stats&&F(s.stats)},E=async()=>{if(!v.trim()){r({title:"Erreur",description:"Veuillez saisir un nom pour le token",variant:"destructive"});return}const s=await ue(v.trim());s.success&&s.token?(A(s.token),N(""),await f(),await k()):r({title:"Erreur",description:s.error||"Impossible de générer le token",variant:"destructive"})},H=async s=>{await navigator.clipboard.writeText(s),w(!0),r({title:"Copié !",description:"Le token a été copié dans le presse-papier"}),setTimeout(()=>w(!1),2e3)},B=async s=>{const h=await he(s);h.success?(r({title:"Token révoqué",description:"Le token a été révoqué avec succès"}),await f(),await k()):r({title:"Erreur",description:h.error||"Impossible de révoquer le token",variant:"destructive"})},V=async s=>{const h=await me(s);h.success?(r({title:"Token supprimé",description:"Le token a été supprimé définitivement"}),await f(),await k()):r({title:"Erreur",description:h.error||"Impossible de supprimer le token",variant:"destructive"})},J=()=>{y(!1),A(null),N(""),w(!1)};return e.jsxs("div",{className:"container mx-auto py-8 space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Tokens API"}),e.jsx("p",{className:"text-muted-foreground mt-2",children:"Gérez vos tokens d'accès à l'API WikiAgriTech Open Data"})]}),i&&e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsxs(l,{children:[e.jsx(d,{className:"pb-3",children:e.jsx(u,{className:"text-sm font-medium",children:"Total tokens"})}),e.jsx(x,{children:e.jsx("div",{className:"text-2xl font-bold",children:i.total_tokens})})]}),e.jsxs(l,{children:[e.jsx(d,{className:"pb-3",children:e.jsx(u,{className:"text-sm font-medium",children:"Tokens actifs"})}),e.jsx(x,{children:e.jsx("div",{className:"text-2xl font-bold text-green-600",children:i.active_tokens})})]}),e.jsxs(l,{children:[e.jsx(d,{className:"pb-3",children:e.jsx(u,{className:"text-sm font-medium",children:"Requêtes API"})}),e.jsx(x,{children:e.jsx("div",{className:"text-2xl font-bold",children:i.total_api_calls})})]}),e.jsxs(l,{children:[e.jsx(d,{className:"pb-3",children:e.jsx(u,{className:"text-sm font-medium",children:"Dernière utilisation"})}),e.jsx(x,{children:e.jsx("div",{className:"text-sm",children:i.last_api_usage?b(new Date(i.last_api_usage),"dd MMM yyyy",{locale:_}):"Jamais"})})]})]}),e.jsxs(l,{children:[e.jsxs(d,{children:[e.jsx(u,{children:"Mes tokens"}),e.jsx(K,{children:"Créez des tokens pour accéder à l'API WikiAgriTech. Chaque token est unique et traçable."})]}),e.jsx(x,{children:e.jsxs($,{open:P,onOpenChange:y,children:[e.jsx(Q,{asChild:!0,children:e.jsxs(c,{children:[e.jsx(ae,{className:"mr-2 h-4 w-4"}),"Générer un nouveau token"]})}),e.jsx(X,{className:"sm:max-w-[500px]",children:j?e.jsxs(e.Fragment,{children:[e.jsxs(U,{children:[e.jsx(q,{children:"Token généré !"}),e.jsx(S,{children:"Copiez ce token maintenant. Il ne sera plus jamais affiché."})]}),e.jsxs(Y,{className:"bg-yellow-50 border-yellow-200",children:[e.jsx(R,{className:"h-4 w-4 text-yellow-600"}),e.jsx(Z,{className:"text-yellow-800",children:"Conservez ce token en lieu sûr. Vous ne pourrez plus le voir après fermeture."})]}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(I,{children:"Votre token"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(z,{readOnly:!0,value:j,className:"font-mono text-sm"}),e.jsx(c,{size:"icon",variant:D?"default":"outline",onClick:()=>H(j),children:D?e.jsx(ne,{className:"h-4 w-4"}):e.jsx(ie,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{className:"bg-slate-50 p-4 rounded-md",children:[e.jsx("p",{className:"text-sm font-medium mb-2",children:"Exemple d'utilisation :"}),e.jsx("pre",{className:"text-xs bg-slate-900 text-slate-100 p-3 rounded overflow-x-auto",children:`curl -H "Authorization: Bearer ${j}" \\ 2 https://wiki-agri-tech.web.app/api/v1/outils`})]})]}),e.jsx(G,{children:e.jsx(c,{onClick:J,children:"J'ai copié mon token"})})]}):e.jsxs(e.Fragment,{children:[e.jsxs(U,{children:[e.jsx(q,{children:"Créer un nouveau token"}
2),e.jsx(S,{children:"Donnez un nom descriptif à votre token pour le reconnaître facilement."})]}),e.jsx("div",{className:"space-y-4 py-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(I,{htmlFor:"token-name",children:"Nom du token"}),e.jsx(z,{id:"token-name",placeholder:"Ex: Token Dev, Production API, etc.",value:v,onChange:s=>N(s.target.value),onKeyDown:s=>s.key==="Enter"&&E()})]})}),e.jsxs(G,{children:[e.jsx(c,{variant:"outline",onClick:()=>y(!1),children:"Annuler"}),e.jsx(c,{onClick:E,children:"Générer"})]})]})})]})})]}),e.jsxs(l,{children:[e.jsx(d,{children:e.jsx(u,{children:"Liste des tokens"})}),e.jsx(x,{children:L?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Chargement..."}):a.length===0?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Aucun token créé. Commencez par générer votre premier token."}):e.jsxs(se,{children:[e.jsx(re,{children:e.jsxs(M,{children:[e.jsx(m,{children:"Nom"}),e.jsx(m,{children:"Status"}),e.jsx(m,{children:"Créé le"}),e.jsx(m,{children:"Dernière utilisation"}),e.jsx(m,{className:"text-right",children:"Actions"})]})}),e.jsx(te,{children:a.map(s=>e.jsxs(M,{children:[e.jsx(p,{className:"font-medium",children:s.name}),e.jsx(p,{children:s.revoked_at?e.jsx(T,{variant:"destructive",children:"Révoqué"}):s.expires_at&&new Date(s.expires_at)<new Date?e.jsx(T,{variant:"secondary",children:"Expiré"}):e.jsx(T,{variant:"default",className:"bg-green-600",children:"Actif"})}),e.jsx(p,{children:b(new Date(s.created_at),"dd MMM yyyy",{locale:_})}),e.jsx(p,{children:s.last_used_at?b(new Date(s.last_used_at),"dd MMM yyyy HH:mm",{locale:_}):"Jamais"}),e.jsxs(p,{className:"text-right space-x-2",children:[!s.revoked_at&&e.jsxs(c,{size:"sm",variant:"outline",onClick:()=>B(s.id),children:[e.jsx(R,{className:"mr-1 h-3 w-3"}),"Révoquer"]}),e.jsxs(c,{size:"sm",variant:"destructive",onClick:()=>V(s.id),children:[e.jsx(oe,{className:"mr-1 h-3 w-3"}),"Supprimer"]})]})]},s.id))})]})})]}),e.jsxs(l,{children:[e.jsx(d,{children:e.jsxs(u,{className:"flex items-center gap-2",children:[e.jsx(ce,{className:"h-5 w-5"}),"Documentation API"]})}),e.jsxs(x,{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-medium mb-2",children:"Authentification"}),e.jsx("p",{className:"text-sm text-muted-foreground mb-2",children:"Utilisez le header Authorization avec votre token :"}),e.jsx("pre",{className:"text-xs bg-slate-900 text-slate-100 p-3 rounded overflow-x-auto",children:"Authorization: Bearer wat_xxxxxxxxxxxxxxxxxxxxx"})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-medium mb-2",children:"Endpoints disponibles"}),e.jsxs("ul",{className:"text-sm text-muted-foreground space-y-1 list-disc list-inside",children:[e.jsxs("li",{children:[e.jsx("code",{className:"text-xs bg-slate-100 px-1 rounded",children:"GET /api/v1/outils"})," - Liste des outils"]}),e.jsxs("li",{children:[e.jsx("code",{className:"text-xs bg-slate-100 px-1 rounded",children:"GET /api/v1/outils/:id"})," - Détail d'un outil"]}),e.jsxs("li",{children:[e.jsx("code",{className:"text-xs bg-slate-100 px-1 rounded",children:"GET /api/v1/entreprises"})," - Liste des entreprises"]}),e.jsxs("li",{children:[e.jsx("code",{className:"text-xs bg-slate-100 px-1 rounded",children:"GET /api/v1/taxonomies"})," - Référentiel taxonomies"]}),e.jsxs("li",{children:[e.jsx("code",{className:"text-xs bg-slate-100 px-1 rounded",children:"GET /api/v1/stats"})," - Statistiques globales"]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-medium mb-2",children:"Rate Limiting"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[e.jsx("strong",{children:"60 requêtes/minute"})," et ",e.jsx("strong",{children:"10 000 requêtes/jour"})," par utilisateur"]})]}),e.jsx("div",{children:e.jsx(c,{variant:"outline",asChild:!0,children:e.jsx("a",{href:"/fr/api-docs",target:"_blank",rel:"noopener noreferrer",children:"Voir la documentation complète"})})})]})]})]})}export{Ce as default}; 3//# sourceMappingURL=ApiTokensManagement-DGgbQjN-.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.