/
implementation.ts
84 lines (77 loc) · 2.82 KB
/
implementation.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
import { APIInterface, APIOptions, VerifySessionOptions } from "../";
import { normaliseHttpMethod } from "../../../utils";
import NormalisedURLPath from "../../../normalisedURLPath";
import { SessionContainerInterface } from "../types";
import { GeneralErrorResponse, UserContext } from "../../../types";
import { getSessionFromRequest, refreshSessionInRequest } from "../sessionRequestFunctions";
export default function getAPIInterface(): APIInterface {
return {
refreshPOST: async function ({
options,
userContext,
}: {
options: APIOptions;
userContext: UserContext;
}): Promise<SessionContainerInterface> {
return refreshSessionInRequest({
req: options.req,
res: options.res,
userContext,
config: options.config,
recipeInterfaceImpl: options.recipeImplementation,
});
},
verifySession: async function ({
verifySessionOptions,
options,
userContext,
}: {
verifySessionOptions: VerifySessionOptions | undefined;
options: APIOptions;
userContext: UserContext;
}): Promise<SessionContainerInterface | undefined> {
let method = normaliseHttpMethod(options.req.getMethod());
if (method === "options" || method === "trace") {
return undefined;
}
let incomingPath = new NormalisedURLPath(options.req.getOriginalURL());
let refreshTokenPath = options.config.refreshTokenPath;
if (incomingPath.equals(refreshTokenPath) && method === "post") {
return refreshSessionInRequest({
req: options.req,
res: options.res,
userContext,
config: options.config,
recipeInterfaceImpl: options.recipeImplementation,
});
} else {
return getSessionFromRequest({
req: options.req,
res: options.res,
options: verifySessionOptions,
config: options.config,
recipeInterfaceImpl: options.recipeImplementation,
userContext,
});
}
},
signOutPOST: async function ({
session,
userContext,
}: {
options: APIOptions;
session: SessionContainerInterface;
userContext: UserContext;
}): Promise<
| {
status: "OK";
}
| GeneralErrorResponse
> {
await session.revokeSession(userContext);
return {
status: "OK",
};
},
};
}