Disconnect unactive users
This commit is contained in:
@@ -82,6 +82,7 @@ export default function CreateForm({
|
|||||||
}
|
}
|
||||||
|
|
||||||
await handleSubmit({
|
await handleSubmit({
|
||||||
|
userId: session.user.userId,
|
||||||
accessToken: session.user.accessToken,
|
accessToken: session.user.accessToken,
|
||||||
content: formattedContent,
|
content: formattedContent,
|
||||||
collection,
|
collection,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import Divider from '@mui/material/Divider'
|
|||||||
import Box from '@mui/material/Box'
|
import Box from '@mui/material/Box'
|
||||||
import {readItems, withToken} from '@directus/sdk'
|
import {readItems, withToken} from '@directus/sdk'
|
||||||
import LogoutCountdown from '../session/logout-countdown.js'
|
import LogoutCountdown from '../session/logout-countdown.js'
|
||||||
import {directusClient} from '@/lib/directus.js'
|
import {directusClient, handleUserStatus} from '@/lib/directus.js'
|
||||||
import {formatDate} from '@/lib/format.js'
|
import {formatDate} from '@/lib/format.js'
|
||||||
|
|
||||||
const commentsPerPage = process.env.NEXT_PUBLIC_COMMENTS_PER_PAGE || 2
|
const commentsPerPage = process.env.NEXT_PUBLIC_COMMENTS_PER_PAGE || 2
|
||||||
@@ -30,6 +30,8 @@ export default function ListComments({session, selectedTitre, isOpen, setIsOpen,
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchComments() {
|
async function fetchComments() {
|
||||||
try {
|
try {
|
||||||
|
await handleUserStatus(session.user.accessToken, session.user.userId)
|
||||||
|
|
||||||
const result = await directusClient.request(withToken(
|
const result = await directusClient.request(withToken(
|
||||||
session.user.accessToken,
|
session.user.accessToken,
|
||||||
readItems('commentaires', {
|
readItems('commentaires', {
|
||||||
@@ -73,7 +75,7 @@ export default function ListComments({session, selectedTitre, isOpen, setIsOpen,
|
|||||||
<Dialog open={isOpen} onClose={handleClose}>
|
<Dialog open={isOpen} onClose={handleClose}>
|
||||||
<DialogTitle>Commentaires</DialogTitle>
|
<DialogTitle>Commentaires</DialogTitle>
|
||||||
<List sx={{width: '100%', maxWidth: 360, bgcolor: 'background.paper'}}>
|
<List sx={{width: '100%', maxWidth: 360, bgcolor: 'background.paper'}}>
|
||||||
{selectedComments && selectedComments.length > 0 ? selectedComments.map(({id, date_created, text, user_created}) => (
|
{selectedComments && selectedComments.length > 0 ? selectedComments.map(({id, date_created, contenu, user_created}) => (
|
||||||
<React.Fragment key={id}>
|
<React.Fragment key={id}>
|
||||||
<ListItem alignItems='flex-start'>
|
<ListItem alignItems='flex-start'>
|
||||||
<ListItemText
|
<ListItemText
|
||||||
@@ -90,7 +92,7 @@ export default function ListComments({session, selectedTitre, isOpen, setIsOpen,
|
|||||||
variant='body2'
|
variant='body2'
|
||||||
color='text.primary'
|
color='text.primary'
|
||||||
>
|
>
|
||||||
{text}
|
{contenu}
|
||||||
</Typography>
|
</Typography>
|
||||||
<br />
|
<br />
|
||||||
{formatDate(date_created, 'PPPPpp')}
|
{formatDate(date_created, 'PPPPpp')}
|
||||||
|
|||||||
+22
-1
@@ -1,6 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
createDirectus, rest, authentication, withToken, createItem
|
createDirectus, rest, authentication, withToken, createItem,
|
||||||
|
readUser
|
||||||
} from '@directus/sdk'
|
} from '@directus/sdk'
|
||||||
|
import {signOut} from 'next-auth/react'
|
||||||
import {hasRestrictedChar} from './format.js'
|
import {hasRestrictedChar} from './format.js'
|
||||||
|
|
||||||
const apiUrl = process.env.DIRECTUS_API_URL || process.env.NEXT_PUBLIC_DIRECTUS_API_URL
|
const apiUrl = process.env.DIRECTUS_API_URL || process.env.NEXT_PUBLIC_DIRECTUS_API_URL
|
||||||
@@ -9,7 +11,24 @@ export const directusClient = createDirectus(apiUrl)
|
|||||||
.with(authentication('cookie', {credentials: 'include', autoRefresh: true}))
|
.with(authentication('cookie', {credentials: 'include', autoRefresh: true}))
|
||||||
.with(rest())
|
.with(rest())
|
||||||
|
|
||||||
|
export async function handleUserStatus(accessToken, userId) {
|
||||||
|
const {status} = await directusClient.request(
|
||||||
|
withToken(
|
||||||
|
accessToken,
|
||||||
|
readUser(userId, {
|
||||||
|
fields: 'status'
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (status !== 'active') {
|
||||||
|
signOut()
|
||||||
|
throw new Error('Déconnexion')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function handleSubmit({
|
export async function handleSubmit({
|
||||||
|
userId,
|
||||||
accessToken,
|
accessToken,
|
||||||
content,
|
content,
|
||||||
collection,
|
collection,
|
||||||
@@ -27,6 +46,8 @@ export async function handleSubmit({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await handleUserStatus(accessToken, userId)
|
||||||
|
|
||||||
await directusClient.request(
|
await directusClient.request(
|
||||||
withToken(
|
withToken(
|
||||||
accessToken,
|
accessToken,
|
||||||
|
|||||||
Reference in New Issue
Block a user