Git Product home page Git Product logo

nuxt-oauth's Introduction

nuxt-oauth

nuxt-outh Simple OAuth2 integration for your Nuxt app

CircleCI

Usage

Get Setup

  1. Install the dependency:
yarn add nuxt-oauth
  1. Add to your nuxt.config.js and configure:
// nuxt.config.js

modules: ['nuxt-oauth'],
oauth: {
  sessionName: 'mySession',
  secretKey: process.env.SECRET_KEY,
  oauthHost: process.env.OAUTH_HOST,
  oauthClientID: process.env.OAUTH_CLIENT_ID,
  oauthClientSecret: process.env.OAUTH_CLIENT_SECRET,
  onLogout: (req, res) => {
    // do something after logging out
  },
  fetchUser: (accessToken) => {
    // do something to return the user
    const user = User.findByToken(accessToken)
    return user
  }
}

Use in your application

  • Use the access token as you'd like from the Vuex store:
// any-component.vue

export default {
  mounted () {
    const { accessToken } = this.$store.state.oauth
    // fetch more details from somewhere...
  }
}
  • Mark your authenticated page components:
// secret.vue

export default {
  authenticated: true,
  name: 'MySecretComponent'
}
  • Or, manually log in:
// any-component.vue
<template>
  <a @click="login">Log In</a>
</template>
<script>
  export default {
    methods: {
      login () {
        const redirectUrl = this.$route.path 
        // Or another path to return to after logging in
        this.$router.push(`/auth/login?redirect-url=${redirectUrl}`)
      }
    }
  }
</script>

Configuration

Option Required? Description
sessionName * Configure the name of the cookie that nuxt-oauth uses
secretKey * Provide a secret key to sign the encrypted cookie. Do not leak this!
oauthHost * Host of your OAuth provider
oauthClientID * Client ID of your application, registered with your OAuth provider
oauthClientSecret * Client ID of your application, registered with your OAuth provider
onLogout Optional hook which is called after logging out. E.g. can be used to perform a full log out on your OAuth provider. Receives args (req, res, redirectUrl). Can be asynchronous (or return a promise).
fetchUser Optional hook which is called when logging in to fetch your user object. Receives args (accessToken).
testMode Flag which tells the module to ignore the OAuth dance and log every one in.

Manual Usage

  • Log In

    Redirect to /auth/login?redirect-uri=REDIRECT_URL where REDIRECT_URL is the URL you'd like to be redirected back to after successfully logging in.

  • Log Out

    Redirect to /auth/logout?redirect-uri=REDIRECT_URL where REDIRECT_URL is the URL you'd like to be redirected back to after successfully logging out.

With your tests

Set options.oauth.testMode to true to tell the module to skip authentication. Using this, along with the fetchUser option, can be helpful in e2e tests to stub your test users.

Develop

git clone [email protected]:samtgarson/nuxt-oauth.git
cd nuxt-oauth
yarn
yarn test

# To use while developing other apps:
yarn link
cd ../my-other-app
yarn link nuxt-oauth

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/samtgarson/nuxt-oauth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Thanks

License

The module is available as open source under the terms of the MIT License.

nuxt-oauth's People

Contributors

samtgarson avatar yuki-nakamura avatar

Watchers

 avatar  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.