UBuilder Auth
npm install @ubuilder/auth* User authentication and autorization.
Authorized request should use Auth.rest() (vue3) or this.$rest (vue2).
npm i @ubuilder/auth
`Usage
`javascript
// 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.
`typescript
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.
`typescript
import type { Auth, Rest, User } from '@ubuilder/auth';const auth = inject('auth');
const rest = inject('rest');
const user = inject('user');
``Vue 2 options API.
* $auth - Auth API
* $rest - Rest API
* $user - current User