├── .env.example ├── src ├── routes.ts ├── app.ts ├── views │ └── pages │ │ ├── login.ejs │ │ └── profile.ejs └── authController.ts ├── tsconfig.json ├── package.json ├── README.md └── .gitignore /.env.example: -------------------------------------------------------------------------------- 1 | CORBADO_PROJECT_ID= 2 | CORBADO_API_SECRET= 3 | CORBADO_FRONTEND_API= 4 | CORBADO_BACKEND_API= 5 | -------------------------------------------------------------------------------- /src/routes.ts: -------------------------------------------------------------------------------- 1 | import express from "express" 2 | import {auth, profile} from "./authController"; 3 | 4 | const router = express.Router(); 5 | 6 | router.get("/", auth) 7 | router.get("/profile", profile) 8 | 9 | export default router -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es6", 4 | "module": "commonjs", 5 | "strict": true, 6 | "esModuleInterop": true, 7 | "skipLibCheck": false, 8 | "forceConsistentCasingInFileNames": true, 9 | "outDir": "./dist" 10 | }, 11 | "include": [ 12 | "src/**/*.ts" 13 | ], 14 | "exclude": [ 15 | "node_modules" 16 | ] 17 | } -------------------------------------------------------------------------------- /src/app.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import cookieParser from "cookie-parser"; 3 | import authRoutes from "./routes" 4 | 5 | const app = express(); 6 | app.use(cookieParser()) 7 | app.set('views', './src/views') 8 | app.set('view engine', 'ejs'); 9 | 10 | const port = 3000; 11 | 12 | app.use("/", authRoutes) 13 | 14 | app.listen(port, () => { 15 | console.log(`Server is running on http://localhost:${port}`); 16 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-example", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "build": "tsc", 7 | "start": "ts-node src/app.ts" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "description": "", 12 | "dependencies": { 13 | "@corbado/node-sdk": "^3.0.2", 14 | "cookie-parser": "^1.4.6", 15 | "dotenv": "^16.4.5", 16 | "ejs": "^3.1.10", 17 | "express": "^4.19.2" 18 | }, 19 | "devDependencies": { 20 | "@types/cookie-parser": "^1.4.7", 21 | "@types/express": "^4.17.21", 22 | "@types/node": "^20.12.11", 23 | "sequelize-cli": "^6.6.2", 24 | "ts-node": "^10.9.2", 25 | "typescript": "^5.4.5" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/views/pages/login.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | 11 |
12 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/views/pages/profile.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |

User profile:

16 |

User-ID: <%= userId %>

17 |

Full name: <%= fullName %>

18 | 21 |
22 | 23 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/authController.ts: -------------------------------------------------------------------------------- 1 | import {Request, Response} from 'express'; 2 | import {SDK, Config} from '@corbado/node-sdk'; 3 | import {config as dotenvConfig} from "dotenv"; 4 | 5 | dotenvConfig() 6 | 7 | const projectID = process.env.CORBADO_PROJECT_ID; 8 | const apiSecret = process.env.CORBADO_API_SECRET; 9 | const frontendAPI = process.env.CORBADO_FRONTEND_API; 10 | const backendAPI = process.env.CORBADO_BACKEND_API; 11 | 12 | const cboConfig = new Config(projectID!, apiSecret!, frontendAPI!, backendAPI!); 13 | const sdk = new SDK(cboConfig); 14 | 15 | export function auth(req: Request, res: Response) { 16 | res.render('pages/login') 17 | } 18 | 19 | export async function profile(req: Request, res: Response) { 20 | const sessionToken = req.cookies.cbo_session_token 21 | if (!sessionToken) { 22 | res.redirect("/") 23 | } 24 | 25 | try { 26 | const user = await sdk.sessions().validateToken(sessionToken) 27 | const userId = user.userId 28 | const fullName = user.fullName 29 | 30 | res.render('pages/profile', {userId, fullName}) 31 | } catch (e) { 32 | console.log(e) 33 | res.redirect("/") 34 | } 35 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js (Express) Passkey Example App 2 | 3 | This is a sample implementation of frontend and backend where the Corbado Node.js SDK and Web-js components are 4 | integrated in an Express application. 5 | 6 | ## 1. File structure 7 | 8 | ``` 9 | ├── ... 10 | ├── .env # Environment variables for the application 11 | ├── src 12 | │ ├── app.ts # Application Entrypoint 13 | │ ├── routes.ts # Defines our routes 14 | │ ├── authController.ts # Handles all of our endpoints 15 | │ └── userService.ts # Service to manage User data 16 | └── ... 17 | ``` 18 | 19 | ## 2. Setup 20 | 21 | ### 2.1. Configure environment variables 22 | 23 | Please follow our [Getting started](https://docs.corbado.com/overview/getting-started) page to create and 24 | configure a project in the [developer panel](https://app.corbado.com). 25 | 26 | Use the values you obtained above to configure the following variables inside `.env`: 27 | 28 | 1. **CORBADO_PROJECT_ID**: The project ID. 29 | 2. **CORBADO_API_SECRET**: The API secret. 30 | 3. **CORBADO_FRONTEND_API**: The frontend API URL. 31 | 4. **CORBADO_BACKEND_API**: The backend API URL. 32 | 33 | ### 2.2. Run the Express App 34 | 35 | Use the following command to install all dependencies: 36 | 37 | ``` 38 | npm i 39 | ``` 40 | 41 | Now, you're ready to run the app like this: 42 | 43 | ``` 44 | npm run start 45 | ``` 46 | 47 | ## 3. Usage 48 | 49 | After step 2 your local server should be fully working. 50 | 51 | ### 3.1. Test authentication 52 | 53 | If you now visit `http://localhost:3000`, you should be seeing our authentication UI. 54 | 55 | Create an account and take a look at the profile page under `/profile` you'll be forwarded to. 56 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the pages line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # pages 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | .idea 107 | 108 | .DS_Store --------------------------------------------------------------------------------