# ngx-core-services

> ⚡ Try it on Stackblitz: https://stackblitz.com/edit/ngx-core-services

Latest version **1.0.30** (published 2020-09-28) · 0 weekly downloads

## Install

```sh
npm install ngx-core-services
pnpm add ngx-core-services
yarn add ngx-core-services
bun add ngx-core-services
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.30 |
| Published | 2020-09-28 |
| First published | 2020-01-30 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 573.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | rhideg |
| Keywords | storage, auth, interceptor, api |

## Links

- npm: https://www.npmjs.com/package/ngx-core-services
- npm.io page: https://npm.io/package/ngx-core-services

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.0.30 (latest) — 2020-09-28
- 1.0.29 — 2020-07-07
- 1.0.28 — 2020-07-07
- 1.0.27 — 2020-06-12
- 1.0.26 — 2020-06-08
- 1.0.25 — 2020-06-08
- 1.0.24 — 2020-06-08
- 1.0.23 — 2020-06-08
- 1.0.22 — 2020-06-08
- 1.0.21 — 2020-05-07
- 1.0.20 — 2020-05-07
- 1.0.19 — 2020-05-07
- 1.0.18 — 2020-05-07
- 1.0.17 — 2020-05-07
- 1.0.16 — 2020-05-07
- … 42 more at https://npm.io/package/ngx-core-services/versions

## README

# NgxCore

⚡ Try it on Stackblitz: https://stackblitz.com/edit/ngx-core-services

# Getting started

## Installation:

Install via npm package manager

```npm i ngx-core-services```

## Dependencies:

```
 @auth0/angular-jwt
 jwt-decode
```

## Usage:

### Module:

```js
import {
  NgxCoreModule,
  JwtInterceptorService,
  ErrorInterceptorService,
  AuthService,
  StorageService,
  ApiService,
  RefreshTokenInterceptor
} from "ngx-core-services";
```

```js
imports: [
...
	// Authentication server, Api server, Project name
	NgxCoreModule.forRoot({
		authHost:  'test', //environment.host,
		apiHost:  'test',//environment.host,
		project:  'test'//environment.name,
		refreshType: 0 // refresh token refresh type, if 0 it refreshes access token auto, else navigates to route ''.
	}),
...
],
providers: [
...
	providers: [
    {
		provide: LocationStrategy,
		useClass: HashLocationStrategy
	},
	{ 	provide: HTTP_INTERCEPTORS,
		useClass: JwtInterceptorService, // Intercept http requests and add jwt to the header
		multi: true
	},
    {
		provide: HTTP_INTERCEPTORS,
		useClass: ErrorInterceptorService, // Checks for http errors
		multi: true
	},
    {
		provide: HTTP_INTERCEPTORS,
		useClass: RefreshTokenInterceptor, // Refresh token interceptor
		multi: true
	},
    AuthService,
    StorageService,
    ApiService,
  ],
...
]
```

## NgxCoreModule:

```js
export interface NgxCoreConfig {
  authHost?: string; // API url for the authentication server
  apiHost?: string; // API url for the API server
  project?: string; // Project name
  refreshType?: number; // Refresh token refresh type
}
```

## ApiService:

```js
get(url: string, data?: any) // HTTP GET request
put(url: string, data?: any) // HTTP PUT request
post(url: string, data?: any) // HTTP POST request
delete(url: string, data?: any) // HTTP DELETE request
```

## StorageService:

### Cookies:

```js
setCookie((name: string), (val: string)); // Set cookie
getCookie((name: string)); // Returns with a cookie associated with the given name
deleteCookie((name: string)); // Deletes a cookie by name
clearCookies(); // Clears cookies
```

### LocalStorage:

```js
setStorage((name: string), (val: string)); // Set local storage
getStorage((name: string)); // Returns with a storage value associated with the given name
deleteStorage((name: string)); // Deletes a storage by name
clearStorage(); // Clears the storage
```

## AuthService:

```js
public  canActivate(route: ActivatedRouteSnapshot):boolean // Route guard
public  isAuthenticated(): boolean // Has token and isvalid
public  getToken() // Returns the saved access_token
public  login(url: string, data) // User login, if data['refreshType'] is given then sets refreshType (concats url to authHost)
public  getRole() // Get user role from access_token identity
public  getUsername() // Get username from access_token identity
public  getName() // Get name from access_token identity
public  getUserData() // Get user data from access_token identity
public  change(data: any) // Data change subject
public  logout(url: string, data?) // User logout, cookie and storage clear.
```

## JwtInterceptorService:

```js
intercept((request: HttpRequest<any>), (next: HttpHandler)); // sets the access_token or refresh_token
```

## ErrorInterceptorService:

```js
intercept((request: HttpRequest<any>), (next: HttpHandler)); // throws error if something bad happens
```

## RefreshTokenInterceptorService:

```js
intercept((request: HttpRequest<any>), (next: HttpHandler)); // sets the refresh token if access_token has expired
```

## AppState

```js
setAppState(); // Sets localstorage key, and loggedIn
getAppState(); // Returns app state
getAppStateByKey(key: string); // Returns app state by key
changeAppState(obj: any); // Changes app state e.g.: obj={'loggedIn': true}
```

### Example:
##### App Component:
```js
import { AppStateService } from "ngx-core-services";
...
constructor(private appStateService: AppStateService){
}
ngOnInit(): void {
  this.appStateService.setAppState();
}
```

## ViewState

```js
getViewState(); // Sets the viewState - only use it in the view constructor!
setViewState(obj: any); // Returns the viewState object
getViewStateByKey(key: string); // Returns data that maches the given parameter.
getView(); // Returns the current view.
changeViewState(obj: any, history?); // Changes the view state. Sets the goven data and history, emits the current change.
```
### Example:
##### View1:
```js
import { ViewStateService } from "ngx-core-services";
...
constructor(private viewStateService: ViewStateService) {
  this.viewStateService.setViewState({view: 'View1'});
}
```
##### Component1:
```js
test = 0;
constructor(private viewStateService: ViewStateService) {
    this.viewStateService.datachange.subscribe(data => {
        this.test = data.get('test');
        // write your code
    });
}
...
btnTest_Click() {
    this.test++;
    this.viewStateService.changeViewState({ test: this.test, test: 'test' }, 'btnTest_Click'); // data object and history, you don't need to change the view's name, only give the data and the history.
}
```

---
_Source: https://npm.io/package/ngx-core-services · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
