Skip to content

Commit

Permalink
BFF with auto-refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Oct 2, 2023
1 parent b0b1da7 commit 8b4b83e
Show file tree
Hide file tree
Showing 38 changed files with 383 additions and 739 deletions.
2 changes: 1 addition & 1 deletion samples/tutorials/bff/angular/bff-gateway.openapi.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost:8080","description":"Generated server url"}],"paths":{"/logout":{"put":{"tags":["logout","Gateway"],"operationId":"logout","responses":{"204":{"description":"No Content"}}}},"/backchannel_logout":{"post":{"tags":["back-channel-logout-controller"],"operationId":"backChannelLogout","responses":{"200":{"description":"OK"}}}},"/me":{"get":{"tags":["getMe","Gateway"],"operationId":"getMe","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/UserDto"}}}}}}},"/login-options":{"get":{"tags":["Gateway","getLoginOptions"],"operationId":"getLoginOptions","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"type":"array","items":{"$ref":"#/components/schemas/LoginOptionDto"}}}}}}}}},"components":{"schemas":{"UserDto":{"type":"object","properties":{"subject":{"type":"string"},"issuer":{"type":"string"},"roles":{"type":"array","items":{"type":"string"}}}},"LoginOptionDto":{"required":["label","loginUri"],"type":"object","properties":{"label":{"type":"string"},"loginUri":{"type":"string"}}}}}}
{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost:8080","description":"Generated server url"}],"paths":{"/logout":{"put":{"tags":["logout","Gateway"],"operationId":"logout","responses":{"204":{"description":"No Content"}}}},"/login-options":{"get":{"tags":["Gateway","getLoginOptions"],"operationId":"getLoginOptions","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"type":"array","items":{"$ref":"#/components/schemas/LoginOptionDto"}}}}}}}}},"components":{"schemas":{"LoginOptionDto":{"required":["label","loginUri"],"type":"object","properties":{"label":{"type":"string"},"loginUri":{"type":"string"}}}}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"openapi":"3.0.1","info":{"title":"Users API","version":"1.0.0"},"servers":[{"url":"http://localhost:6443","description":"Generated server url"}],"security":[{"OAuth2":[]}],"paths":{"/greetings":{"get":{"tags":["get","Greetings"],"operationId":"getGreeting","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/GreetingDto"}}}}}}}},"components":{"schemas":{"GreetingDto":{"required":["message"],"type":"object","properties":{"message":{"type":"string"}}}}}}
{"openapi":"3.0.1","info":{"title":"Users API","version":"1.0.0"},"servers":[{"url":"http://localhost:7443","description":"Generated server url"}],"security":[{"OAuth2":[]}],"paths":{"/users/me":{"get":{"tags":["getMe","Users"],"operationId":"getMe","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/UserInfo"}}}}}}},"/greetings/public":{"get":{"tags":["getPublicGreeting","Greetings"],"operationId":"getGreeting","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/GreetingDto"}}}}}}},"/greetings/nice":{"get":{"tags":["getNiceGreeting","Greetings"],"operationId":"getNiceGreeting","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/GreetingDto"}}}}}}}},"components":{"schemas":{"UserInfo":{"required":["email","exp","iss","name","roles"],"type":"object","properties":{"name":{"type":"string"},"iss":{"type":"string"},"roles":{"type":"array","items":{"type":"string"}},"exp":{"minimum":0,"type":"integer","format":"int64"},"email":{"type":"string"}}},"GreetingDto":{"required":["message"],"type":"object","properties":{"message":{"type":"string"}}}}}}
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { GatewayApi, LoginOptionDto } from '@c4-soft/gateway-api';
import { UsersApi } from '@c4-soft/greetings-api';
import { interval } from 'rxjs';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
import { lastValueFrom } from 'rxjs/internal/lastValueFrom';
import { Observable } from 'rxjs/internal/Observable';
import { Subscription } from 'rxjs/internal/Subscription';
import { lastValueFrom } from 'rxjs/internal/lastValueFrom';

@Injectable({
providedIn: 'root',
})
export class UserService {
private user$ = new BehaviorSubject<User>(User.ANONYMOUS);
private refreshSub?: Subscription;

constructor(private gatewayApi: GatewayApi, private http: HttpClient) {
constructor(
private gatewayApi: GatewayApi,
private usersApi: UsersApi,
private http: HttpClient
) {
this.refresh();
}

refresh(): void {
this.gatewayApi.getMe().subscribe({
this.refreshSub?.unsubscribe()
this.usersApi.getMe().subscribe({
next: (user) => {
console.info(user);
this.user$.next(
user.subject
? new User(
user.subject,
user.issuer || '',
user.roles || []
)
user.name
? new User(user.name, user.iss || '', user.roles || [])
: User.ANONYMOUS
);
const now = Date.now();
const delay = (1000 * user.exp - now) * 0.8;
if (delay > 10000) {
this.refreshSub = interval(delay).subscribe(() => this.refresh());
}
},
error: (error) => {
console.warn(error);
Expand All @@ -41,14 +50,16 @@ export class UserService {
}

async logout() {
lastValueFrom(this.gatewayApi.logout('response')).then((resp) => {
const logoutUri = resp.headers.get('location') || '';
if (logoutUri) {
window.location.href = logoutUri;
}
}).finally(() => {
this.user$.next(User.ANONYMOUS)
});
lastValueFrom(this.gatewayApi.logout('response'))
.then((resp) => {
const logoutUri = resp.headers.get('location') || '';
if (logoutUri) {
window.location.href = logoutUri;
}
})
.finally(() => {
this.user$.next(User.ANONYMOUS);
});
}

get loginOptions(): Observable<Array<LoginOptionDto>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
README.md
api.module.ts
api/api.ts
api/backChannelLogoutController.service.ts
api/backChannelLogoutController.serviceInterface.ts
api/gateway.service.ts
api/gateway.serviceInterface.ts
api/getLoginOptions.service.ts
api/getLoginOptions.serviceInterface.ts
api/getMe.service.ts
api/getMe.serviceInterface.ts
api/logout.service.ts
api/logout.serviceInterface.ts
configuration.ts
encoder.ts
index.ts
model/loginOptionDto.ts
model/models.ts
model/userDto.ts
package.json
param.ts
variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@ wwwroot/*.js
node_modules
typings
dist

api/
model/

api.module.ts
configuration.ts
encoder.ts
git_push.sh
index.ts
karma.conf.js
package.json
package-lock.json
param.ts
README.md
variables.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
README.md
api.module.ts
api/api.ts
api/get.service.ts
api/get.serviceInterface.ts
api/getMe.service.ts
api/getMe.serviceInterface.ts
api/getNiceGreeting.service.ts
api/getNiceGreeting.serviceInterface.ts
api/getPublicGreeting.service.ts
api/getPublicGreeting.serviceInterface.ts
api/greetings.service.ts
api/greetings.serviceInterface.ts
api/users.service.ts
api/users.serviceInterface.ts
configuration.ts
encoder.ts
git_push.sh
index.ts
model/greetingDto.ts
model/models.ts
model/userInfo.ts
package.json
param.ts
variables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
export * from './get.service';
import { GetApi } from './get.service';
export * from './get.serviceInterface';
export * from './getMe.service';
import { GetMeApi } from './getMe.service';
export * from './getMe.serviceInterface';
export * from './getNiceGreeting.service';
import { GetNiceGreetingApi } from './getNiceGreeting.service';
export * from './getNiceGreeting.serviceInterface';
export * from './getPublicGreeting.service';
import { GetPublicGreetingApi } from './getPublicGreeting.service';
export * from './getPublicGreeting.serviceInterface';
export * from './greetings.service';
import { GreetingsApi } from './greetings.service';
export * from './greetings.serviceInterface';
export const APIS = [GetApi, GreetingsApi];
export * from './users.service';
import { UsersApi } from './users.service';
export * from './users.serviceInterface';
export const APIS = [GetMeApi, GetNiceGreetingApi, GetPublicGreetingApi, GreetingsApi, UsersApi];

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8b4b83e

Please sign in to comment.