-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 added possibility to authenticate against api and save accesstoken…
… in cookie
- Loading branch information
1 parent
ed47936
commit 05e258f
Showing
15 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
devday/src/app/interceptors/access-token.interceptor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AccessTokenInterceptor } from './access-token.interceptor'; | ||
|
||
describe('AccessTokenInterceptor', () => { | ||
beforeEach(() => TestBed.configureTestingModule({ | ||
providers: [ | ||
AccessTokenInterceptor | ||
] | ||
})); | ||
|
||
it('should be created', () => { | ||
const interceptor: AccessTokenInterceptor = TestBed.inject(AccessTokenInterceptor); | ||
expect(interceptor).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
HttpRequest, | ||
HttpHandler, | ||
HttpEvent, | ||
HttpInterceptor | ||
} from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { LoginService } from '../services/login.service'; | ||
import { CookieService } from 'ngx-cookie-service'; | ||
|
||
@Injectable() | ||
export class AccessTokenInterceptor implements HttpInterceptor { | ||
private token : string; | ||
|
||
constructor(private cookieService : CookieService) {} | ||
|
||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||
if (!this.token) { | ||
this.token = this.cookieService.get('sessionToken') | ||
} | ||
return next.handle(this.addToken(request)); | ||
} | ||
|
||
addToken(request: HttpRequest<any>) { | ||
let withToken | ||
if (this.token) { | ||
withToken = request.clone({ | ||
setHeaders: { | ||
Authorization: `Bearer ${this.token}` | ||
} | ||
}) | ||
} else { | ||
withToken = request; | ||
} | ||
|
||
console.log(withToken) | ||
|
||
return withToken | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { ApiInterceptor } from './api.interceptor'; | ||
|
||
describe('ApiInterceptor', () => { | ||
beforeEach(() => TestBed.configureTestingModule({ | ||
providers: [ | ||
ApiInterceptor | ||
] | ||
})); | ||
|
||
it('should be created', () => { | ||
const interceptor: ApiInterceptor = TestBed.inject(ApiInterceptor); | ||
expect(interceptor).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
HttpRequest, | ||
HttpHandler, | ||
HttpEvent, | ||
HttpInterceptor | ||
} from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class ApiInterceptor implements HttpInterceptor { | ||
|
||
constructor() {} | ||
|
||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||
return next.handle(this.addContentType(request)); | ||
} | ||
|
||
addContentType(request: HttpRequest<any>) { | ||
let withHeaders = request.clone({ | ||
setHeaders: { | ||
'accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
console.log(withHeaders) | ||
|
||
return withHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { LoginService } from './login.service'; | ||
|
||
describe('LoginService', () => { | ||
let service: LoginService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(LoginService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { map, Subject } from 'rxjs'; | ||
import { environment } from 'src/environments/environment'; | ||
import { BasicRestService } from './basic-rest.service'; | ||
|
||
export interface LoginData { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LoginService { | ||
|
||
constructor(private restService: BasicRestService) { } | ||
|
||
login(loginData : LoginData) { | ||
return this.restService.post(environment.apiUrl + 'api-token-auth/', loginData) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
production: true | ||
production: true, | ||
apiUrl: 'https://www.devday.de/api/' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"/api/*": { | ||
"target": "https://www.devday.de/api/", | ||
"secure": false, | ||
"logLevel": "debug" | ||
} | ||
} |