| 1 |
import { useState, useEffect } from 'react' |
| 2 |
import { useConfig } from '../auth' |
| 3 |
import * as allauth from '../lib/allauth' |
| 4 |
import Button from '../components/Button' |
| 5 |
|
| 6 |
export default function Sessions () { |
| 7 |
const config = useConfig() |
| 8 |
const [sessions, setSessions] = useState([]) |
| 9 |
const [response, setResponse] = useState({ fetching: false, content: { status: 200, data: [] } }) |
| 10 |
|
| 11 |
useEffect(() => { |
| 12 |
setResponse((r) => { return { ...r, fetching: true } }) |
| 13 |
allauth.getSessions().then((resp) => { |
| 14 |
if (resp.status === 200) { |
| 15 |
setSessions(resp.data) |
| 16 |
} |
| 17 |
}).then(() => { |
| 18 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 19 |
}) |
| 20 |
}, []) |
| 21 |
|
| 22 |
const otherSessions = sessions.filter(session => !session.is_current) |
| 23 |
function logout (sessions) { |
| 24 |
setResponse({ ...response, fetching: true }) |
| 25 |
allauth.endSessions(sessions.map(s => s.id)).then((resp) => { |
| 26 |
setResponse((r) => { return { ...r, content: resp } }) |
| 27 |
if (resp.status === 200) { |
| 28 |
setSessions(resp.data) |
| 29 |
} |
| 30 |
}).catch((e) => { |
| 31 |
console.error(e) |
| 32 |
window.alert(e) |
| 33 |
}).then(() => { |
| 34 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
return ( |
| 39 |
<div> |
| 40 |
<h1>Sessions</h1> |
| 41 |
|
| 42 |
<table className='table table-bordered'> |
| 43 |
<thead> |
| 44 |
<tr> |
| 45 |
<th>Started At</th> |
| 46 |
<th>IP Address</th> |
| 47 |
<th>Browser</th> |
| 48 |
{config.data.usersessions.track_activity ? <th>Last Seen At</th> : null} |
| 49 |
<th>Current</th> |
| 50 |
<th>Actions</th> |
| 51 |
</tr> |
| 52 |
</thead> |
| 53 |
<tbody> |
| 54 |
{sessions.map((session, i) => { |
| 55 |
return ( |
| 56 |
<tr key={i}> |
| 57 |
<td>{new Date(session.created_at * 1000).toLocaleString()}</td> |
| 58 |
<td>{session.ip}</td> |
| 59 |
<td>{session.user_agent}</td> |
| 60 |
{config.data.usersessions.track_activity ? <td>{session.last_seen_at}</td> : null} |
| 61 |
<td>{session.is_current ? '⭐' : ''}</td> |
| 62 |
<td><Button onClick={() => logout([session])}>Logout</Button></td> |
| 63 |
</tr> |
| 64 |
) |
| 65 |
})} |
| 66 |
</tbody> |
| 67 |
</table> |
| 68 |
|
| 69 |
<Button disabled={otherSessions.length <= 1} onClick={() => logout(otherSessions)}>Logout elsewhere</Button> |
| 70 |
|
| 71 |
</div> |
| 72 |
) |
| 73 |
} |