1 line
8 KiB
JavaScript
1 line
8 KiB
JavaScript
import{a as c,j as e}from"./vendor-react-BHG7lGYR.js";import{a as x,p as v,l as g,L as p,B as N,C as I,b as E,c as F,v as M,e as P}from"./index-BWcMVITa.js";import{B as S}from"./badge-CPAgRIoX.js";import{C}from"./confirmation-dialog-D8kbmfRa.js";import{p as O,_ as U,aB as B,aD as $,T as z,q,aE as G,aF as H,e as Y}from"./vendor-icons-DaGlTw4_.js";import"./vendor-IYr-MHu4.js";import"./vendor-router-D-s5vIeO.js";import"./vendor-tanstack-BzWBL1hV.js";import"./vendor-utils-CgOSfOkx.js";function V(t){const s=t.toLowerCase(),i={deviceType:"unknown",os:"Unknown",browser:"Unknown"};if(s.includes("mobile")||s.includes("android")||s.includes("iphone")?i.deviceType="mobile":s.includes("tablet")||s.includes("ipad")?i.deviceType="tablet":i.deviceType="desktop",s.includes("windows")){i.os="Windows";const r=s.match(/windows nt (\d+\.\d+)/);if(r){const o=r[1];o==="10.0"?i.osVersion="10":o==="6.3"?i.osVersion="8.1":o==="6.2"?i.osVersion="8":o==="6.1"?i.osVersion="7":i.osVersion=o}}else if(s.includes("mac os x")||s.includes("macintosh")){i.os="macOS";const r=s.match(/mac os x (\d+[._]\d+)/);r&&(i.osVersion=r[1].replace("_","."))}else if(s.includes("linux"))i.os="Linux";else if(s.includes("android")){i.os="Android";const r=s.match(/android (\d+\.\d+)/);r&&(i.osVersion=r[1])}else if(s.includes("ios")||s.includes("iphone")||s.includes("ipad")){i.os="iOS";const r=s.match(/os (\d+[._]\d+)/);r&&(i.osVersion=r[1].replace("_","."))}if(s.includes("edg/")){i.browser="Microsoft Edge";const r=s.match(/edg\/(\d+\.\d+)/);r&&(i.browserVersion=r[1])}else if(s.includes("chrome/")&&!s.includes("edg/")){i.browser="Chrome";const r=s.match(/chrome\/(\d+\.\d+)/);r&&(i.browserVersion=r[1])}else if(s.includes("firefox/")){i.browser="Firefox";const r=s.match(/firefox\/(\d+\.\d+)/);r&&(i.browserVersion=r[1])}else if(s.includes("safari/")&&!s.includes("chrome/")){i.browser="Safari";const r=s.match(/version\/(\d+\.\d+)/);r&&(i.browserVersion=r[1])}else if(s.includes("opera/")||s.includes("opr/")){i.browser="Opera";const r=s.match(/(?:opera|opr)\/(\d+\.\d+)/);r&&(i.browserVersion=r[1])}if(s.includes("iphone"))s.match(/iphone\s+os\s+(\d+[._]\d+)/)&&(i.deviceModel="iPhone");else if(s.includes("ipad"))i.deviceModel="iPad";else if(s.includes("android")){const r=s.match(/android.*?;\s*([^)]+)\)/);r&&(i.deviceModel=r[1].trim())}return i}function J(t){const s=[];return t.deviceModel&&s.push(t.deviceModel),s.push(t.os),t.osVersion&&s.push(t.osVersion),s.push(t.browser),t.browserVersion&&s.push(t.browserVersion),s.join(" • ")}function K(t){if(!t)return"Unknown location";const s=[];return t.city&&s.push(t.city),t.region&&s.push(t.region),t.country&&s.push(t.country),s.length>0?s.join(", "):"Unknown location"}function Q(t){return t==="127.0.0.1"||t==="localhost"||t.startsWith("192.168.")||t.startsWith("10.")||t.startsWith("172.16.")||t.startsWith("172.17.")||t.startsWith("172.18.")||t.startsWith("172.19.")||t.startsWith("172.20.")||t.startsWith("172.21.")||t.startsWith("172.22.")||t.startsWith("172.23.")||t.startsWith("172.24.")||t.startsWith("172.25.")||t.startsWith("172.26.")||t.startsWith("172.27.")||t.startsWith("172.28.")||t.startsWith("172.29.")||t.startsWith("172.30.")||t.startsWith("172.31.")}function oe(){const[t,s]=c.useState([]),[i,r]=c.useState(!0),[o,w]=c.useState(null),[h,j]=c.useState(!1),[y,l]=c.useState(null),[d,m]=c.useState(null),[W,u]=c.useState(!1);c.useEffect(()=>{f()},[]);const f=async()=>{try{r(!0),l(null);const a=await x.get("/auth/sessions");s(a.data.sessions)}catch(a){const n=v(a);g.error("Failed to fetch sessions",{message:n.message}),l(n.message)}finally{r(!1)}},_=a=>{m(a)},R=async()=>{if(d)try{w(d),l(null),await x.delete(`/auth/sessions/${d}`),await f(),m(null)}catch(a){const n=v(a);g.error("Failed to revoke session",{message:n.message,sessionId:d}),l(n.message)}finally{w(null)}},L=()=>{u(!0)},A=async()=>{try{j(!0),l(null),await x.delete("/auth/sessions"),await f(),u(!1)}catch(a){const n=v(a);g.error("Failed to revoke sessions",{message:n.message}),l(n.message)}finally{j(!1)}},T=a=>{switch(a){case"mobile":return e.jsx(Y,{className:"h-5 w-5"});case"tablet":return e.jsx(H,{className:"h-5 w-5"});case"desktop":return e.jsx(G,{className:"h-5 w-5"});default:return e.jsx(q,{className:"h-5 w-5"})}},D=t.map(a=>{const n=V(a.user_agent);return{...a,device_info:n,location_info:Q(a.ip_address)?{country:"Local",region:"Network",city:"Private IP"}:null}}),b=a=>{const n=new Date(a);return new Intl.DateTimeFormat("en-US",{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"}).format(n)};return i?e.jsx("div",{className:"flex items-center justify-center min-h-[400px]",children:e.jsx(p,{})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold tracking-tight",children:"Active Sessions"}),e.jsx("p",{className:"text-muted-foreground",children:"Manage your active sessions and sign out from other devices"})]}),y&&e.jsx("div",{className:"bg-destructive/10 text-destructive px-4 py-4 rounded-md",children:y}),e.jsx("div",{className:"flex justify-end",children:e.jsx(N,{onClick:L,disabled:h||t.length<=1,variant:"destructive",children:h?e.jsxs(e.Fragment,{children:[e.jsx(p,{className:"mr-2 h-4 w-4"}),"Revoking..."]}):e.jsxs(e.Fragment,{children:[e.jsx(O,{className:"mr-2 h-4 w-4"}),"Revoke All Other Sessions"]})})}),e.jsxs(I,{children:[e.jsxs(E,{children:[e.jsxs(F,{className:"flex items-center gap-2",children:[e.jsx(U,{className:"h-5 w-5"}),"Sessions (",t.length,")"]}),e.jsx(M,{children:"These are the devices where you're currently signed in"})]}),e.jsx(P,{children:t.length===0?e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"No active sessions found."}):e.jsx("div",{className:"space-y-4",children:D.map(a=>{const n=a.device_info||V(a.user_agent),k=a.location_info;return e.jsxs("div",{className:`flex items-center justify-between p-4 border rounded-lg ${a.is_current?"border-primary bg-primary/5":"border-border"}`,children:[e.jsxs("div",{className:"flex items-start gap-4 flex-1",children:[e.jsx("div",{className:"mt-1",children:T(n.deviceType)}),e.jsxs("div",{className:"flex-1 space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("p",{className:"font-medium",children:J(n)}),a.is_current&&e.jsx(S,{variant:"default",className:"bg-primary text-primary-foreground",children:"Current Session"}),e.jsx(S,{variant:"secondary",className:"capitalize",children:n.deviceType})]}),e.jsxs("div",{className:"flex flex-wrap gap-4 text-sm text-muted-foreground",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(B,{className:"h-4 w-4"}),e.jsx("span",{children:a.ip_address||"Unknown IP"})]}),k&&e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx($,{className:"h-4 w-4"}),e.jsx("span",{children:K(k)})]}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:"Created:"})," ",b(a.created_at)]}),a.last_activity&&e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:"Last activity:"})," ",b(a.last_activity)]})]}),n.browserVersion&&e.jsxs("div",{className:"text-xs text-muted-foreground",children:[n.browser," ",n.browserVersion,n.osVersion&&` • ${n.os} ${n.osVersion}`]})]})]}),!a.is_current&&e.jsx(N,{onClick:()=>_(a.id),disabled:o===a.id,variant:"destructive",size:"sm",children:o===a.id?e.jsxs(e.Fragment,{children:[e.jsx(p,{className:"mr-2 h-4 w-4"}),"Revoking..."]}):e.jsxs(e.Fragment,{children:[e.jsx(z,{className:"mr-2 h-4 w-4"}),"Revoke"]})})]},a.id)})})})]}),e.jsx(C,{open:!!d,onClose:()=>m(null),onConfirm:R,title:"Revoke Session",description:"Are you sure you want to revoke this session? The user will be logged out from this device.",confirmLabel:"Revoke",cancelLabel:"Cancel",variant:"destructive",isLoading:!!o}),e.jsx(C,{open:W,onClose:()=>u(!1),onConfirm:A,title:"Revoke All Other Sessions",description:"Are you sure you want to revoke all other sessions? You will remain logged in on this device, but all other devices will be logged out.",confirmLabel:"Revoke All",cancelLabel:"Cancel",variant:"destructive",isLoading:h})]})}export{oe as SessionsPage,oe as default};
|