Providers

Olobase Admin provides 2 configurable authentication providers:

  • basicAuthProvider: Basic HTTP authentication.
  • jwtAuthProvider: JWT (default) for stateless authentication.

All of these providers need an axios instance to work. The easiest way is to create an axios instance and pass it to both authentication and data providers:

src/plugins/admin.js

import OlobaseAdmin from "olobase-admin";
import {
  jsonServerDataProvider,
  jwtAuthProvider,
} from "olobase-admin/src/providers";
import { en, tr } from "olobase-admin/src/locales";

let admin = new OlobaseAdmin(import.meta.env);
/**
 * Install admin plugin
 */
export default {
  install: (app, http, resources) => {
    admin.setOptions({
      dataProvider: jsonServerDataProvider(http),
      authProvider: jwtAuthProvider(http),
    });
    admin.init();
    OlobaseAdmin.install(app); // install layouts & components
    app.provide("i18n", i18n);
    app.provide("router", router);
    /*
    ...
    */
  },
};

All included providers can be used in the same way; If you want to use basicAuthProvider instead of jwtAuthProvider, simply replace jwtAuthProvider with basicAuthProvider.

First Auth Parameter: Axios

To access the axios instance from anywhere in your custom Vue components via admin.http, the axios instance is called the OlobaseAdmin constructor method and from there to the jwtAuthProvider is sent to the provider. This submitted example allows reuse of the current authentication state (cookie or token).

Second Auth Parameter: Params

If you pass a second optional params variable to your auth provider like this, it can be used to replace or add to various existing parameters:

let params = {
  routes: {
    login: "auth/token",
    logout: "auth/logout",
    refresh: "auth/refresh",
  },
  getToken: (r) => r.token,
  getCredentials: ({ username, password }) => {
    return {
      username,
      password,
    };
  },
  getId: (r) => r.user.id,
  getFullname: (r) => r.user.fullname,
  getEmail: (r) => r.user.email,
  getAvatar: () => localStorage.getItem("avatar"),
  getPermissions: (r) => r.user.permissions,
};
jwtAuthProvider(http, params);
Option Description
routes Object containing all authentication routes, i.e. login, logout, refresh (JWT), user information.
getToken JWT only function that returns token from a successful login API response has token set by default.
getCredentials Function mapper for credentials that returns a compatible credential format for your API.
getId The function that returns the id of the current user.
getFullname The function that returns the name of the current user (user information retrieved from the API endpoint) is by default: fullname
getEmail The function that returns the current user's email is, by default: email.
getPermissions The function that returns the roles of the current user is by default: permissions.