{"id":47271434,"url":"https://github.com/nik2208/ng-awesome-node-auth","last_synced_at":"2026-04-23T07:01:04.283Z","repository":{"id":343761312,"uuid":"1177649249","full_name":"nik2208/ng-awesome-node-auth","owner":"nik2208","description":"Angular Interceptor and Guards for awesome-node-auth. https://www.npmjs.com/package/ng-awesome-node-auth","archived":false,"fork":false,"pushed_at":"2026-04-20T16:36:11.000Z","size":401,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"develop","last_synced_at":"2026-04-20T18:39:11.427Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://ng.awesomenodeauth.com","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nik2208.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-10T08:27:32.000Z","updated_at":"2026-04-20T16:36:15.000Z","dependencies_parsed_at":"2026-03-11T22:01:06.764Z","dependency_job_id":null,"html_url":"https://github.com/nik2208/ng-awesome-node-auth","commit_stats":null,"previous_names":["nik2208/ng-awesome-node-auth"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nik2208/ng-awesome-node-auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nik2208%2Fng-awesome-node-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nik2208%2Fng-awesome-node-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nik2208%2Fng-awesome-node-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nik2208%2Fng-awesome-node-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nik2208","download_url":"https://codeload.github.com/nik2208/ng-awesome-node-auth/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nik2208%2Fng-awesome-node-auth/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32101447,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-21T11:25:29.218Z","status":"ssl_error","status_checked_at":"2026-04-21T11:25:28.499Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-03-15T13:00:29.421Z","updated_at":"2026-04-23T07:01:04.174Z","avatar_url":"https://github.com/nik2208.png","language":"TypeScript","funding_links":[],"categories":["Security and Authentication"],"sub_categories":["Authentication"],"readme":"# NG Awesome Node Auth Project\n\nThis project combines a powerful Node.js authentication backend using `awesome-node-auth` with a modern Angular frontend utilizing the `ng-awesome-node-auth` library.\n\n## Project Structure\n\n- **Angular Application**: Located in `src/app`.\n- **Express Server**: Located in `src/server.ts` (handles SSR and API routing).\n- **In-Memory Stores**: Located in `src/server/` (User and Settings stores).\n\n---\n\n## 🚀 Server-Side Implementation\n\nThe server is built with Express and handles both the Angular SSR engine and the `awesome-node-auth` backend.\n\n### 1. Auth Configuration (`src/server/auth.config.ts`)\n\nDefine your stores and core authentication settings.\n\n```typescript\nimport { AuthConfigurator, AuthConfig } from 'awesome-node-auth';\nimport { InMemoryUserStore } from './in-memory-user-store';\nimport { InMemorySettingsStore } from './in-memory-settings-store';\nimport { join } from 'node:path';\n\nexport const userStore = new InMemoryUserStore();\nexport const settingsStore = new InMemorySettingsStore();\nexport const uploadDir = join(process.cwd(), 'public/uploads');\n\nexport const authConfig: AuthConfig = {\n  apiPrefix: '/api/auth',\n  accessTokenSecret: process.env['JWT_SECRET'],\n  ui: { enabled: true },\n  email: { siteUrl: 'http://localhost:4200' }\n};\n\nexport const authConfigurator = new AuthConfigurator(authConfig, userStore);\n```\n\n### 2. API Routes (`src/server/auth.routes.ts`)\n\nMount the authentication router and the Admin Panel.\n\n```typescript\nimport { Router } from 'express';\nimport { authConfigurator, settingsStore, uploadDir } from './auth.config';\n\nconst router = Router();\n\nrouter.use('/', authConfigurator.router({\n  settingsStore,\n  uploadDir,\n  onRegister: async (data) =\u003e {\n    // Custom registration logic (e.g., password hashing)\n    return userStore.create(data);\n  }\n}));\n\nexport default router;\n```\n\n### 3. Main Server Entry (`src/server.ts`)\n\nWire everything together, including the Admin Panel and Angular SSR.\n\n```typescript\nimport express from 'express';\nimport { createAdminRouter } from 'awesome-node-auth';\nimport authRoutes from './server/auth.routes';\nimport { userStore, settingsStore, uploadDir, ADMIN_SECRET } from './server/auth.config';\n\nconst app = express();\n\n// Auth API \u0026 UI\napp.use('/api/auth', authRoutes);\n\n// Admin Panel (mounted at root for correct path resolution)\napp.use('/admin/auth', createAdminRouter(userStore, {\n  adminSecret: ADMIN_SECRET,\n  settingsStore,\n  uploadDir,\n  apiPrefix: '/api/auth'\n}));\n\n// Fallback for all other routes to Angular SSR\napp.use('**', angularSsrHandler);\n```\n\n---\n\n### 1. Breaking SSR Routing Loops\nWhen using Angular SSR, the server tries to render all routes. If an unauthenticated user hits `/dashboard`, Angular redirects to `/login`. If `/login` is also handled by Angular (and it's a SPA), this can lead to infinite loops.\n\n**The Solution:**\nHandle auth redirects at the **Express server level** before the Angular engine. This ensures the browser is redirected to the backend-served UI pages:\n\n```typescript\nconst authPaths = ['login', 'register', 'forgot-password', 'reset-password', '2fa', 'verify-email'];\n\nauthPaths.forEach(p =\u003e {\n  app.get(`/${p}`, (req, res) =\u003e {\n    // Redirect to the backend-served UI, preserving query parameters\n    const query = req.url.includes('?') ? '?' + req.url.split('?')[1] : '';\n    res.redirect(`/api/auth/ui/${p}${query}`);\n  });\n});\n```\n\n### 2. Preventing Build-Time Server Starts\nAngular's build process (`ng build`) imports your server module to extract routes. If your server starts listening on a port during this phase, the build will hang.\n\n**The Solution:**\nWrap your `app.listen()` block with a check for `isMainModule()`:\n\n```typescript\nimport { isMainModule } from '@angular/ssr/node';\n\nif (isMainModule(import.meta.url)) {\n  const port = process.env['PORT'] || 4200;\n  app.listen(port, () =\u003e {\n    console.log(`Server listening on http://localhost:${port}`);\n  });\n}\n```\nThis ensures the server only starts during actual execution, not during the build.\n\n### 3. Wildcard SSR Route\nIn `src/app/app.routes.server.ts`, ensure you have a wildcard route configured for server rendering to handle the fallback correctly:\n  ```typescript\n  export const serverRoutes: ServerRoute[] = [\n    { path: '**', renderMode: RenderMode.Server }\n  ];\n  ```\n\n---\n\n## ⚙️ Enabling Full Admin Features\nTo unlock all features in the Admin Panel (Settings, Uploads, Theme Sync), the `createAdminRouter` must be configured with:\n- `settingsStore`: Enables the \"Control\" tab for real-time UI customization.\n- `uploadDir`: Enables file uploads for logos and backgrounds.\n- `apiPrefix`: Essential for the Admin Panel to correctly resolve the Auth API endpoints.\n\n```typescript\napp.use('/admin/auth', createAdminRouter(userStore, {\n  adminSecret: ADMIN_SECRET,\n  settingsStore,\n  uploadDir,\n  apiPrefix: '/api/auth'\n}));\n```\n\n---\n\n## 🎨 Frontend Implementation\n\nThe Angular app uses the `ng-awesome-node-auth` library for seamless integration.\n\n### 1. App Configuration (`src/app/app.config.ts`)\n\n```typescript\nimport { provideAuth, provideAuthUi } from 'ng-awesome-node-auth';\n\nexport const appConfig: ApplicationConfig = {\n  providers: [\n    provideAuth({ \n      apiPrefix: '/api/auth',\n      // Optional: set to true to handle session expiry manually \n      // without automatic full-page redirects.\n      headless: false \n    }),\n    provideAuthUi() // For theme synchronization and config-aware UI\n  ]\n};\n```\n\n### 2. Route Protection (`src/app/app.routes.ts`)\n\n```typescript\nimport { authGuard, guestGuard } from 'ng-awesome-node-auth';\n\nexport const routes: Routes = [\n  { path: 'login', component: LoginComponent, canActivate: [guestGuard] },\n  { path: 'dashboard', component: DashboardComponent, canActivate: [authGuard] }\n];\n```\n\n---\n\n## 🛠️ Development\n\n### Scripts\n\n- `npm run dev`: Start Angular dev server.\n- `npm run build:ssr`: Build the Angular application and the Express server for production.\n- `node dist/ng-awesome-node-auth-prj/server/server.mjs`: Start the production server.\n\n### Maintenance\n\nThe project includes an auto-cleanup timer in `server.ts` that clears the in-memory user database every 5 minutes during testing.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnik2208%2Fng-awesome-node-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnik2208%2Fng-awesome-node-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnik2208%2Fng-awesome-node-auth/lists"}