@@ -5,60 +5,66 @@ import { prisma } from "@/lib/prisma";
5
5
import { NextRequest , NextResponse } from "next/server" ;
6
6
7
7
export async function POST ( request : NextRequest ) {
8
- const body = ( await request . json ( ) ) as {
9
- returnUrl ?: string ;
10
- idTokenHint ?: string ;
11
- clientId ?: string ;
12
- postLogoutRedirectUri ?: string ;
13
- state ?: string ;
14
- } ;
15
- const { returnUrl , idTokenHint , clientId , postLogoutRedirectUri , state } =
16
- body ;
8
+ try {
9
+ const body = ( await request . json ( ) ) as {
10
+ returnUrl ?: string ;
11
+ idTokenHint ?: string ;
12
+ clientId ?: string ;
13
+ postLogoutRedirectUri ?: string ;
14
+ state ?: string ;
15
+ } ;
16
+ const { returnUrl , idTokenHint , state } = body ;
17
17
18
- const wellKnownResponse = await fetch (
19
- `${ configuration . portal . issuer } /.well-known/openid-configuration`
20
- ) ;
18
+ const wellKnownResponse = await fetch (
19
+ `${ configuration . portal . issuer } /.well-known/openid-configuration`
20
+ ) ;
21
21
22
- const wellKnown = ( await wellKnownResponse . json ( ) ) as {
23
- issuer : string ;
24
- authorization_endpoint : string ;
25
- token_endpoint : string ;
26
- userinfo_endpoint : string ;
27
- end_session_endpoint : string ;
28
- } ;
22
+ const wellKnown = ( await wellKnownResponse . json ( ) ) as {
23
+ issuer : string ;
24
+ authorization_endpoint : string ;
25
+ token_endpoint : string ;
26
+ userinfo_endpoint : string ;
27
+ end_session_endpoint : string ;
28
+ } ;
29
29
30
- if ( wellKnownResponse . status !== 200 ) {
31
- throw { code : wellKnownResponse . status , details : wellKnown } ;
32
- }
30
+ if ( wellKnownResponse . status !== 200 ) {
31
+ throw { code : wellKnownResponse . status , details : wellKnown } ;
32
+ }
33
33
34
- const params : {
35
- id_token_hint ?: string ;
36
- client_id ?: string ;
37
- post_logout_redirect_uri ?: string ;
38
- state ?: string ;
39
- } = {
40
- id_token_hint : idTokenHint ,
41
- client_id : clientId ,
42
- post_logout_redirect_uri : postLogoutRedirectUri ,
43
- state : state ,
44
- } ;
34
+ const params : {
35
+ client_id ?: string ;
36
+ post_logout_redirect_uri ?: string ;
37
+ id_token_hint ?: string ;
38
+ state ?: string ;
39
+ } = {
40
+ client_id : configuration . portal . clientId ,
41
+ post_logout_redirect_uri : configuration . portal . postLogoutRedirectUri ,
42
+ } ;
45
43
46
- if ( idTokenHint ) {
47
- await prisma . session . updateMany ( {
48
- where : {
49
- idToken : idTokenHint ,
50
- } ,
51
- data : {
52
- deletedAt : new Date ( ) ,
53
- } ,
54
- } ) ;
55
- }
44
+ if ( idTokenHint ) params . id_token_hint = idTokenHint ;
45
+ if ( state ) params . state = state ;
56
46
57
- const endSessionUrl = `${
58
- wellKnown . end_session_endpoint
59
- } ?${ new URLSearchParams ( params ) . toString ( ) } `;
47
+ if ( idTokenHint ) {
48
+ await prisma . session . updateMany ( {
49
+ where : {
50
+ idToken : idTokenHint ,
51
+ } ,
52
+ data : {
53
+ deletedAt : new Date ( ) ,
54
+ } ,
55
+ } ) ;
56
+ }
60
57
61
- if ( returnUrl ) setShortLiveCookie ( returnUrlCookieName , returnUrl ) ;
58
+ const endSessionUrl = `${
59
+ wellKnown . end_session_endpoint
60
+ } ?${ new URLSearchParams ( params ) . toString ( ) } `;
62
61
63
- return NextResponse . json ( { endSessionUrl } ) ;
62
+ if ( returnUrl ) setShortLiveCookie ( returnUrlCookieName , returnUrl ) ;
63
+
64
+ return NextResponse . json ( { endSessionUrl } ) ;
65
+ } catch ( error : any ) {
66
+ return NextResponse . json ( error . details || { message : error . message } , {
67
+ status : error . code ,
68
+ } ) ;
69
+ }
64
70
}
0 commit comments