-
Notifications
You must be signed in to change notification settings - Fork 375
/
logout.ts
133 lines (124 loc) · 3.82 KB
/
logout.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { NextApiResponse, NextApiRequest } from 'next';
import { NextRequest, NextResponse } from 'next/server';
import { HandleLogin as BaseHandleLogin, HandleLogout as BaseHandleLogout } from '../auth0-session';
import { assertReqRes } from '../utils/assert';
import { HandlerErrorCause, LogoutHandlerError } from '../utils/errors';
import { Auth0NextApiRequest, Auth0NextApiResponse, Auth0NextRequest, Auth0NextResponse } from '../http';
import { AppRouteHandlerFnContext, AuthHandler, Handler, getHandler, OptionsProvider } from './router-helpers';
/**
* Options to customize the logout handler.
*
* @see {@link HandleLogout}
*
* @category Server
*/
export interface LogoutOptions {
/**
* URL to return to after logout. Overrides the default
* in {@link BaseConfig.routes.postLogoutRedirect routes.postLogoutRedirect}.
*/
returnTo?: string;
/**
* Additional custom parameters to pass to the logout endpoint.
*/
logoutParams?: { [key: string]: any };
}
/**
* Options provider for the default logout handler.
* Use this to generate options that depend on values from the request.
*
* @category Server
*/
export type LogoutOptionsProvider = OptionsProvider<LogoutOptions>;
/**
* Use this to customize the default logout handler without overriding it.
* You can still override the handler if needed.
*
* @example Pass an options object
*
* ```js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogout } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
* logout: handleLogout({ returnTo: 'https://example.com' })
* });
* ```
*
* @example Pass a function that receives the request and returns an options object
*
* ```js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogout } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
* logout: handleLogout((req) => {
* return { returnTo: 'https://example.com' };
* })
* });
* ```
*
* This is useful for generating options that depend on values from the request.
*
* @example Override the logout handler
*
* ```js
* import { handleAuth, handleLogout } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
* logout: async (req, res) => {
* try {
* await handleLogout(req, res, {
* returnTo: 'https://example.com'
* });
* } catch (error) {
* console.error(error);
* }
* }
* });
* ```
*
* @category Server
*/
export type HandleLogout = AuthHandler<LogoutOptions>;
/**
* The handler for the `/api/auth/logout` API route.
*
* @throws {@link HandlerError}
*
* @category Server
*/
export type LogoutHandler = Handler<LogoutOptions>;
/**
* @ignore
*/
export default function handleLogoutFactory(handler: BaseHandleLogout): HandleLogout {
const appRouteHandler = appRouteHandlerFactory(handler);
const pageRouteHandler = pageRouteHandlerFactory(handler);
return getHandler<LogoutOptions>(appRouteHandler, pageRouteHandler) as HandleLogout;
}
const appRouteHandlerFactory: (
handler: BaseHandleLogin
) => (req: NextRequest, ctx: AppRouteHandlerFnContext, options?: LogoutOptions) => Promise<Response> | Response =
(handler) =>
async (req, _ctx, options = {}) => {
try {
const auth0Res = new Auth0NextResponse(new NextResponse());
await handler(new Auth0NextRequest(req), auth0Res, options);
return auth0Res.res;
} catch (e) {
throw new LogoutHandlerError(e as HandlerErrorCause);
}
};
const pageRouteHandlerFactory: (
handler: BaseHandleLogin
) => (req: NextApiRequest, res: NextApiResponse, options?: LogoutOptions) => Promise<void> | void =
(handler) =>
async (req, res, options = {}) => {
try {
assertReqRes(req, res);
return await handler(new Auth0NextApiRequest(req), new Auth0NextApiResponse(res), options);
} catch (e) {
throw new LogoutHandlerError(e as HandlerErrorCause);
}
};