forked from passageidentity/passage-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestServer.ts
More file actions
41 lines (35 loc) · 1.16 KB
/
testServer.ts
File metadata and controls
41 lines (35 loc) · 1.16 KB
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
import psg from "./src/index";
import express, { NextFunction } from "express";
import { PassageConfig } from "./src/types/PassageConfig";
const app = express();
require("dotenv").config();
const passageConfig: PassageConfig = {
appID: process.env.APP_ID ? process.env.APP_ID : "",
apiKey: process.env.API_KEY ? process.env.API_KEY : "",
authStrategy: "COOKIE",
};
// example of custom middleware
const passage = new psg(passageConfig);
const customMiddleware = (() => {
return async (req: any, res: any, next: NextFunction) => {
try {
const userID = await passage.authenticateRequest(req);
if (userID) res.userID = userID;
else res.userID = false;
next();
} catch (e) {
res.status(401).send("Could not authenticate user!");
}
};
})();
// example implementation of custom middleware
app.get("/", customMiddleware, async (req: Request, res: any) => {
const userID = res.userID;
if (userID) {
const { email }: any = await passage.user.get(userID);
res.json({ email });
} else {
res.send("Failed to get user");
}
});
export default app;