wmc-ng-auth 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth.guard.ts +29 -0
- package/auth.interceptor.ts +14 -0
- package/auth.service.ts +40 -0
- package/index.ts +3 -0
- package/package.json +15 -0
package/auth.guard.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ActivatedRouteSnapshot,
|
|
3
|
+
CanActivate,
|
|
4
|
+
CanActivateFn,
|
|
5
|
+
GuardResult,
|
|
6
|
+
MaybeAsync, Router,
|
|
7
|
+
RouterStateSnapshot
|
|
8
|
+
} from '@angular/router';
|
|
9
|
+
import {AuthService} from './auth.service';
|
|
10
|
+
import {Injectable} from '@angular/core';
|
|
11
|
+
|
|
12
|
+
@Injectable({
|
|
13
|
+
providedIn: 'root'
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export class AuthGuard implements CanActivate{
|
|
17
|
+
constructor(private router: Router, private auth:AuthService) {
|
|
18
|
+
}
|
|
19
|
+
canActivate() {
|
|
20
|
+
if (this.auth.isLoggedIn()){
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.router.navigate(['/login'])
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HttpInterceptorFn } from '@angular/common/http';
|
|
2
|
+
import {inject} from '@angular/core';
|
|
3
|
+
import {AuthService} from './auth.service';
|
|
4
|
+
|
|
5
|
+
export const authInterceptor: HttpInterceptorFn = (req, next) => {
|
|
6
|
+
const authService = inject(AuthService);
|
|
7
|
+
if (!authService.isLoggedIn())
|
|
8
|
+
return next(req);
|
|
9
|
+
const cloned = req.clone({
|
|
10
|
+
headers: req.headers.set('Authorization',
|
|
11
|
+
`Bearer ${authService.getToken()}`)
|
|
12
|
+
});
|
|
13
|
+
return next(cloned);
|
|
14
|
+
};
|
package/auth.service.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {Injectable, signal} from '@angular/core';
|
|
2
|
+
import {HttpClient} from '@angular/common/http';
|
|
3
|
+
import {tap} from 'rxjs';
|
|
4
|
+
import {Router} from '@angular/router';
|
|
5
|
+
|
|
6
|
+
@Injectable({
|
|
7
|
+
providedIn: 'root',
|
|
8
|
+
})
|
|
9
|
+
export class AuthService {
|
|
10
|
+
_user = signal('')
|
|
11
|
+
private baseUrl = 'http://localhost:3000'
|
|
12
|
+
private static tokenKey: string = "token";
|
|
13
|
+
constructor(private httpClient : HttpClient, private router : Router) {
|
|
14
|
+
}
|
|
15
|
+
login(username: string, password: string) {
|
|
16
|
+
return this.httpClient
|
|
17
|
+
.post<{ token: string }>(`${this.baseUrl}/login`,
|
|
18
|
+
{ username: username, password: password })
|
|
19
|
+
.pipe(
|
|
20
|
+
tap(response => {
|
|
21
|
+
localStorage.setItem(AuthService.tokenKey, response.token);
|
|
22
|
+
this._user.set(username);
|
|
23
|
+
})
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
logout(): void {
|
|
28
|
+
localStorage.removeItem(AuthService.tokenKey);
|
|
29
|
+
this._user.set('');
|
|
30
|
+
this.router.navigate(['/login']);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
isLoggedIn(){
|
|
34
|
+
return !!localStorage.getItem(AuthService.tokenKey)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getToken() {
|
|
38
|
+
return localStorage.getItem(AuthService.tokenKey)
|
|
39
|
+
}
|
|
40
|
+
}
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wmc-ng-auth",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Angular auth service, guard, and interceptor for JWT-based authentication",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"scripts": {},
|
|
7
|
+
"keywords": ["angular", "auth", "jwt", "guard", "interceptor"],
|
|
8
|
+
"author": "tobero",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"@angular/common": ">=17.0.0",
|
|
12
|
+
"@angular/core": ">=17.0.0",
|
|
13
|
+
"@angular/router": ">=17.0.0"
|
|
14
|
+
}
|
|
15
|
+
}
|