Giter VIP home page Giter VIP logo

nuxt-hanko's Introduction

Nuxt Hanko

npm version npm downloads Github Actions Codecov

Hanko integration for Nuxt

Features

  • ✨ Easy integration for Hanko
  • 🧱 Type safety and auto-registration for Hanko web components
  • 💪 Server utilities for full-stack auth

Installation

Install and add @nuxtjs/hanko to your nuxt.config.

npx nuxi@latest module add hanko
export default defineNuxtConfig({
  modules: ['@nuxtjs/hanko'],
  hanko: {
    // You can also configure this by setting NUXT_PUBLIC_HANKO_API_URL at runtime
    apiURL: '<YOUR_API_URL>',
    // You can also customise these if required
    // cookieName: 'hanko'
    // redirects: {
    //   login: '/login',
    //   success: '/',
    //   home: '/',
    //   followRedirect: true
    // },
    // registerComponents: true,
    // augmentContext: true,
    // globalMiddleware: false,
  },
})

Usage

Components

To use, you can use the Hanko components anywhere in your app: <hanko-auth>, <hanko-events> and <hanko-profile>. These are web components that will be rendered on the client-side only. Props are typed.

You can turn auto-registration of components off (if you wish to use Hanko just on the server side or programmatically) with registerComponents: false.

Check out the Hanko documentation to learn more.

<template>
  <hanko-auth />
</template>

Middleware

By default three new route middlewares are available in your Nuxt app: hanko-logged-in, hanko-logged-out and hanko-allow-all.

  • hanko-logged-in will prevent access to a page unless you are logged in. (It will redirect you to redirects.login instead, and then redirect back to this page once you login. You can disable this behaviour by setting redirects.followRedirect to false.)
  • hanko-logged-out will prevent access to a page unless you are logged out. (It will redirect you to redirects.success when you log in, and otherwise to redirects.home.)
  • hanko-allow-all will allow all users access to page, even if the globalMiddleware option is set to true. (If the globalMiddleware option is not set to true, this middleware has no effect.)

You can also create your own middleware for full control.

Global Middleware

If the globalMiddleware configuration is set to true, the middleware is automatically applied to all of your pages. You can still override this behavior on each page, by applying the hanko-logged-out or the hanko-allow-all middleware.

Note: The globalMiddleware option will not apply any authentication checks to your API-paths.

Auto-imports

useHanko is exposed in the Vue part of your app to allow you direct access to the Hanko API. You can access the current user and much more. Note: It will return null on the server.

The hankoLoggedIn and hankoLoggedOut middleware are exposed to enable you to extend their functionality, such as creating a custom global middleware.

Server utilities

By default you can access a verified JWT context on event.context.hanko. (It will be undefined if the user is not logged in.) If you want to handle this yourself you can set augmentContext: false.

verifyHankoEvent is exposed in the Nitro part of your app to expose the underlying utility used to create event.context.hanko if you want to handle things manually.

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Stub module with pnpm dev:prepare
  • Run pnpm dev to start playground in development mode

Credits

Thanks to @McPizza0 for the push to make the module!

License

Made with ❤️

Published under the MIT License.

nuxt-hanko's People

Contributors

renovate[bot] avatar danielroe avatar felix-dolderer avatar mcpizza0 avatar autofix-ci[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.