npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hedger/nuxt-sanctum

v0.1.5

Published

Nuxt module for Laravel Sanctum

Downloads

5

Readme

nuxt-sanctum

npm version npm downloads License Nuxt

The nuxt-sanctum package brings Laravel Sanctum support to Nuxt.

Features

  • 🚀  Universal rendering
  • 🍪  Cookie-based authentication
  • ⚙️  Sensible defaults, but configurable

Quick Setup

  1. Add @hedger/nuxt-sanctum dependency to your project
# Using bun
bun add @hedger/nuxt-sanctum

# Using pnpm
pnpm add @hedger/nuxt-sanctum

# Using yarn
yarn add @hedger/nuxt-sanctum

# Using npm
npm install @hedger/nuxt-sanctum
  1. Add @hedger/nuxt-sanctum to the modules section of nuxt.config.ts
export default defineNuxtConfig({
	modules: ["@hedger/nuxt-sanctum"],
});

Usage

The following examples should get your started. To customize the behavior, have a look a the options.

Signing In

To sign a user in, use the login method exposed by the useSanctum composable.

const { login } = useSanctum();

await login({
	email: "[email protected]",
	password: "winteriscoming",
});

Redirecting after signing in

By default, the user will be redirected to the URL specified in the login.redirectsTo option. You may override this behavior by passing an alternative URL to the login method. Additionally, you may pass false to the login method to prevent redirection altogether.

// Override the default redirect
await login({
	email: "[email protected]",
	password: "winteriscoming",
},"/somewhere-else");

// Prevent redirection
await login({
	email: "[email protected]",
	password: "winteriscoming",
}, false);

Signing Out

To sign a user out, use the logout method exposed by the useSanctum composable.

const { logout } = useSanctum();

await logout();

Redirecting after signing out

By default, the user will be redirected to the URL specified in the logout.redirectsTo option. You may override this behavior by passing an alternative URL to the logout method. Additionally, you may pass false to the logout method to prevent redirection altogether.

// Override the default redirect
await logout("/somewhere-else");

// Prevent redirection
await logout(false);

Checking if a user is signed in

To check if a user is signed in, use the authenticated variable exposed by the useSanctum composable.

const { authenticated } = useSanctum();

if (authenticated.value) {
	// The user is signed in
}

Retrieving the user's details

To retrieve the details about the currently signed in user, use the user variable exposed by the useSanctum composable.

const { user } = useSanctum();

console.log(user.value?.name) // John Snow

Type safety

You may specify the type of the user object by passing it as a generic type argument to the useSanctum composable.

interface User {
	id: number;
	name: string;
	email: string;
}

const { user } = useSanctum<User>();

Making API requests

To make API requests, you may use the useSanctumFetch composable. This composable is a wrapper around the useFetch composable provided by the Nuxt that automatically handles the CSRF token and passes down the user's session cookie.

const { data } = useSanctumFetch("/api/user");

Restricting access to routes

This package provides the auth and guest middlewares to restrict access to routes.

Restricting to authenticated users

Use the auth middleware to ensure that only authenticated users can access a route. Unauthenticated users will be redirected to the URL specified in the middlewares.auth.redirectsTo option.

<script setup lang="ts">
	definePageMeta({
		middleware: "auth",
	});
</script>

Restricting to guest users

Use the guest middleware to ensure that only guest users can access a route. Authenticated users will be redirected to the URL specified in the middlewares.guest.redirectsTo option.

<script setup lang="ts">
	definePageMeta({
		middleware: "guest",
	});
</script>

Troubleshooting

Using HTTPS in development

When your Laravel API is served over HTTPS in a development environment, SSL errors may occur due to Node.js rejecting self-signed certificates. To fix this, set NODE_TLS_REJECT_UNAUTHORIZED to 0 when starting your development server.

{
	"scripts": {
		"dev": "NODE_TLS_REJECT_UNAUTHORIZED=0 nuxt dev"
	}
}

[!NOTE] Bun does not seem affected by this issue.

DNS resolution

If you run your Laravel API with php artisan serve, be aware that by default, it will only bind to the IPv4 interface. This may cause DNS resolution issues when using the useSanctumFetch composable, because it may try to resolve the localhost hostname to an IPv6 address.

There are a few ways to work around this:

  • Bind to the IPv6 interface instead by running php artisan serve --host ::1
  • Edit your /etc/hosts file to remove the IPv6 entry for localhost