How oktadev/okta-node-sql-server-example works
Project Type and Purpose
• Type: Full-stack web application (SPA front-end + RESTful back end).
• Purpose: A calendar/event management app secured with Okta, persisting data in SQL Server, presented by a Vue.js SPA and server-rendered views.
File Structure
Top-level folders:
• client/ – Vue.js single-page app (App.vue, index.js).
• src/ – Hapi server code:
– server.js + src/index.js: bootstraps the Hapi server.
– src/routes/ (API endpoints for events & auth).
– src/plugins/ (SQL connector + Okta auth plugin).
– src/data/ (data-access layer with raw SQL scripts & helper functions).
– src/views/ (EJS templates for server-rendered pages).
– src/config.js (env variables: ports, SQL, Okta).
• tasks/ – utility scripts (initdb.js + SQL to create schema).
• test/ – unit/integration tests.
• .env.sample – placeholder for environment configuration.
Extract Insights from README
• Stack: Node.js (Hapi), SQL Server, Vue.js, Okta OIDC.
• Execution flow:
– Developer sets up .env with DB and Okta credentials.
– tasks:initdb runs SQL scripts to create “calendar” database and events table.
– npm run dev spins up Hapi server (serves API + views) and possibly bundles the Vue client.
– User accesses http://localhost:8080; SPA communicates with back end.
Main Components & Their Roles
A. Client (Vue.js SPA)
– Runs in the browser.
– Handles user interface: event list, add/edit/delete forms, login/out.
– Communicates over HTTPS with the server’s REST API.
B. Hapi Server (Node.js)
– Serves static assets (Vue bundle) and EJS-rendered pages (login callback).
– Exposes REST endpoints under /api/events and /api/auth (in src/routes).
– Applies security via Okta plugin (src/plugins/auth.js).
– Connects to SQL Server via SQL plugin (src/plugins/sql.js).
C. SQL Server Database
– Stores calendar and event data.
– Initialized via tasks/initdb.js and SQL scripts in src/data/events.
D. External Service: Okta OIDC Provider
– Manages user authentication and token issuance.
– Server uses Okta SDK (plugin) to validate tokens and protect routes.
E. Development & Operational Utilities
– tasks/initdb: CLI task to set up the database schema.
– test suite: validates routes, plugins, and data layer.
Defining Relationships and Data Flows
1. User → Browser → Vue.js SPA
2. SPA → (1) GET/POST /api/events → Hapi Server → SQL Plugin → SQL Server
(2) redirects to Okta for login → Okta → callback → Hapi auth plugin
3. Hapi Server → Renders EJS views for login/callback pages
4. tasks:initdb CLI → Direct SQL Connection → SQL Server (one-time schema setup)
5. Tests → Mock server + database stubs
Architectural Patterns & Design Principles
• Layered Architecture:
– Presentation (client + views),
– API/Controller (routes),
– Plugin Services (auth, sql),
– Data Access Layer (raw SQL scripts + utils).
• Plugin-based extensibility: Hapi plugins for authentication and database.
• Separation of concerns:
– UI vs. API vs. Data
– Configuration in .env (via src/config.js)
• Security by externalizing auth to Okta (OAuth2 / OIDC).