Skip to content
Closed
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
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,35 @@ export class AppComponent implements OnInit {
}
```

6. Enjoy!
### 6. Using signals
In order to use frontegg signals you will have to call it from the frontegg services and assign them to the component state

```
import { Component, OnInit, Signal } from '@angular/core';
import { FronteggAppService, FronteggAuthService, AuthState } from '@frontegg/angular';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
})
export class AppComponent implements OnInit {
user: Signal<AuthState['user'] | undefined>
authenticated: Signal<boolean | undefined>
constructor(private fronteggAppService: FronteggAppService,
private fronteggAuthService: FronteggAuthService,
private router: Router) {
this.user = this.fronteggAuthService.signals.user
this.authenticated = this.fronteggAppService.signals.isAuthenticated
}
}
```

Then access it from the html component file
```
<div>
<p>Authenticated: {{authenticated()}}</p>
<p>Authenticated as: {{user()?.name}}</p>
</div>
```

7. Enjoy!
33 changes: 32 additions & 1 deletion projects/frontegg-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,35 @@ export class AppComponent implements OnInit {
}
```

6. Enjoy!
### 6. Using signals
In order to use frontegg signals you will have to call it from the frontegg services and assign them to the component state

```
import { Component, OnInit, Signal } from '@angular/core';
import { FronteggAppService, FronteggAuthService, AuthState } from '@frontegg/angular';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
})
export class AppComponent implements OnInit {
user: Signal<AuthState['user'] | undefined>
authenticated: Signal<boolean | undefined>
constructor(private fronteggAppService: FronteggAppService,
private fronteggAuthService: FronteggAuthService,
private router: Router) {
this.user = this.fronteggAuthService.signals.user
this.authenticated = this.fronteggAppService.signals.isAuthenticated
}
}
```

Then access it from the html component file
```
<div>
<p>Authenticated: {{authenticated()}}</p>
<p>Authenticated as: {{user()?.name}}</p>
</div>
```

7. Enjoy!
11 changes: 8 additions & 3 deletions projects/frontegg-app/src/lib/frontegg-app.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable, NgZone, Inject } from '@angular/core';
import { Route, Router, ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
import { FronteggApp, initialize } from '@frontegg/js';
import { AuthPageRoutes,FronteggState, isAuthRoute } from '@frontegg/redux-store';
import { AuthPageRoutes, FronteggState, isAuthRoute } from '@frontegg/redux-store';
import { FronteggAppOptions, FronteggCheckoutDialogOptions } from '@frontegg/types';
import { BehaviorSubject, Observable } from 'rxjs';
import { ContextHolder, RedirectOptions, FronteggFrameworks } from '@frontegg/rest-api';
import { FronteggComponent } from './frontegg.component';
import sdkVersion from '../sdkVersion';
import angularCoreVersion from '@angular/core/package.json';
import { mapObservablesToSignals } from './v16';

export class FronteggAppOptionsClass implements FronteggAppOptions {
contextOptions: FronteggAppOptions['contextOptions'] = {
Expand Down Expand Up @@ -66,6 +67,10 @@ export class FronteggAppService {
return this.isAuthenticatedSubject.asObservable();
};

get signals() {
return mapObservablesToSignals(this);
}

constructor(@Inject(FronteggAppOptionsClass) private config: FronteggAppOptions, public router: Router, private ngZone: NgZone) {
if (!this.config) {
throw Error('Need to pass config: FronteggConfigOptions in FronteggAppModule.forRoot(config)');
Expand Down Expand Up @@ -117,8 +122,8 @@ export class FronteggAppService {
...this.mapAuthComponents,
{
path: '',
canActivate: [ FronteggLoadGuard ],
children: [ ...this.router.config ],
canActivate: [FronteggLoadGuard],
children: [...this.router.config],
},
]);
const initialFronteggState = this.fronteggApp.store.getState() as FronteggState;
Expand Down
5 changes: 5 additions & 0 deletions projects/frontegg-app/src/lib/frontegg-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {
import type { FronteggState, ActivateAccountState, SocialLoginState } from '@frontegg/redux-store';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { mapObservablesToSignals } from './v16';

interface AuthSubStates {
field: Partial<keyof AuthState>;
Expand Down Expand Up @@ -225,6 +226,10 @@ export class FronteggAuthService {
return this.ssoACSSubject.asObservable();
}

get signals() {
return mapObservablesToSignals(this);
}

constructor(private fronteggAppService: FronteggAppService, private router: Router) {
const authSubStates: AuthSubStates[] = [
{ field: 'acceptInvitationState', subject: this.acceptInvitationStateSubject },
Expand Down
1 change: 1 addition & 0 deletions projects/frontegg-app/src/lib/v16/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SIGNALS_ERROR_MESSAGE = 'Signals only supported from angular version 16, in order to use frontegg signals please upgrade your angular version to 16 or higher';
1 change: 1 addition & 0 deletions projects/frontegg-app/src/lib/v16/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mapObservablesToSignals';
4 changes: 4 additions & 0 deletions projects/frontegg-app/src/lib/v16/isAngular16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import angularCoreVersion from '@angular/core/package.json';

const major = Number(angularCoreVersion?.version?.split('.')?.[0] ?? 0);
export const isAngular16 = major >= 16;
19 changes: 19 additions & 0 deletions projects/frontegg-app/src/lib/v16/mapObservablesToSignals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SIGNALS_ERROR_MESSAGE } from './consts';
import { isAngular16 } from './isAngular16';
import { Signal, toSignal } from './signals';

export const mapObservablesToSignals = <T>(obj: T) => {
if (!isAngular16) {
throw new Error(SIGNALS_ERROR_MESSAGE)
}
const observables = Object.getOwnPropertyNames(Object.getPrototypeOf(obj))
.filter(prop => prop.endsWith("$"));

const fields: Record<string, Signal> = {};
observables.forEach(observable => {
const fieldName = observable.slice(0, -1); // Remove the trailing '$'

fields[fieldName] = toSignal(obj[observable as keyof typeof obj]);
})
return fields;
}
21 changes: 21 additions & 0 deletions projects/frontegg-app/src/lib/v16/signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @ts-ignore Signal is not exported from angular/core before version 16 **/
import type { Signal } from '@angular/core';
import { SIGNALS_ERROR_MESSAGE } from './consts';
import { isAngular16 } from './isAngular16';

let toSignal = (observable: any) => {
if (!isAngular16) {
throw new Error(SIGNALS_ERROR_MESSAGE)
}
}

if (isAngular16) {
const toSignalImportPath = '@angular/core/rxjs-interop'
import(toSignalImportPath)
.then(({ toSignal: angularToSignal }) => {
toSignal = angularToSignal;
}).catch(() => { });
}


export { toSignal, Signal }