-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
59 lines (41 loc) · 1.4 KB
/
middleware.js
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
import { NextResponse } from 'next/server'
// const origin = 'http://avaloncliente-clusterip-srv/v1'
const origin = `http://localhost:5198/v1`
// const origin = `https://localhost:7150/v1`
export const config = {
matcher: ['/((?!api|_next/static|images|favicon.ico).*)',]
}
export default async function middleware(request) {
console.log('Destino: ' + request.nextUrl.pathname)
if(request.nextUrl.pathname =='/logout' || request.nextUrl.pathname =='/'){
return NextResponse.next();
}
if(request.nextUrl.pathname == '/deslogar'){
const res = NextResponse.redirect(new URL('/', request.url));
res.headers.set("logado", "false")
res.headers.set("permissoes", "[]")
res.cookies.delete('serpro-token');
return res;
}
const token = request.cookies.get('serpro-token')
if(!token) {
console.log('token não encontrado')
return NextResponse.next();
}
if(request.nextUrl.pathname == '/deslogar'){
const res = NextResponse.redirect(new URL('/', request.url));
res.headers.set("logado", "false")
res.headers.set("permissoes", "[]")
res.cookies.delete('serpro-token');
return res;
}
if(request.nextUrl.pathname =='/login') {
console.log('Acessando pagina de login mas com acesso')
return NextResponse.redirect(new URL('/', request.url));
}
return NextResponse.next({
headers: {
token: token.value
}
});
}