Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10e2593

Browse files
Creates Authguard service
1 parent fc8d32c commit 10e2593

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { AuthGuardService } from './auth-guard.service';
4+
5+
describe('AuthGuardService', () => {
6+
let service: AuthGuardService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(AuthGuardService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, Router } from '@angular/router';
3+
import { Observable, of } from 'rxjs';
4+
import { map } from 'rxjs/operators';
5+
import { AuthService } from './auth.service';
6+
7+
@Injectable()
8+
export class AuthGuard implements CanActivate {
9+
constructor(private authService: AuthService, private router: Router) {}
10+
11+
canActivate(): Observable<boolean> {
12+
const isAuthenticated = this.authService.isAuthenticated();
13+
if (!isAuthenticated) {
14+
this.router.navigate(['/login']);
15+
}
16+
return of(isAuthenticated);
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /