* init p2p polling app
This commit is contained in:
24
server/middleware/uuid.ts
Normal file
24
server/middleware/uuid.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
// 1. Check if the cookie already exists
|
||||
const cookie = getCookie(event, 'user_guid');
|
||||
|
||||
// 2. If it doesn't exist, generate and set it
|
||||
if (!cookie) {
|
||||
const newUuid = uuidv4();
|
||||
|
||||
setCookie(event, 'user_guid', newUuid, {
|
||||
maxAge: 60 * 60 * 24 * 7, // 1 week
|
||||
path: '/',
|
||||
// httpOnly: true, // Set to true if you DON'T need to read it in Vue/JS
|
||||
sameSite: 'lax',
|
||||
});
|
||||
|
||||
// 3. Inject it into the context so it's available
|
||||
// to other server routes/plugins during this same request
|
||||
event.context.userGuid = newUuid;
|
||||
} else {
|
||||
event.context.userGuid = cookie;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user