Skip to content

Add seter to currentAuthData #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/service-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ get currentUserData(): UserData
```

## .currentAuthData
Returns current authentication data which are used to set auth headers.
Sets or returns current authentication data which are used to set auth headers.

```js
get currentAuthData(): AuthData

set currentAuthData(authData: AuthData)
```

## .tokenOptions
Expand Down
2 changes: 1 addition & 1 deletion projects/angular-token/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-token",
"version": "7.1.0-rc.0",
"version": "7.1.0-rc.5",
"repository": "https://github.com/neroniaky/angular-token.git",
"author": {
"name": "Jan-Philipp Riethmacher",
Expand Down
4 changes: 2 additions & 2 deletions projects/angular-token/src/lib/angular-token.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class AngularTokenInterceptor implements HttpInterceptor {

// Add the headers if the request is going to the configured server
const authData = this.tokenService.authData.value;

if (authData &&
(this.tokenService.tokenOptions.apiBase === null || req.url.match(this.tokenService.tokenOptions.apiBase))) {
(this.tokenService.tokenOptions.apiBase === null || req.url.match(this.tokenService.tokenOptions.apiBase)
|| req.url.match(this.tokenService.tokenOptions.secondaryApiBase))) {

const headers = {
'access-token': authData.accessToken,
Expand Down
1 change: 1 addition & 0 deletions projects/angular-token/src/lib/angular-token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface AngularTokenOptions {

apiBase?: string;
apiPath?: string;
secondaryApiBase?: string;

signInPath?: string;
signInRedirect?: string;
Expand Down
5 changes: 5 additions & 0 deletions projects/angular-token/src/lib/angular-token.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ describe('AngularTokenService', () => {
it('userSignedIn should return false', () => {
expect(service.userSignedIn()).toEqual(false);
});

it('currentUserdata should return auth data after setting them', () => {
service.currentAuthData = authData;
expect(service.currentAuthData).toEqual(authData);
});
});

describe('user signed in', () => {
Expand Down
12 changes: 12 additions & 0 deletions projects/angular-token/src/lib/angular-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export class AngularTokenService implements CanActivate {
return this.authData.value;
}

set currentAuthData(authData: AuthData) {
if (this.checkAuthData(authData)) {
this.authData.next(authData);
}
}

get apiBase(): string {
console.warn('[angular-token] The attribute .apiBase will be removed in the next major release, please use' +
'.tokenOptions.apiBase instead');
Expand Down Expand Up @@ -144,6 +150,12 @@ export class AngularTokenService implements CanActivate {
this.tryLoadAuthData();
}

updateCurrentAuthData(authData: AuthData) {
if (this.checkAuthData(authData)) {
this.authData.next(authData);
}
}

userSignedIn(): boolean {
if (this.authData.value == null) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions projects/angular-token/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export { ANGULAR_TOKEN_OPTIONS } from './lib/angular-token.token';
export { AngularTokenService } from './lib/angular-token.service';

export { AngularTokenModule } from './lib/angular-token.module';

export { AngularTokenInterceptor } from './lib/angular-token.interceptor';