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

Commit

Permalink
62 renaming (#64)
Browse files Browse the repository at this point in the history
* base rename

* fix docs

* env var rename

* lint and format

---------

Co-authored-by: Piotr Bochenek <[email protected]>
  • Loading branch information
MF57 and Piotr Bochenek authored Nov 16, 2023
1 parent 95a454c commit cbed1af
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class AppComponent {
></descope>
```

### Use the `DescopeAuthService` and its exposed fields (`sdk`, `descopeSession$`, `descopeUser$`) to access authentication state, user details and utilities
### Use the `DescopeAuthService` and its exposed fields (`descopeSdk`, `session$`, `user$`) to access authentication state, user details and utilities

This can be helpful to implement application-specific logic. Examples:

Expand Down Expand Up @@ -173,18 +173,18 @@ export class AppComponent implements OnInit {
constructor(private authService: DescopeAuthService) {}

ngOnInit() {
this.authService.descopeSession$.subscribe((session) => {
this.authService.session$.subscribe((session) => {
this.isAuthenticated = session.isAuthenticated;
});
this.authService.descopeUser$.subscribe((descopeUser) => {
this.authService.user$.subscribe((descopeUser) => {
if (descopeUser.user) {
this.userName = descopeUser.user.name ?? '';
}
});
}

logout() {
this.authService.sdk.logout();
this.authService.descopeSdk.logout();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class DescopeComponent implements OnInit, OnChanges {
if (this.success) {
this.webComponent.addEventListener('success', (e: Event) => {
from(
this.authService.sdk.httpClient.hooks?.afterRequest!(
this.authService.descopeSdk.httpClient.hooks?.afterRequest!(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{} as any,
new Response(JSON.stringify((e as CustomEvent).detail))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('DescopeAuthService', () => {
of({ ok: true, data: { sessionJwt: 'newToken' } })
);
// Taking 4 values from stream: first is initial value, next 3 are the result of refreshSession
service.descopeSession$.pipe(take(4), toArray()).subscribe({
service.session$.pipe(take(4), toArray()).subscribe({
next: (result) => {
expect(result.slice(1)).toStrictEqual([
{
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('DescopeAuthService', () => {
of({ ok: false, data: { sessionJwt: 'newToken' } })
);
// Taking 4 values from stream: first is initial value, next 3 are the result of refreshSession
service.descopeSession$.pipe(take(4), toArray()).subscribe({
service.session$.pipe(take(4), toArray()).subscribe({
next: (result) => {
expect(result.slice(1)).toStrictEqual([
{
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('DescopeAuthService', () => {
it('correctly handle descopeUser stream when user is successfully refreshed', (done: jest.DoneCallback) => {
meSpy.mockReturnValueOnce(of({ ok: true, data: { name: 'test' } }));
// Taking 4 values from stream: first is initial value, next 3 are the result of refreshUser
service.descopeUser$.pipe(take(4), toArray()).subscribe({
service.user$.pipe(take(4), toArray()).subscribe({
next: (result) => {
expect(result.slice(1)).toStrictEqual([
{ isUserLoading: true, user: undefined },
Expand All @@ -246,7 +246,7 @@ describe('DescopeAuthService', () => {
it('correctly handle descopeUser stream when refresh session failed', (done: jest.DoneCallback) => {
meSpy.mockReturnValueOnce(of({ ok: false }));
// Taking 3 values from stream: first is initial value, next 2 are the result of refreshUser
service.descopeUser$.pipe(take(3), toArray()).subscribe({
service.user$.pipe(take(3), toArray()).subscribe({
next: (result) => {
expect(result.slice(1)).toStrictEqual([
{ isUserLoading: true, user: undefined },
Expand Down
34 changes: 19 additions & 15 deletions projects/angular-sdk/src/lib/services/descope-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export type DescopeUser = { user?: UserResponse; isUserLoading: boolean };
providedIn: 'root'
})
export class DescopeAuthService {
public sdk: AngularDescopeSDK;
public descopeSdk: AngularDescopeSDK;
private readonly sessionSubject: BehaviorSubject<DescopeSession>;
private readonly userSubject: BehaviorSubject<DescopeUser>;
readonly descopeSession$: Observable<DescopeSession>;
readonly descopeUser$: Observable<DescopeUser>;
readonly session$: Observable<DescopeSession>;
readonly user$: Observable<DescopeUser>;

constructor(config: DescopeAuthConfig) {
this.sdk = observabilify<DescopeSDK>(
this.descopeSdk = observabilify<DescopeSDK>(
createSdk({
...config,
persistTokens: isBrowser() as true,
Expand All @@ -42,13 +42,13 @@ export class DescopeAuthService {
isSessionLoading: false,
sessionToken: ''
});
this.descopeSession$ = this.sessionSubject.asObservable();
this.session$ = this.sessionSubject.asObservable();
this.userSubject = new BehaviorSubject<DescopeUser>({
isUserLoading: false
});
this.descopeUser$ = this.userSubject.asObservable();
this.sdk.onSessionTokenChange(this.setSession.bind(this));
this.sdk.onUserChange(this.setUser.bind(this));
this.user$ = this.userSubject.asObservable();
this.descopeSdk.onSessionTokenChange(this.setSession.bind(this));
this.descopeSdk.onUserChange(this.setUser.bind(this));
}

refreshSession() {
Expand All @@ -57,7 +57,7 @@ export class DescopeAuthService {
...beforeRefreshSession,
isSessionLoading: true
});
return this.sdk.refresh().pipe(
return this.descopeSdk.refresh().pipe(
tap((data) => {
const afterRequestSession = this.sessionSubject.value;
if (data.ok && data.data) {
Expand Down Expand Up @@ -90,7 +90,7 @@ export class DescopeAuthService {
...beforeRefreshUser,
isUserLoading: true
});
return this.sdk.me().pipe(
return this.descopeSdk.me().pipe(
tap((data) => {
const afterRequestUser = this.userSubject.value;
if (data.data) {
Expand All @@ -115,7 +115,9 @@ export class DescopeAuthService {
getSessionToken() {
if (isBrowser()) {
return (
this.sdk as AngularDescopeSDK & { getSessionToken: () => string | null }
this.descopeSdk as AngularDescopeSDK & {
getSessionToken: () => string | null;
}
).getSessionToken();
}
console.warn('Get session token is not supported in SSR');
Expand All @@ -125,10 +127,12 @@ export class DescopeAuthService {
getRefreshToken() {
if (isBrowser()) {
return (
this.sdk as AngularDescopeSDK & { getRefreshToken: () => string | null }
this.descopeSdk as AngularDescopeSDK & {
getRefreshToken: () => string | null;
}
).getRefreshToken();
}
this.sdk.getJwtPermissions;
this.descopeSdk.getJwtPermissions;
console.warn('Get refresh token is not supported in SSR');
return '';
}
Expand All @@ -138,15 +142,15 @@ export class DescopeAuthService {
console.error('Could not get JWT Permissions - not authenticated');
return [];
}
return this.sdk.getJwtPermissions(token, tenant);
return this.descopeSdk.getJwtPermissions(token, tenant);
}

getJwtRoles(token = this.getSessionToken(), tenant?: string) {
if (token === null) {
console.error('Could not get JWT Roles - not authenticated');
return [];
}
return this.sdk.getJwtRoles(token, tenant);
return this.descopeSdk.getJwtRoles(token, tenant);
}

isAuthenticated() {
Expand Down
8 changes: 4 additions & 4 deletions projects/demo-app/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class HomeComponent implements OnInit {
roles: string[] = [];
userName: string = '';
stepUpConfigured = (environment.descopeStepUpFlowId ?? '').length > 0;
backendUrl = environment.descopeBackendUrl ?? '';
backendUrl = environment.backendUrl ?? '';

constructor(
private router: Router,
Expand All @@ -24,13 +24,13 @@ export class HomeComponent implements OnInit {
) {}

ngOnInit() {
this.authService.descopeSession$.subscribe((session) => {
this.authService.session$.subscribe((session) => {
this.isAuthenticated = session.isAuthenticated;
if (session.sessionToken) {
this.roles = this.authService.getJwtRoles(session.sessionToken);
}
});
this.authService.descopeUser$.subscribe((descopeUser) => {
this.authService.user$.subscribe((descopeUser) => {
if (descopeUser.user) {
this.userName = descopeUser.user.name ?? '';
}
Expand All @@ -42,7 +42,7 @@ export class HomeComponent implements OnInit {
}

logout() {
this.authService.sdk.logout();
this.authService.descopeSdk.logout();
}

fetchData() {
Expand Down
2 changes: 1 addition & 1 deletion projects/demo-app/src/environments/conifg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface Env {
descopeTenantId?: string;
descopeTelemetryKey?: string;
descopeStepUpFlowId?: string;
descopeBackendUrl?: string;
backendUrl?: string;
}
2 changes: 1 addition & 1 deletion projects/demo-app/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const environment: Env = {
descopeTenantId: '',
descopeTelemetryKey: '',
descopeStepUpFlowId: '',
descopeBackendUrl: ''
backendUrl: ''
};

0 comments on commit cbed1af

Please sign in to comment.