Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
make user optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Bochenek committed Nov 3, 2023
1 parent 1ebc9d3 commit 1783609
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 51 deletions.
6 changes: 2 additions & 4 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@
}
},
"cli": {
"schematicCollections": [
"@angular-eslint/schematics"
],
"analytics": false
"schematicCollections": ["@angular-eslint/schematics"],
"analytics": false
}
}
2 changes: 1 addition & 1 deletion projects/angular-sdk/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"lib": {
"entryFile": "src/public-api.ts"
},
"allowedNonPeerDependencies": ["@descope/web-component"]
"allowedNonPeerDependencies": ["@descope/web-component"]
}
12 changes: 6 additions & 6 deletions projects/angular-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "angular-sdk",
"version": "0.0.1",
"engines": {
"node": "^16.14.0 || ^18.10.0",
"npm": ">= 8.1.0"
},
"engines": {
"node": "^16.14.0 || ^18.10.0",
"npm": ">= 8.1.0"
},
"peerDependencies": {
"@angular/common": "^16.0.0",
"@angular/core": "^16.0.0"
},
"dependencies": {
"tslib": "^2.3.0",
"@descope/web-component": "2.11.8"
},
"@descope/web-component": "2.11.8"
},
"sideEffects": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { DescopeAuthConfig } from '../../types/types';
jest.mock('@descope/web-js-sdk');
//Mock DescopeWebComponent
jest.mock('@descope/web-component', () => {
return jest.fn();
return jest.fn(() => {
const element = document.createElement('div'); // Create a mock DOM element
element.setAttribute = jest.fn();
element.addEventListener = jest.fn();
return element;
});
});

describe('DescopeComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
Component,
ElementRef,
EventEmitter,
Input,
OnChanges, OnInit,
Output
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
OnInit,
Output
} from '@angular/core';
import DescopeWebComponent from '@descope/web-component';
import DescopeWc, { ILogger } from '@descope/web-component';
Expand Down Expand Up @@ -34,20 +35,18 @@ export class DescopeComponent implements OnInit, OnChanges {
@Output() success: EventEmitter<void> = new EventEmitter<void>();
@Output() error: EventEmitter<void> = new EventEmitter<void>();

private readonly webComponent: DescopeWebComponent;
private readonly webComponent: DescopeWebComponent = new DescopeWebComponent();

constructor(
private elementRef: ElementRef,
private authService: DescopeAuthService
) {
DescopeWc.sdkConfigOverrides = { baseHeaders };
this.webComponent = new DescopeWebComponent();
}
) {}

ngOnInit() {
DescopeWc.sdkConfigOverrides = { baseHeaders };
this.setupWebComponent();
this.elementRef.nativeElement.appendChild(this.webComponent);
}
this.elementRef.nativeElement.appendChild(this.webComponent);
}

ngOnChanges(): void {
this.setupWebComponent();
Expand Down Expand Up @@ -105,6 +104,5 @@ export class DescopeComponent implements OnInit, OnChanges {
this.error?.emit();
});
}

}
}
19 changes: 2 additions & 17 deletions projects/angular-sdk/src/lib/services/descope-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface DescopeSession {
sessionToken: string | null;
}

export type DescopeUser = { user: UserResponse; isUserLoading: boolean };
export type DescopeUser = { user?: UserResponse; isUserLoading: boolean };

@Injectable({
providedIn: 'root'
Expand All @@ -27,14 +27,6 @@ export class DescopeAuthService {
readonly descopeSession$: Observable<DescopeSession>;
readonly descopeUser$: Observable<DescopeUser>;

private readonly EMPTY_USER = {
loginIds: [],
userId: '',
createTime: 0,
TOTP: false,
SAML: false
};

constructor(config: DescopeAuthConfig) {
this.sdk = observabilify<DescopeSDK>(
createSdk({
Expand All @@ -53,8 +45,7 @@ export class DescopeAuthService {
});
this.descopeSession$ = this.sessionSubject.asObservable();
this.userSubject = new BehaviorSubject<DescopeUser>({
isUserLoading: false,
user: this.EMPTY_USER
isUserLoading: false
});
this.descopeUser$ = this.userSubject.asObservable();
}
Expand Down Expand Up @@ -101,19 +92,13 @@ export class DescopeAuthService {
return this.sdk.me().pipe(
tap((data) => {
const afterRequestUser = this.userSubject.value;
console.log(data);
if (data.data) {
this.userSubject.next({
...afterRequestUser,
user: {
...data.data
}
});
} else {
this.userSubject.next({
...afterRequestUser,
user: this.EMPTY_USER
});
}
}),
finalize(() => {
Expand Down
9 changes: 1 addition & 8 deletions projects/demo-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ export class AppComponent implements OnInit {
});

user$: Observable<DescopeUser> = of({
isUserLoading: false,
user: {
loginIds: [],
userId: '',
createTime: 0,
TOTP: false,
SAML: false
}
isUserLoading: false
});

ngOnInit() {
Expand Down

0 comments on commit 1783609

Please sign in to comment.