npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smartbit4all/session

v1.3.7

Published

## References

Downloads

16

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.