@smartbit4all/session
v1.3.7
Published
## References
Downloads
16
Keywords
Readme
Smart Session
References
These packages must be updated in case of a new version:
How to use
Installation
Go to your project, open the terminal and use the following command:
npm i @smartbit4all/session
Provide the SmartSessionService
as a global service in the app.module:
app.module.ts:
@NgModule({
declarations: [...],
imports: [
SmartSessionModule,
...
],
providers: [
SmartSessionService,
...
],
...
})
Usage
In order to use the SmartSessionService I recommend to follow the guideline belove.
Generate a service which will handle the session and the authentication.
Terminal:
>>> cd src/app
>>> mkdir services
>>> cd ./services
>>> ng g s authentication
Provide this new service as a global service:
app.module.ts:
@NgModule({
declarations: [...],
imports: [...],
providers: [
SmartSessionService,
AuthenticationService,
...
],
...
})
Use dependency injection to inject this new service into your component(s). You must inject this into all components which the application could start with (for instance AppComponent or LoginComponent).
app.component.ts:
export class AppComponent / LoginComponent {
constructor(private auth: AuthenticationService) { ... }
}
The AuthenticationService must be prepared to use the SmartSessionService.
authentication.service.ts:
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
private url?: string;
constructor(
private router: Router,
private session: SmartSessionService
) {
// set up SmartSessionService
this.url = 'http://localhost:9171/api';
this.session.setUrl(this.url);
this.session.setCookieName('example-cookie-name');
this.startSession();
}
async startSession(): Promise<void> {
await this.session.initialize();
}
async getSession(): Promise<SessionInfoData> {
return this.session.getSession();
}
async isAuthenticated(): Promise<boolean> {
return this.session.getIsAuthenticated();
}
}
Authentication state changed
The change of authentication state can be detected in any components. To handle it you must subscribe to the change itself.
Note that in this version a user is authenticated when the response of the GET session contains a list of authentications.
this.isAuthenticated = response.authentications.length > 0
any.component.ts:
constructor(
private auth: AuthenticationService,
private session: SmartSessionService
) {
this.session.authenticationStateChanged.subscribe((isAuth: boolean) => {
if (!isAuth) {
this.router.navigate(['/login']);
}
});
}
Using locales
The SmartSessionService
supports the usage of locales in you application. It is important to note that the language is related to the session!
This example uses the NgxTranslate/core package.
authentication.service.ts:
import { TranslateService } from '@ngx-translate/core';
...
export class AuthenticationService {
...
constructor(
...
private translate: TranslateService
) {
this.session.localeChanged.subscribe(() => {
this.translate.use(this.session.getLocale());
});
}
async changeLocale(locale: string): Promise<void> {
await this.session.changeLocale(locale);
}
getLocale(): string {
return this.session.getLocale();
}
}
SmartDevTool usage
Turn on and off the devTool:
// active
localStorage.setItem('smartDevToolActive', 'true');
// inactive
localStorage.setItem('smartDevToolActive', 'false');
Set the devTool's button visibility
// visible
localStorage.setItem('useDevTool', 'true');
// invisible
localStorage.setItem('useDevTool', 'false');
Version logs
@smartbit4all/session v1.3.5
Type: Update
The SmartSessionService
got a new functionality, which is called as SmartSessionTimer
. This timer can be easily injected into HTML:
<smart-session-timer></smart-session-timer>
@smartbit4all/session v1.3.0
Type: Update
Cookie handling
The ngx-cookie-service dependency was removed and it is not used anymore by the SmartSessionService
.
SmartDevTool
A tool called SmartDevTool has been introduced in order to help the developers to debug the API calls.
@smartbit4all/session v1.2.0
Type: Update
This version contains bugfixes and a new feature which helps to use locales in the application.
The SmartSessionService
got a localeChanged property which emits an event when the locale of the session is changed.
New functions related to locales have been added:
public getLocale(): string { ... }
public async changeLocale(locale: string): Promise<void> { ... }
@smartbit4all/session v1.1.3
Type: Bugfix
Session refresh bugfixes.
@smartbit4all/session v1.1.0
Type: Feature
The SmartSession
package had a burning issue about refreshing the session when it expires. The problem has been solved by an interceptor called SmartErrorCatchingInterceptor
which refreshes the token with the refresh token and retries the original API call.
@smartbit4all/session v1.0.4
Type: Bugfix
This update fixes a bug which caused that even if the user has logged out, the token sent by the bff api services were remained the same.
Type: Feature
The SmartSessionService
got a new function called clearAndStartNewSession
which can be called when the user logs out.
@smartbit4all/session v1.0.3
Type: Bugfix
A bug has been fixed which caused duplicated header parameters.
@smartbit4all/session v1.0.1
Type: Update
The package has been published.
@smartbit4all/session v0.1.0
Type: Feature
The SmartSession has been created.