Skip to main content

@nuclia/core / Exports / Authentication

Class: Authentication

It manages authentication to the Nuclia backend. It can be based on login/password for account authentication, or on an API key for private Knowledge Box authentication. Authentication is not necessary when using a public Knowledge Box.

Implements

Table of contents

Constructors

Methods

Constructors

constructor

new Authentication(nuclia)

Parameters

NameType
nucliaINuclia

Defined in

libs/sdk-core/src/lib/auth/auth.ts:27

Methods

authenticate

authenticate(tokens): boolean

Returns a boolean if successful. Stores authentication tokens in localStorage and triggers isAuthenticated.

This method is automatically called when using login and can be useful when using a custom authentication flow.

Example:

nuclia.auth.authenticate(tokens);

Parameters

NameType
tokensAuthTokens

Returns

boolean

Implementation of

IAuthentication.authenticate

Defined in

libs/sdk-core/src/lib/auth/auth.ts:164


deleteAuthenticatedUser

deleteAuthenticatedUser(): Observable<void>

Deletes current user account and removes stored tokens.

Example:

nuclia.auth.deleteAuthenticatedUser().subscribe(() => {
console.log('User deleted');
});

Returns

Observable<void>

Implementation of

IAuthentication.deleteAuthenticatedUser

Defined in

libs/sdk-core/src/lib/auth/auth.ts:251


getAuthHeaders

getAuthHeaders(): Object

Returns the authentication header (which will be Authorization for account authentication, or X-NUCLIA-SERVICEACCOUNT for private Knowledge Box authentication).

Example:

const headers = nuclia.auth.getAuthHeaders();

Returns

Object

Implementation of

IAuthentication.getAuthHeaders

Defined in

libs/sdk-core/src/lib/auth/auth.ts:45

getAuthHeaders(method, path): Object

Parameters

NameType
methodstring
pathstring

Returns

Object

Implementation of

IAuthentication.getAuthHeaders

Defined in

libs/sdk-core/src/lib/auth/auth.ts:46


getJWTUser

getJWTUser(): null | JwtUser

Parses JWT token and returns corresponding user information.

Example:

const user = nuclia.auth.getJWTUser();
console.log(`Hello ${user?.ext.first_name}!`);

Returns

null | JwtUser

Implementation of

IAuthentication.getJWTUser

Defined in

libs/sdk-core/src/lib/auth/auth.ts:264


getRefreshToken

getRefreshToken(): string

Returns refresh token stored in localStorage.

Returns

string

Implementation of

IAuthentication.getRefreshToken

Defined in

libs/sdk-core/src/lib/auth/auth.ts:274


getToken

getToken(force?): string

Returns authentication token stored in localStorage.

Parameters

NameType
force?boolean

Returns

string

Implementation of

IAuthentication.getToken

Defined in

libs/sdk-core/src/lib/auth/auth.ts:202


hasLoggedOut

hasLoggedOut(): Observable<boolean>

Returns an Observable emitting when the user has logged out.

Example:

nuclia.auth.hasLoggedOut().subscribe((loggedOut) => {
if (loggedOut) {
// do something
}
});

Returns

Observable<boolean>

Implementation of

IAuthentication.hasLoggedOut

Defined in

libs/sdk-core/src/lib/auth/auth.ts:115


isAuthenticated

isAuthenticated(): Observable<boolean>

Emits when the authentication status changes.

Example:

nuclia.auth.isAuthenticated().subscribe((isAuthenticated) => {
if (isAuthenticated) {
console.log('You are authenticated');
} else {
console.log('You are not authenticated');
}
});

Returns

Observable<boolean>

Implementation of

IAuthentication.isAuthenticated

Defined in

libs/sdk-core/src/lib/auth/auth.ts:99


login

login(username, password, validation?): Observable<boolean>

Calls the login endpoint for account authentication and emits when done.

It can optionally take a reCaptcha validation code if the Nuclia backend requires it. Once authenticated, the Nuclia SDK will periodically refresh the token before it expires.

Example:

nuclia.auth.login(username, password).subscribe({
next: (success) => {
this.loginError = success ? '' : 'Error';
console.log('logged in', success);
},
error: (error) => {
this.loginError = 'Error';
console.error(error);
},
complete: () => {
this.pending = false;
},
});

Parameters

NameType
usernamestring
passwordstring
validation?string

Returns

Observable<boolean>

Implementation of

IAuthentication.login

Defined in

libs/sdk-core/src/lib/auth/auth.ts:145


logout

logout(): void

Calls the logout endpoint and removes the token stored in localStorage.

Returns

void

Implementation of

IAuthentication.logout

Defined in

libs/sdk-core/src/lib/auth/auth.ts:176


refresh

refresh(): Observable<boolean>

Returns

Observable<boolean>

Defined in

libs/sdk-core/src/lib/auth/auth.ts:183


setPassword

setPassword(password): Observable<boolean>

Sets the current user’s password.

Example:

nuclia.auth.setPassword(password).subscribe({
next: (success) => {
this.passwordError = success ? '' : 'Error';
console.log('password set', success);
},
error: (error) => {
this.passwordError = 'Error';
console.error(error);
},
complete: () => {
this.pending = false;
},
});

Parameters

NameType
passwordstring

Returns

Observable<boolean>

Implementation of

IAuthentication.setPassword

Defined in

libs/sdk-core/src/lib/auth/auth.ts:235