Skip to content

Auth

Functions and types relating to the authentication of users in the Telestion frontend.

Note that in most cases, you don’t need to import anything from this package directly, since auth*n is already handled by the framework itself.

Example

import { ... } from '@wuespace/telestion/auth';

Classes

LoginError

Extends

Constructors

new LoginError()
new LoginError(messages): LoginError
Parameters
messages

ErrorMessages

Returns

LoginError

Overrides

Error.constructor

Properties

message
message: string;
Inherited from

Error.message

messages
messages: ErrorMessages;

name
name: string;
Inherited from

Error.name

stack?
optional stack: string;
Inherited from

Error.stack

prepareStackTrace()?
static optional prepareStackTrace: (err, stackTraces) => any;

Optional override for formatting stack traces

Parameters
err

Error

stackTraces

CallSite[]

Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from

Error.prepareStackTrace

stackTraceLimit
static stackTraceLimit: number;
Inherited from

Error.stackTraceLimit

Methods

captureStackTrace()
static captureStackTrace(targetObject, constructorOpt?): void

Create .stack property on a target object

Parameters
targetObject

object

constructorOpt?

Function

Returns

void

Inherited from

Error.captureStackTrace

Interfaces

ErrorMessages

Properties

natsUrlMessage?
optional natsUrlMessage: string;

passwordMessage?
optional passwordMessage: string;

usernameMessage?
optional usernameMessage: string;

User

A logged-in user.

Properties

natsUrl
natsUrl: string;

The NATS URL that the user is connected to.

username
username: string;

The user’s username.

Functions

attemptAutoLogin()

function attemptAutoLogin(): Promise<boolean>

Attempt to auto-login using credentials stored in sessionStorage.

The credentials will automatically be cleared if they are invalid, the session ends, or logout is called.

Credentials are automatically stored updated by login and logout.

Returns

Promise<boolean>

true if auto-login was successful, false otherwise


getNatsConnection()

function getNatsConnection(): null | NatsConnection

Returns

null | NatsConnection


getUser()

function getUser(): null | User

Returns the user object if the user is currently logged in, else returns null if no user is currently logged in.

Returns

null | User


isLoggedIn()

function isLoggedIn(): boolean

Checks if a user is logged in.

Returns

boolean

true if the user is logged in, false otherwise.


login()

function login(
   natsUrl, 
   username, 
password): Promise<null | User>

Logs in a user with the given credentials.

Parameters

natsUrl

string

The url to connect to the NATS server.

username

string

The username for authentication.

password

string

The password for authentication.

Returns

Promise<null | User>

A promise that resolves once the user is logged in. The resolved value is the logged-in user object.

Throws

Error If the provided credentials are incorrect.


logout()

function logout(): Promise<void>

Logs out the user if currently logged in.

Returns

Promise<void>

A promise that resolves once the user is logged out.


~~setAutoLoginCredentials()~~

function setAutoLoginCredentials(credentials): void

Sets credentials with which to auto-login.

If an auto-login attempt fails, the credentials will be cleared for the remainder of the session and a login form shown to the user. If the user logs in successfully, the credentials will be updated.

Security Warning

Use this function only if user authentication is handled by a separate system. Calling this function in your application means your NATS credentials will be hard-coded into your application, which is a security risk.

Parameters

credentials

The credentials to store

natsUrl

string = ...

The URL of the NATS server to connect to.

password

string = ...

The password to use when connecting to the NATS server.

username

string = ...

The username to use when connecting to the NATS server.

Returns

void

Deprecated

No, this won’t be removed anytime soon. You can ignore this warning if you’re aware of the security implications.


setNatsConnection()

function setNatsConnection(nc): void

Parameters

nc

null | NatsConnection

Returns

void


setUser()

function setUser(user): void

Sets a new user object or null if the user is no longer logged in.

Parameters

user

the user object or null

null | User

Returns

void


useNatsSubscription()

function useNatsSubscription(
   subject, 
   callback, 
   options?): void

Parameters

subject

string

callback

(message) => void | Promise<void>

options?

SubscriptionOptions

Returns

void

References

useNats

Re-exports useNats