-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
51 lines (43 loc) · 1.48 KB
/
middleware.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
import { NextRequest, NextResponse } from "next/server";
import { cookies } from 'next/headers'
export async function middleware(request: NextRequest) {
// options.withCredentials = true;
// options.credentials = "include";
const public_urls = ['/login', '/signup']
try {
// // @ts-ignore
// const token = request.cookies;
const store = cookies()
console.log("--------------------------------------")
let data = (store.get("authToken")?.value);
console.log(data);
console.log("--------------------------------------")
// const cookie = `authToken=${token.value}`;
const response = await fetch('http://localhost:7089/me', {
credentials: 'same-origin',
// @ts-ignore
withCredentials: true,
method:"POST",
headers:{
"Cookie": `authToken=${data}`
}
})
if (public_urls.includes(request.nextUrl.pathname)) {
return NextResponse.redirect('/chat')
} else {
return
// NextResponse.next()
}
} catch (error) {
if (public_urls.includes(request.nextUrl.pathname)) {
return
// NextResponse.next()
} else {
return NextResponse.redirect(new URL('/login', request.nextUrl))
}
}
// if(response.ok){
// }else{
// }
// return NextResponse.redirect(new URL('/home', request.url))
}