|
| 1 | +--- |
| 2 | +title: Multiple Issuer |
| 3 | +--- |
| 4 | + |
| 5 | +```typescript |
| 6 | +import Elysia from "elysia"; |
| 7 | +import { OidcClient } from "../src"; |
| 8 | +import { SQLiteAdapter } from "../src/dataAdapters/SQLiteAdapter"; |
| 9 | +// import { OidcClient } from "elysia-openid-client"; |
| 10 | +// import { SQLiteAdapter } from "elysia-openid-client/dataAdapters/SQLiteAdapter"; |
| 11 | + |
| 12 | +const baseUrl = "https://app.example.com"; |
| 13 | + |
| 14 | +// Use the same data adapter for all issuers |
| 15 | +const dataAdapter = new SQLiteAdapter(); |
| 16 | + |
| 17 | +// Callback URL: `https://app.example.com/auth/callback` |
| 18 | +const rp1 = await OidcClient.create({ |
| 19 | + baseUrl, |
| 20 | + issuerUrl: "https://issuer.exmaple.com", |
| 21 | + clientMetadata: { |
| 22 | + client_id: "client-id", |
| 23 | + client_secret: "client-secret", |
| 24 | + }, |
| 25 | + dataAdapter, |
| 26 | +}); |
| 27 | +const endpoints1 = rp1.getEndpoints(); |
| 28 | + |
| 29 | +console.log(rp1.issuerMetadata); |
| 30 | + |
| 31 | +// Callback URL: `https://app.example.com/another/callback` |
| 32 | +const rp2 = await OidcClient.create({ |
| 33 | + baseUrl, |
| 34 | + issuerUrl: "https://another-issuer.exmaple.com", |
| 35 | + clientMetadata: { |
| 36 | + client_id: "another-client-id", |
| 37 | + client_secret: "another-client-secret", |
| 38 | + }, |
| 39 | + dataAdapter, |
| 40 | + settings: { |
| 41 | + pathPrefix: "/another", |
| 42 | + }, |
| 43 | +}); |
| 44 | +const endpoints2 = rp2.getEndpoints(); |
| 45 | + |
| 46 | +console.log(rp2.issuerMetadata); |
| 47 | + |
| 48 | +// No matter which RP hook is used |
| 49 | +const hook = rp1.getAuthHook({ |
| 50 | + loginRedirectUrl: "/select", |
| 51 | +}); |
| 52 | + |
| 53 | +new Elysia() |
| 54 | + .use(endpoints1) |
| 55 | + .use(endpoints2) |
| 56 | + .guard((app) => |
| 57 | + app |
| 58 | + .use(hook) |
| 59 | + .get("/", ({ sessionStatus }) => |
| 60 | + sessionStatus ? "Logged in" : "Restricted", |
| 61 | + ) |
| 62 | + .get("/status", ({ sessionStatus }) => sessionStatus) |
| 63 | + // Issuer can be identified from `sessionClaims.iss` |
| 64 | + .get("/claims", ({ sessionClaims }) => sessionClaims), |
| 65 | + ) |
| 66 | + .get("/select", ({ set }) => { |
| 67 | + set.headers["Content-Type"] = "text/html"; |
| 68 | + return ` |
| 69 | +<html> |
| 70 | +<body> |
| 71 | +<p><a href="/auth/login">Issuer</a></p> |
| 72 | +<p><a href="/another/login">Another</a></p> |
| 73 | +</body> |
| 74 | +</html> |
| 75 | + `; |
| 76 | + }) |
| 77 | + .get("/free", () => "Not restricted") |
| 78 | + .get("/logout", () => "Logout completed") |
| 79 | + .listen(80); |
| 80 | +``` |
0 commit comments