Authentication
@nuclia/core • Docs
@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
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
• tokens: AuthTokens
Returns
boolean
Implementation of
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()
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)
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:46
getAuthInfo()
getAuthInfo(
includeIP):Observable<AuthInfo>
Returns authentication information
Parameters
• includeIP: boolean = false
Returns
Observable<AuthInfo>
Implementation of
Defined in
libs/sdk-core/src/lib/auth/auth.ts:256
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
Defined in
libs/sdk-core/src/lib/auth/auth.ts:269
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:279
getToken()
getToken(
force?):string
Returns authentication token stored in localStorage.
Parameters
• force?: boolean
Returns
string
Implementation of
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
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
• username: string
• password: string
• validation?: string
Returns
Observable<boolean>
Implementation of
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
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
• password: string
Returns
Observable<boolean>