Skip to main content

Authentication

@nuclia/coreDocs


@nuclia/core / 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

Constructors

new Authentication()

new Authentication(nuclia): Authentication

Parameters

nuclia: INuclia

Returns

Authentication

Defined in

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

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

tokens: AuthTokens

Returns

boolean

Implementation of

IAuthentication.authenticate

Defined in

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


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:362


getAuthHeaders()

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:47

getAuthHeaders(method, path)

getAuthHeaders(method, path): object

Parameters

method: string

path: string

Returns

object

Implementation of

IAuthentication.getAuthHeaders

Defined in

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


getAuthInfo()

getAuthInfo(includeIP): Observable<AuthInfo>

Returns authentication information

Parameters

includeIP: boolean = false

Returns

Observable<AuthInfo>

Implementation of

IAuthentication.getAuthInfo

Defined in

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


getAuthUrl()

getAuthUrl(): string

Returns

string

Implementation of

IAuthentication.getAuthUrl

Defined in

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


getHydraUrl()

getHydraUrl(): string

Returns

string

Defined in

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


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:380


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:390


getToken()

getToken(force?): string

Returns authentication token stored in localStorage.

Parameters

force?: boolean

Returns

string

Implementation of

IAuthentication.getToken

Defined in

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


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:117


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:101


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:271


processAuthorizationResponse()

processAuthorizationResponse(authCode, returnedState): Observable<object>

Parameters

authCode: string

returnedState: string

Returns

Observable<object>

state

state: any

success

success: boolean

Implementation of

IAuthentication.processAuthorizationResponse

Defined in

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


redirectToOAuth()

redirectToOAuth(queryParams?): void

Parameters

queryParams?

Returns

void

Implementation of

IAuthentication.redirectToOAuth

Defined in

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


refresh()

refresh(): Observable<boolean>

Returns

Observable<boolean>

Defined in

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


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

password: string

Returns

Observable<boolean>

Implementation of

IAuthentication.setPassword

Defined in

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


validateMagicToken()

validateMagicToken(token, zone?): Observable<MagicAction>

Parameters

token: string

zone?: string

Returns

Observable<MagicAction>

Implementation of

IAuthentication.validateMagicToken

Defined in

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