Skip to content

Commit 62a495d

Browse files
authored
feat: functional interceptor and guards (@7.0.0) (#65)
1 parent 12b6d5d commit 62a495d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+16807
-21001
lines changed

.eslintrc.json

Lines changed: 0 additions & 221 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ node_modules
44
# temp
55
.angular
66
dist
7+
temp
8+
tmp
79
dist.tgz

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2023 Serhii Solonko.
3+
Copyright (c) 2025 Serhii Solonko.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 10 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Angular 16+ Authentication
1+
# Angular 20+ Authentication
22

33
This package provides authentication module with interceptor
44

@@ -17,134 +17,22 @@ Authentication modules provides ability to attach authentication token automatic
1717
(through http interceptors), refresh token functionality, guards for protected or public pages and more.
1818

1919
#### Usage
20-
21-
1. Import `AuthService` interface to implement it with your custom Authentication service, e.g.:
22-
23-
```typescript
24-
import { AuthService } from 'ngx-auth';
25-
26-
@Injectable()
27-
export class AuthenticationService implements AuthService {
28-
private interruptedUrl: string;
29-
30-
constructor(private http: Http) {}
31-
32-
isAuthorized() {
33-
const isAuthorized = !!sessionStorage.getItem('accessToken');
34-
35-
return of(isAuthorized);
36-
}
37-
38-
getAccessToken() {
39-
const accessToken = sessionStorage.getItem('accessToken');
40-
41-
return of(accessToken);
42-
}
43-
44-
refreshToken(): Observable<any> {
45-
const refreshToken = sessionStorage.getItem('refreshToken');
46-
47-
return this.http
48-
.post('http://localhost:3001/refresh-token', { refreshToken })
49-
.catch(() => this.logout())
50-
}
51-
52-
refreshShouldHappen(response: HttpErrorResponse) {
53-
return response.status === 401;
54-
}
55-
56-
verifyRefreshToken(req: HttpRequest<any>) {
57-
return req.url.endsWith('refresh-token');
58-
}
59-
60-
skipRequest(req: HttpRequest<any>) {
61-
return req.url.endsWith('third-party-request');
62-
}
63-
64-
getInterruptedUrl() {
65-
return this.interruptedUrl;
66-
}
67-
68-
setInterruptedUrl(url: string) {
69-
this.interruptedUrl = url;
70-
}
71-
72-
}
73-
```
74-
75-
2. Specify functions `publicGuard` for public routes and `protectedGuard` for protected respectively, e.g.:
76-
77-
```typescript
78-
const routes: Routes = [
79-
{
80-
path: '',
81-
component: PublicComponent,
82-
canActivate: [publicGuard],
83-
children: [/*...*/],
84-
},
85-
{
86-
path: '',
87-
component: ProtectedComponent,
88-
canActivate: [protectedGuard],
89-
children: [/*...*/],
90-
}
91-
];
92-
```
93-
94-
2. Create additional `AuthenticationModule` and provide important providers and imports, e.g.:
95-
96-
```typescript
97-
import { NgModule } from '@angular/core';
98-
import { AuthModule, AUTH_SERVICE, PUBLIC_FALLBACK_PAGE_URI, PROTECTED_FALLBACK_PAGE_URI } from 'ngx-auth';
99-
100-
import { AuthenticationService } from './authentication.service';
101-
102-
@NgModule({
103-
imports: [ AuthModule ],
104-
providers: [
105-
{ provide: PROTECTED_FALLBACK_PAGE_URI, useValue: '/' },
106-
{ provide: PUBLIC_FALLBACK_PAGE_URI, useValue: '/login' },
107-
{ provide: AUTH_SERVICE, useClass: AuthenticationService }
108-
]
109-
})
110-
export class AuthenticationModule { }
111-
```
112-
113-
where,
114-
* `PROTECTED_FALLBACK_PAGE_URI` - main protected page to be redirected to, in case if user will reach public route, that is protected
115-
by `publicGuard` and will be authenticated
116-
117-
* `PUBLIC_FALLBACK_PAGE_URI` - main public page to be redirected to, in case if user will reach protected route, that is protected
118-
by `protectedGuard` and won't be authenticated
119-
120-
* `AUTH_SERVICE` - Authentication service token providers
121-
122-
3. Provide your `AuthenticationModule` in your `AppModule`
20+
1. Import `NgxAuthService` interface to implement it with your custom Authentication service, e.g.:
21+
2. Specify functions `ngxPublicGuard` for public routes and `ngxProtectedGuard` for protected respectively, e.g.:
22+
3. Use `provideNgxAuthProviders` to provide `NgxAuthService` implementation and `protectedRedirectUri`, `publicRedirectUri` redirect uris
12323

12424
### Customizing authentication headers
125-
126-
By default, requests are intercepted and a `{ Authorization: 'Bearer ${token}'}` header is injected. To customize this behavior, implement the `getHeaders` method on your `AuthenticationService`
25+
By default, requests are intercepted and a `{ Authorization: 'Bearer ${token}'}` header is injected. To customize this behavior, implement the `getHeaders` method on your `NgxAuthService`
12726

12827
### After login redirect to the interrupted URL
28+
The `NgxAuthService` 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.
12929

130-
The `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):
131-
132-
```typescript
133-
@Component({
134-
selector: 'app-login',
135-
templateUrl: './login.component.html'
136-
})
137-
export class LoginComponent {
138-
login() {
139-
this.authService.login()
140-
.subscribe(() =>
141-
this.router.navigateByUrl(this.authService.getInterruptedUrl())
142-
);
143-
}
144-
}
145-
```
14630

14731
## Older Versions
32+
For angular 16-19, use version 6.0.0
33+
```
34+
npm install [email protected] --save
35+
```
14836
For angular 7-15, use version 5.4.0
14937
```
15038
npm install [email protected] --save

0 commit comments

Comments
 (0)