0.2.1 • Published 10 months ago

vue-auth-sanctum v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

Vue Auth Sanctum

A Vue 3 plugin for authentication using Laravel Sanctum.

Installation

To install the package, run the following command:

  npm install vue-auth-sanctum

Configuration

Add the plugin in your main.ts or main.js file, and provide the required options.

import { createApp } from 'vue';
import App from './App.vue';
import { vueAuthSanctum } from 'vue-auth-sanctum';

const authConfig = {
  apiURL: 'http://localhost:8000',
  mode: 'cookie', // Optional: 'cookie' or 'token'
  endpoints: {
    csrf: '/sanctum/csrf-cookie',
    login: '/login',
    logout: '/logout',
    user: '/api/user',
  },
  csrf: {
    cookie: 'XSRF-TOKEN',
    header: 'X-XSRF-TOKEN',
  }
};

const app = createApp(App);
app.use(vueAuthSanctum, authConfig);
app.mount('#app');

useSanctumAuth

To authenticate a user you should pass the credentials payload as an argument to the login method. The payload should contain all fields required by your Laravel Sanctum backend.

views/Login.vue

  import {ref} from 'vue';
  import { useSanctumAuth } from 'vue-auth-sanctum';

  const { login } = useSanctumAuth();

  const userCredentials = ref({
      email: 'user@mail.com',
      password: '123123',
  });

  await login(userCredentials.value);

useSanctumUser

This composable provides access to the current authenticated user. It supports generic types, so you can get the user as any class you want.

views/Dashboard.vue

  import { useSanctumUser } from 'vue-auth-sanctum';
  
  const user = useSanctumUser();
0.2.1

10 months ago

0.2.0

10 months ago

0.1.9

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago