Skip to content

Composables

useAuth

ts
const auth = useAuth();

useAuth() returns an $auth instance. You can use it to access the properties and methods of the $auth instance such as user, loggedIn, etc.

ts
const { user, loggedIn } = auth;

Interface

ts
interface AuthService {
  config: AuthConfig;
  httpService: HttpService;
  storage: AuthStorage;
  readonly user: Ref<User | null>;
  readonly store: Store<'auth', State, Getters, Actions>;
  readonly redirectPath: string;
  readonly accessToken: string | null | undefined;
  readonly refreshToken: string | null | undefined;
  readonly loggedIn: Ref<boolean>;
  readonly hasTokens: boolean;
  readonly isSessionExpired: boolean;
  readonly isSessionEnd: boolean;
  readonly isPersistent: boolean;
  login<T = unknown>(
    credentials: Record<string, unknown>,
    options?: { sessionOnly?: boolean }
  ): Promise<T>;
  fetchUser<T = unknown>(): Promise<T>;
  logout(callApi?: boolean): Promise<void>;
  refreshTokens<T = unknown>(): Promise<T>;
  setReferer(url: string | null): void;
}

useLogin

ts
const {
  credentials,
  pending,
  errorMsg,
  error,
  login,
  resetError
} = useLogin({
  credentials: { email: '', password: '' },
});

UseLoginOptions

Composable useLogin takes an options object with the following properties:

PropertyTypeDefaultDescription
redirectPath(auth: Auth) => RawLocation | RawLocation | undefined'/'The path to redirect to after login. You can use a callback with the input parameter as the $auth object and return a RawLocation.
credentialsany{}An object containing login information.
persistentboolean | undefinedtrueSet true to remember the login session.
invalidErrorMessagestring | undefined'Invalid login credentials'The error message when login fails due to invalid credentials.
otherErrorMessagestring | undefined'An error has occurred'The error message when login encounters an unspecified error.

Return value

PropertyTypeDescription
credentialsRef<any>Contains login information.
persistentRef<boolean>Used for the "Remember Me" checkbox.
errorMsgComputedRef<string | null>Error message.
errorRef<any>Error object containing errors when login fails.
pendingRef<boolean>Has a value of true when logging in is in progress.
resetError() => voidUsed to clear the value of the error ref.
login() => Promise<any>Login handler function, returns the value of the login function, takes two parameters as follows:
- params?: Record<string, unknown>: login information, if not passed, it will use the information in the credentials ref.
- { sessionOnly }: { sessionOnly?: boolean } = {}: Set sessionOnly = true if not "remember me".

Interfaces

ts
useLogin = (options: UseLoginOptions = {}): UseLoginReturns;

interface UseLoginOptions {
  redirectPath?: RawLocation;
  credentials?: any;
  persistent?: boolean;
  invalidErrorMessage?: string;
  otherErrorMessage?: string;
}

type UseLoginReturns = {
  credentials: Ref<any>;
  persistent: Ref<boolean>;
  errorMsg: ComputedRef<string | null>;
  error: Ref<any>;
  pending: Ref<boolean>;
  resetError: () => void;
  login: (
    params?: Record<string, unknown>,
    { sessionOnly }?: { sessionOnly?: boolean }
  ) => Promise<any>;
}

useLogout

ts
const useLogout = (redirectPath: RawLocation = '/') => {
  errorMsg,
  pending,
  doLogout,
}

Parameters

ParameterTypeDefaultDescription
redirectPathRawLocation'/'The redirect path after logout

Return values

PropertyTypeDescription
errorMsgComputedRef<string | null>The error message
pendingRef<boolean>Value is true when performing the logout request
doLogout() => voidThe execution function