Skip to content

Commit

Permalink
feat: improve store to be global, change README to start dev instead …
Browse files Browse the repository at this point in the history
…of start
  • Loading branch information
raynerljm committed May 22, 2023
1 parent 2d5b961 commit a1d2f3f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
4 changes: 2 additions & 2 deletions examples/nextjs-csr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Before you can run the development server, you will have to register your client
Copy the `.env.example` file, rename it to `.env`, and fill in your credentials obtained during registration.

Then, run the server by running:
Then, run the development server by running:

```bash
npm run start:dev
npm run dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
3 changes: 1 addition & 2 deletions examples/nextjs-csr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "NEXT_ENV=development next dev",
"dev": "next dev",
"build": "next build",
"start": "next start",
"start:dev": "next build && NEXT_ENV=development next start",
"lint": "next lint"
},
"dependencies": {
Expand Down
17 changes: 7 additions & 10 deletions examples/nextjs-csr/src/lib/sgidClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { SgidClient } from "@opengovsg/sgid-client";
import { SgidClient } from '@opengovsg/sgid-client'

const sgidClient = new SgidClient({
clientId: String(process.env.SGID_CLIENT_ID) || "",
clientSecret: String(process.env.SGID_CLIENT_SECRET) || "",
privateKey: String(process.env.SGID_PRIVATE_KEY).replace(/\\n/g, "\n") || "",
redirectUri:
process.env.NEXT_ENV !== "development"
? "https://sgid-nextjs-csr-demo.vercel.app/api/callback"
: "http://localhost:3000/api/callback",
});
clientId: String(process.env.SGID_CLIENT_ID),
clientSecret: String(process.env.SGID_CLIENT_SECRET),
privateKey: String(process.env.SGID_PRIVATE_KEY),
redirectUri: 'http://localhost:3000/api/callback',
})

export { sgidClient };
export { sgidClient }
22 changes: 21 additions & 1 deletion examples/nextjs-csr/src/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
type Global = typeof global

interface GlobalWithStore extends Global {
store: Map<string, Session>
}

/**
* We place the store in the global object to prevent it from being cleared whenever compilation happens
* https://stackoverflow.com/questions/75272877/how-to-prevent-next-js-from-instantiating-a-singleton-class-object-multiple-time
*/
type Session = {
state?: string
codeVerifier?: string
Expand All @@ -7,6 +17,16 @@ type Session = {
userInfo?: Record<string, string>
}

const store = new Map<string, Session>()
let store: Map<string, Session>

if (process.env.NODE_ENV === 'production') {
store = new Map<string, Session>()
} else {
// If the store does not exist, initialize it
if (!(global as GlobalWithStore).store) {
;(global as GlobalWithStore).store = new Map<string, Session>()
}
store = (global as GlobalWithStore).store
}

export { store }
28 changes: 14 additions & 14 deletions examples/nextjs-csr/src/pages/api/auth-url.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import { v4 as uuidv4 } from "uuid";
import { store } from "../../lib/store";
import { sgidClient } from "../../lib/sgidClient";
import { setCookie } from "cookies-next";
import { generatePkcePair } from "@opengovsg/sgid-client";
import type { NextApiRequest, NextApiResponse } from 'next'
import { v4 as uuidv4 } from 'uuid'
import { store } from '../../lib/store'
import { sgidClient } from '../../lib/sgidClient'
import { setCookie } from 'cookies-next'
import { generatePkcePair } from '@opengovsg/sgid-client'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
let { state } = req.query;
state = String(state);
let { state } = req.query
state = String(state)

const sessionId = uuidv4();
const sessionId = uuidv4()

const { codeChallenge, codeVerifier } = generatePkcePair();
const { codeChallenge, codeVerifier } = generatePkcePair()

const { url, nonce } = sgidClient.authorizationUrl({
state,
codeChallenge,
});
})

store.set(sessionId, { state, nonce, codeVerifier });
store.set(sessionId, { state, nonce, codeVerifier })

setCookie("sessionId", sessionId, { req, res });
setCookie('sessionId', sessionId, { req, res, httpOnly: true, secure: true })

res.redirect(url);
res.redirect(url)
}

0 comments on commit a1d2f3f

Please sign in to comment.