Skip to content

Commit

Permalink
latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Venditto committed May 27, 2019
1 parent f26ef56 commit 561e350
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CatTeaserComponent } from '../commons/components/cat-teaser/cat-teaser.

import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { CommonReqInterceptor } from '../commons/interceptors/commonReq.interceptor';
import { CommonResInterceptor } from '../commons/interceptors/commonRes.interceptor';
import { OopsComponentComponent } from '../commons/components/oops-component/oops-component.component';
import { SupporterAreaComponent } from '../commons/components/supporter-area/supporter-area.component';
import { AuthenticationGuard } from 'src/commons/guards/authentication.guard';
Expand Down Expand Up @@ -50,6 +51,11 @@ import { AuthenticationGuard } from 'src/commons/guards/authentication.guard';
provide: HTTP_INTERCEPTORS,
useClass: CommonReqInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: CommonResInterceptor,
multi: true,
}
],
bootstrap: [AppComponent]
Expand Down
4 changes: 2 additions & 2 deletions src/commons/components/cat-teaser/cat-teaser.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<section class="content__base">
<div class="supporter__base">
<a class="supporter__link" href="supporter-area" title="Don't be lazy. Support us">Don't be lazy! Support this mission!</a>
<a class="supporter__link" href="supporter-area" title="Don't be lazy. Support us"> If you're already a supporter, get special perks!</a>
</div>
<ul class="teasergallery__base columncontrol__grid--lt2">
<li #lazyKitten *ngFor="let kitten of kittens" attr.data-attr="{{urlPrefix}}{{kitten.name}}{{urlSuffix}}" class="teaser__base teaser__base--one">
<img class="teaser__image" src="" alt="pic of {{kitten.name}}" />
<h3 class="teaser__hl">{{kitten.name}}</h3>
<p class="teaser__summary">Lorem ipsum dolor sit amet bla bla bla</p>
<a class="teaser__link" href="#">Know {{kitten.name}}</a>
<a class="teaser__link" href="#">Support {{kitten.name}} to space!</a>
</li>
</ul>
</section>
5 changes: 4 additions & 1 deletion src/commons/components/cat-teaser/cat-teaser.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, OnInit, OnDestroy, AfterViewInit, ViewChildren, ElementRef,
import { Apollo } from 'apollo-angular';
import { Subscription } from 'rxjs';
import gql from 'graphql-tag';
import { AuthenticationService } from '../../services/authentication/authentication.service';

// We use the gql tag to parse our query string into a query document
const AllKitten = gql`
Expand Down Expand Up @@ -33,7 +34,8 @@ export class CatTeaserComponent implements OnInit, OnDestroy, AfterViewInit {
private catsSubscription: Subscription;
constructor(
private apollo: Apollo,
private renderer: Renderer2
private renderer: Renderer2,
private auth: AuthenticationService
) {}

lazyLoadCats(): void {
Expand Down Expand Up @@ -65,6 +67,7 @@ export class CatTeaserComponent implements OnInit, OnDestroy, AfterViewInit {
}

ngOnInit(): void {
// this.auth.setToken();
this.querySubscription = this.apollo.watchQuery<any>({
query: AllKitten
})
Expand Down
2 changes: 1 addition & 1 deletion src/commons/guards/authentication.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AuthenticationGuard implements CanActivate {
const canIt: boolean = this.authenticationService.isAuthenticated();

console.log(canIt + ' this is the value of canIt - can it access the supporters zone?');

// I mean, it seriously can?
if (canIt) {
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/commons/interceptors/commonReq.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { Injectable, Injector } from '@angular/core';
@Injectable()
export class CommonReqInterceptor implements HttpInterceptor {
constructor(
public auth: AuthenticationService,
private injector: Injector
public auth: AuthenticationService
) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Instantiate AuthenticationService
Expand Down
45 changes: 45 additions & 0 deletions src/commons/interceptors/commonRes.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpResponse,
HttpErrorResponse
} from '@angular/common/http';
import { AuthenticationService } from '../services/authentication/authentication.service';
import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Injectable, Injector } from '@angular/core';


@Injectable()
export class CommonResInterceptor implements HttpInterceptor {
// Must initialize injector in constructor to be able to inject and instantiate AuthenticationService directly
constructor(
private auth: AuthenticationService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const certError: string = 'Unknown Error';
return next.handle(req)
.pipe(map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
if (event.url.indexOf('api') >= 0 && event.body.exceptions) {
// If session_token cookie is invalid, log user out and delete userId cookie
if (event.body.exceptions.indexOf('exceptions') >= 0) {
this.auth.sendBacktoHome();
}
}
}
return event;
}))
.pipe(catchError((err: any, caught) => {
if (err instanceof HttpErrorResponse) {
this.auth.deleteToken();
if (err.status === 0 || err.statusText === certError) {
console.error('You must accept the certificates');
}
return Observable.throw(err);
}
}));
}
}
13 changes: 12 additions & 1 deletion src/commons/services/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Injectable } from '@angular/core';
import { Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';

@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
constructor(private injector: Injector) {}
// fake set token
setToken() {
// just set a random number for demo
Expand All @@ -16,7 +18,16 @@ export class AuthenticationService {
return localStorage.getItem('token');
}

deleteToken(): void {
localStorage.removeItem('token');
}

isAuthenticated(): boolean {
return !!this.getToken();
}

sendBacktoHome(): void {
const router: Router = this.injector.get(Router);
router.navigate(['/oops']);
}
}

0 comments on commit 561e350

Please sign in to comment.