{"id":13497514,"url":"https://github.com/serhiisol/ngx-auth","last_synced_at":"2025-04-04T14:07:02.952Z","repository":{"id":54987672,"uuid":"82852768","full_name":"serhiisol/ngx-auth","owner":"serhiisol","description":"Angular 16+ Authentication Module","archived":false,"fork":false,"pushed_at":"2023-06-09T11:00:09.000Z","size":366,"stargazers_count":234,"open_issues_count":0,"forks_count":47,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-10-30T08:18:14.245Z","etag":null,"topics":["angular","authentication","interceptors"],"latest_commit_sha":null,"homepage":"","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/serhiisol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-02-22T21:09:44.000Z","updated_at":"2024-08-07T08:00:06.000Z","dependencies_parsed_at":"2024-01-16T09:56:16.402Z","dependency_job_id":"e520508e-4c33-4247-9eda-7c69e36680f6","html_url":"https://github.com/serhiisol/ngx-auth","commit_stats":{"total_commits":37,"total_committers":5,"mean_commits":7.4,"dds":"0.10810810810810811","last_synced_commit":"660e0566287fe9bae2242186291cdd40e9c682f7"},"previous_names":["serhiisol/ng4-auth"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serhiisol%2Fngx-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serhiisol%2Fngx-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serhiisol%2Fngx-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serhiisol%2Fngx-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serhiisol","download_url":"https://codeload.github.com/serhiisol/ngx-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247190250,"owners_count":20898702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["angular","authentication","interceptors"],"created_at":"2024-07-31T20:00:32.370Z","updated_at":"2025-04-04T14:07:02.924Z","avatar_url":"https://github.com/serhiisol.png","language":"TypeScript","funding_links":[],"categories":["Uncategorized","Security and Authentication","Dependencies"],"sub_categories":["Uncategorized","Authentication","Modules"],"readme":"# Angular 16+ Authentication\n\nThis package provides authentication module with interceptor\n\n```\nnpm install ngx-auth --save\n```\n\nFor older versions of angular see [Older Versions](#older-versions) section.\n\n## Full example\nFull example you can find in the [example folder](example).\n\n## Authentication module\n\nAuthentication modules provides ability to attach authentication token automatically to the headers\n(through http interceptors), refresh token functionality, guards for protected or public pages and more.\n\n#### Usage\n\n1. Import `AuthService` interface to implement it with your custom Authentication service, e.g.:\n\n```typescript\nimport { AuthService } from 'ngx-auth';\n\n@Injectable()\nexport class AuthenticationService implements AuthService {\n  private interruptedUrl: string;\n\n  constructor(private http: Http) {}\n\n  isAuthorized() {\n    const isAuthorized = !!sessionStorage.getItem('accessToken');\n\n    return of(isAuthorized);\n  }\n\n  getAccessToken() {\n    const accessToken = sessionStorage.getItem('accessToken');\n\n    return of(accessToken);\n  }\n\n  refreshToken(): Observable\u003cany\u003e {\n    const refreshToken = sessionStorage.getItem('refreshToken');\n\n    return this.http\n      .post('http://localhost:3001/refresh-token', { refreshToken })\n      .catch(() =\u003e this.logout())\n  }\n\n  refreshShouldHappen(response: HttpErrorResponse) {\n    return response.status === 401;\n  }\n\n  verifyRefreshToken(req: HttpRequest\u003cany\u003e) {\n    return req.url.endsWith('refresh-token');\n  }\n\n  skipRequest(req: HttpRequest\u003cany\u003e) {\n    return req.url.endsWith('third-party-request');\n  }\n\n  getInterruptedUrl() {\n    return this.interruptedUrl;\n  }\n\n  setInterruptedUrl(url: string) {\n    this.interruptedUrl = url;\n  }\n\n}\n```\n\n2. Specify functions `publicGuard` for public routes and `protectedGuard` for protected respectively, e.g.:\n\n```typescript\nconst routes: Routes = [\n  {\n    path: '',\n    component: PublicComponent,\n    canActivate: [publicGuard],\n    children: [/*...*/],\n  },\n  {\n    path: '',\n    component: ProtectedComponent,\n    canActivate: [protectedGuard],\n    children: [/*...*/],\n  }\n];\n```\n\n2. Create additional `AuthenticationModule` and provide important providers and imports, e.g.:\n\n```typescript\nimport { NgModule } from '@angular/core';\nimport { AuthModule, AUTH_SERVICE, PUBLIC_FALLBACK_PAGE_URI, PROTECTED_FALLBACK_PAGE_URI } from 'ngx-auth';\n\nimport { AuthenticationService } from './authentication.service';\n\n@NgModule({\n  imports: [ AuthModule ],\n  providers: [\n    { provide: PROTECTED_FALLBACK_PAGE_URI, useValue: '/' },\n    { provide: PUBLIC_FALLBACK_PAGE_URI, useValue: '/login' },\n    { provide: AUTH_SERVICE, useClass: AuthenticationService }\n  ]\n})\nexport class AuthenticationModule { }\n```\n\nwhere,\n* `PROTECTED_FALLBACK_PAGE_URI` - main protected page to be redirected to, in case if user will reach public route, that is protected\nby `publicGuard` and will be authenticated\n\n* `PUBLIC_FALLBACK_PAGE_URI` - main public page to be redirected to, in case if user will reach protected route, that is protected\nby `protectedGuard` and won't be authenticated\n\n* `AUTH_SERVICE` - Authentication service token providers\n\n3. Provide your `AuthenticationModule` in your `AppModule`\n\n### Customizing authentication headers\n\nBy default, requests are intercepted and a `{ Authorization: 'Bearer ${token}'}` header is injected. To customize this behavior, implement the `getHeaders` method on your `AuthenticationService`\n\n### After login redirect to the interrupted URL\n\nThe `AuthService` has an optional method `setInterruptedUrl` which saves the URL that was requested before the user is redirected to the login page. This property can be used in order to redirect the user to the originally requested page after he logs in. E.g. in your `login.component.ts` (check also `AuthService` implementation above):\n\n```typescript\n@Component({\n  selector: 'app-login',\n  templateUrl: './login.component.html'\n})\nexport class LoginComponent {\n  login() {\n    this.authService.login()\n      .subscribe(() =\u003e\n        this.router.navigateByUrl(this.authService.getInterruptedUrl())\n      );\n  }\n}\n```\n\n## Older Versions\nFor angular 7-15, use version 5.4.0\n```\nnpm install ngx-auth@5.4.0 --save\n```\n\nFor angular 6, use version 4.1.0\n```\nnpm install ngx-auth@4.1.0 --save\n```\n\nFor angular 5, use version 3.1.0\n```\nnpm install ngx-auth@3.1.0 --save\n```\n\nFor angular 4, use version 2.2.0\n```\nnpm install ngx-auth@2.2.0 --save\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserhiisol%2Fngx-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserhiisol%2Fngx-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserhiisol%2Fngx-auth/lists"}