This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial component * onSuccess, onFailure events * full api * add base headers to sdk * fix lint and test * better layout for demo app * fix for afterRequestHooks * dont create webcomponent after each change * fix tests --------- Co-authored-by: Piotr Bochenek <[email protected]>
- Loading branch information
Showing
15 changed files
with
230 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ | |
"error", | ||
{ | ||
"type": "element", | ||
"prefix": "lib", | ||
"style": "kebab-case" | ||
} | ||
] | ||
|
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,6 @@ | ||
// declare const BUILD_VERSION: string; | ||
|
||
export const baseHeaders = { | ||
'x-descope-sdk-name': 'angular', | ||
'x-descope-sdk-version': '1.1.1' | ||
}; |
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
48 changes: 48 additions & 0 deletions
48
projects/angular-sdk/src/lib/descope/descope.component.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,48 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { DescopeComponent } from './descope.component'; | ||
import { DescopeAuthConfig } from '../descope-auth.module'; | ||
import createSdk from '@descope/web-js-sdk'; | ||
import mocked = jest.mocked; | ||
|
||
jest.mock('@descope/web-js-sdk'); | ||
//Mock DescopeWebComponent | ||
jest.mock('@descope/web-component', () => { | ||
return jest.fn(); | ||
}); | ||
|
||
describe('DescopeComponent', () => { | ||
let component: DescopeComponent; | ||
let fixture: ComponentFixture<DescopeComponent>; | ||
let mockedCreateSdk: jest.Mock; | ||
const onSessionTokenChangeSpy = jest.fn(); | ||
const onUserChangeSpy = jest.fn(); | ||
const mockConfig: DescopeAuthConfig = { | ||
projectId: 'someProject' | ||
}; | ||
|
||
beforeEach(() => { | ||
mockedCreateSdk = mocked(createSdk); | ||
|
||
mockedCreateSdk.mockReturnValue({ | ||
onSessionTokenChange: onSessionTokenChangeSpy, | ||
onUserChange: onUserChangeSpy | ||
}); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [DescopeComponent], | ||
providers: [ | ||
DescopeAuthConfig, | ||
{ provide: DescopeAuthConfig, useValue: mockConfig } | ||
] | ||
}); | ||
|
||
fixture = TestBed.createComponent(DescopeComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
109 changes: 109 additions & 0 deletions
109
projects/angular-sdk/src/lib/descope/descope.component.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,109 @@ | ||
import { | ||
Component, | ||
ElementRef, | ||
EventEmitter, | ||
Input, | ||
OnChanges, | ||
Output | ||
} from '@angular/core'; | ||
import DescopeWebComponent from '@descope/web-component'; | ||
import DescopeWc, { ILogger } from '@descope/web-component'; | ||
import { DescopeAuthService } from '../descope-auth.service'; | ||
import { from } from 'rxjs'; | ||
import { baseHeaders } from '../constants'; | ||
|
||
@Component({ | ||
selector: 'descope[projectId][flowId]', | ||
template: '' | ||
}) | ||
export class DescopeComponent implements OnChanges { | ||
@Input() projectId: string; | ||
@Input() flowId: string; | ||
|
||
@Input() locale: string; | ||
@Input() theme: 'light' | 'dark' | 'os'; | ||
@Input() tenant: string; | ||
@Input() telemetryKey: string; | ||
@Input() redirectUrl: string; | ||
@Input() autoFocus: true | false | 'skipFirstScreen'; | ||
|
||
@Input() debug: boolean; | ||
@Input() errorTransformer: (error: { text: string; type: string }) => string; | ||
@Input() logger: ILogger; | ||
|
||
@Output() success: EventEmitter<void> = new EventEmitter<void>(); | ||
@Output() error: EventEmitter<void> = new EventEmitter<void>(); | ||
|
||
private readonly webComponent: DescopeWebComponent; | ||
|
||
constructor( | ||
private elementRef: ElementRef, | ||
private authService: DescopeAuthService | ||
) { | ||
DescopeWc.sdkConfigOverrides = { baseHeaders }; | ||
this.webComponent = new DescopeWebComponent(); | ||
} | ||
ngOnChanges(): void { | ||
this.setupWebComponent(); | ||
} | ||
|
||
private setupWebComponent() { | ||
this.webComponent.setAttribute('project-id', this.projectId); | ||
this.webComponent.setAttribute('flow-id', this.flowId); | ||
if (this.locale) { | ||
this.webComponent.setAttribute('locale', this.locale); | ||
} | ||
if (this.theme) { | ||
this.webComponent.setAttribute('theme', this.theme); | ||
} | ||
if (this.tenant) { | ||
this.webComponent.setAttribute('tenant', this.tenant); | ||
} | ||
if (this.telemetryKey) { | ||
this.webComponent.setAttribute('telemetryKey', this.telemetryKey); | ||
} | ||
if (this.redirectUrl) { | ||
this.webComponent.setAttribute('redirect-url', this.redirectUrl); | ||
} | ||
if (this.autoFocus) { | ||
this.webComponent.setAttribute('auto-focus', this.autoFocus.toString()); | ||
} | ||
if (this.debug) { | ||
this.webComponent.setAttribute('debug', this.debug.toString()); | ||
} | ||
|
||
if (this.errorTransformer) { | ||
this.webComponent.errorTransformer = this.errorTransformer; | ||
} | ||
|
||
if (this.logger) { | ||
this.webComponent.logger = this.logger; | ||
} | ||
|
||
if (this.success) { | ||
this.webComponent.addEventListener('success', () => { | ||
from( | ||
this.authService.sdk.httpClient.hooks?.afterRequest!( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
{} as any, | ||
new Response(JSON.stringify({})) | ||
) as Promise<unknown> | ||
).subscribe(() => { | ||
this.success?.emit(); | ||
}); | ||
}); | ||
} | ||
|
||
if (this.error) { | ||
this.webComponent.addEventListener('error', () => { | ||
this.error?.emit(); | ||
}); | ||
} | ||
|
||
const nativeElement = this.elementRef.nativeElement; | ||
while (nativeElement.lastElementChild) { | ||
nativeElement.removeChild(nativeElement.lastElementChild); | ||
} | ||
nativeElement.appendChild(this.webComponent); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<header> | ||
<div class="card"> | ||
{{ session$ | async | json }} | ||
</div> | ||
<div class="card"> | ||
{{ user$ | async | json }} | ||
</div> | ||
</header> | ||
<main> | ||
<button (click)="refreshSession()">Refresh Session</button> | ||
<button (click)="refreshUser()">Refresh User</button> | ||
<router-outlet></router-outlet> | ||
<div class="card-wrapper"> | ||
<div class="card"> | ||
<button (click)="refreshSession()">Refresh Session</button> | ||
<pre>{{ session$ | async | json }}</pre> | ||
</div> | ||
<div class="card"> | ||
<button (click)="refreshUser()">Refresh User</button> | ||
<pre>{{ user$ | async | json }}</pre> | ||
</div> | ||
</div> | ||
</main> |
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 |
---|---|---|
@@ -1,2 +1,13 @@ | ||
<button (click)="signUp()">Sign Up</button> | ||
<button (click)="login()">Login</button> | ||
<div class="theme-wrapper"> | ||
<button (click)="changeTheme('light')">Light Mode</button> | ||
<button (click)="changeTheme('dark')">Dark Mode</button> | ||
</div> | ||
<descope | ||
(success)="onSuccess()" | ||
(error)="onError()" | ||
[projectId]="projectId" | ||
[theme]="theme" | ||
flowId="sign-in" | ||
></descope> |
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 |
---|---|---|
|
@@ -6,3 +6,8 @@ | |
flex-direction: column; | ||
gap: 20px; | ||
} | ||
|
||
.theme-wrapper { | ||
display: flex; | ||
gap: 20px; | ||
} |
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