@ubuilder/auth
TypeScript icon, indicating that this package has built-in type declarations

8.4.6 • Public • Published

UBuilder Auth

UBuilder Auth 8.x not compatible with version 7.x.

  • User authentication and autorization.

Authorized request should use Auth.rest() (vue3) or this.$rest (vue2).

Installation

npm i @ubuilder/auth

Usage

// vue 3
import { createApp } from 'vue';
import Auth from '@ubuilder/auth';

const app = createApp(...);

const options = {/* plugin options */};
app.use(Auth, options);

// vue 2
import Vue from 'vue';
import Auth from '@ubuilder/auth-vue2';

Vue.use(Auth, options);

Options

  • baseURL: string - Base URL for UBuilder auth server. Default value is '/api/auth'.
  • apiURL: string - Base URL for API server. Default value is '/'. If restOptions.baseURL exists, this option has no effect.
  • storageKey: string - LocalStorage key for current user. Default value is 'ubuilder:user'.
  • authPrefix: string - Prefix for provide and inject. Default value is '' (empty string).
  • onUserChanged: (user) => void - handler for user changed.
  • restOptions: RestOptions - options for rest call.

When using multiple instance with authPrefix, must set X-UBAuth header to authPrefix value.

Provides

  • auth: Auth - Auth API
  • rest: Rest API with authentication.
  • user: User - Current user
  • function onUserChanged((user) => unknown, authPrefix?: string)

Using inject function. These functions always returns instance. When authPrefix instance not exists, returns no-op or anonymous.

import { useAuth, useRest, currentUser, onUserChanged } from '@ubuilder/auth';

const auth = useAuth();
const rest = useRest();
const user = currentUser();
onUserChanged((user) => { /* do with user changed */ });

Using vanilla vue inject.

import type { Auth, Rest, User } from '@ubuilder/auth';

const auth = inject<Auth>('auth');
const rest = inject<Rest>('rest');
const user = inject<User>('user');

Vue 2 options API.

  • $auth - Auth API
  • $rest - Rest API
  • $user - current User

Auth API

  • login(username: string, password: string, remember?: boolean): Promise<User> - login process
  • login(request: LoginRequest): Promise<User> - login process with request.
  • logout(): Promise<void> - logout process
  • changePassword(oldPassword: string, newPassword: string): Promise<void> - change current user's password. After change password, all session will logout.
  • currentUser(): User - get current reactive user. If not logged in, returns anonymous user - check by isLoggedIn property.
  • rest(): Rest - get @ubuilder/rest API for authorized API request.
  • initialized(): Promise<void> - for waiting initial auth refresh.
  • refreshToken(): Promise<void> - refresh current user's token manually.

User interface

  • token: string - Access token for UBuilder server.
  • expireAt: number - token expire time
  • sessionExpireAt: number - session expire time
  • authorities: string[] - array of authority.
  • details: Record<string, unknown> - UserDetails from UBuidler auth server.
  • isLoggedIn: boolean - determine user is logged in.
  • hasAuthority(authority?: string): boolean - determine user has authority. When needs track user changes, should use computed.

Readme

Keywords

Package Sidebar

Install

npm i @ubuilder/auth

Weekly Downloads

42

Version

8.4.6

License

CC-BY-NC-ND-4.0

Unpacked Size

13 kB

Total Files

5

Last publish

Collaborators

  • yylee