Skip to content

Commit

Permalink
#4 - add auth.guard to avoid user try access to profile page when hav…
Browse files Browse the repository at this point in the history
…en't signin yet
  • Loading branch information
lele9h0st committed Jul 27, 2022
1 parent e72cf5b commit a476924
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/app/_help/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthGuard } from './auth.guard';

describe('AuthGuard', () => {
let guard: AuthGuard;

beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(AuthGuard);
});

it('should be created', () => {
expect(guard).toBeTruthy();
});
});
24 changes: 24 additions & 0 deletions src/app/_help/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import { Observable } from 'rxjs';
import {TokenStorageService} from "../_service/token-storage.service";

@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {

constructor(private router:Router, private tokenStorage: TokenStorageService ) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if(!(localStorage.getItem("isSignin")=="true")){
this.router.navigate(["signin"]);
return false;
}

return true;
}

}
3 changes: 2 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { ContactComponent } from './pages/contact/contact.component';
import { HomeComponent } from './pages/home/home.component';
import { NotfoundComponent } from './pages/notfound/notfound.component';
import { ProfileComponent } from "./profile/profile/profile.component";
import {AuthGuard} from "./_help/auth.guard";

const routes: Routes = [
{ path: 'profile', component: ProfileComponent },
{ path: 'profile', component: ProfileComponent ,canActivate:[AuthGuard]},
{ path: 'home', component: HomeComponent },
{ path: 'contact', component: ContactComponent },
{ path: '404', component: NotfoundComponent },
Expand Down
1 change: 1 addition & 0 deletions src/app/auth/signin/signin-form/signin-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class SigninFormComponent implements OnInit {
this.tokenStorage.saveUser(user);
console.log(user);
alert("login success")
localStorage.setItem("isSignin","true");
this.isLoggedIn = true;
this.siginForm.reset();
alert(this.tokenStorage.getUser().username)
Expand Down
3 changes: 3 additions & 0 deletions src/app/pages/notfound/notfound.component.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/app/pages/notfound/notfound.component.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 51 additions & 3 deletions src/app/shared/navbar/navbar.component.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/app/shared/navbar/navbar.component.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a476924

Please sign in to comment.