-
Notifications
You must be signed in to change notification settings - Fork 15
/
auth.ts
64 lines (56 loc) · 1.73 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use server"
//@ts-ignore
import { v4 as uuidv4 } from 'uuid';
import { cookies } from 'next/headers'
interface Session {
user: {
/** The user's id. */
id: string
turnCount?: number
}
}
const expireTime = 3 * 60 * 60 * 1000 // 3 hour
export async function auth(force: boolean = false) {
const cookiesList = cookies()
const hasCookie = cookiesList.has('userId')
if (!force && hasCookie) {
const userId = cookiesList.get('userId')?.value
return {user: {id: userId}}
} else {
const userId = uuidv4()
cookiesList.set('userId', userId, { expires: Date.now() + expireTime })
cookiesList.set('turnCount', '0', { expires: Date.now() + expireTime })
console.info('create session', userId);
return {user: {id: userId}}
}
}
export async function setTurnCount(turnCount: number) {
const cookiesList = cookies()
const hasCookie = cookiesList.has('userId')
if (hasCookie) {
const userId = cookiesList.get('userId')?.value
cookiesList.set('turnCount', turnCount.toString(), { expires: Date.now() + expireTime })
console.log('set turn count', userId, turnCount);
}
return true
};
export async function getTurnCount() {
const cookiesList = cookies()
const hasCookie = cookiesList.has('userId')
if (hasCookie) {
const turnCount = parseInt(cookiesList.get('turnCount')?.value || '0')
const userId = cookiesList.get('userId')?.value
console.log('get turn count', userId, turnCount);
return turnCount
}
};
export async function clear() {
const cookiesList = cookies()
const hasCookie = cookiesList.has('userId')
if (hasCookie) {
const userId = cookiesList.get('userId')?.value
console.info('clear session', userId);
cookies().delete('userId')
}
return true
}