nuxt-oauth Simple OAuth2 integration for your Nuxt app
- Install the dependency:
yarn add nuxt-oauth
- 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 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 (
nuxt-oauth
will ensure users are logged in before accessing these pages):
// secret.vue
export default {
authenticated: true,
name: 'MySecretComponent'
}
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 (usually ending in oauth or oauth2 ) |
oauthClientID |
* | Client ID of your application, registered with your OAuth provider |
oauthClientSecret |
* | Client ID of your application, registered with your OAuth provider |
scopes |
An array of scopes to authenticate against | |
authorizationPath |
The path to redirect users to authenticate (defaults to /authorize ) |
|
accessTokenPath |
The path to request the access token (defaults to /token ) |
|
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 (see here for more). |
You can also use the functionality manually. nuxt-oauth
injects the following helpers into your store, components and ctx.app
: $login
and $logout
. Use these to manually log your user in or out.
Following a successful login/logout, your user will be redirected back to the page from which the helper was called (you can pass a redirectUrl
to the helpers to override this). For a full example, see below.
<!-- any-component.vue -->
<template>
<a @click="logout" v-if="loggedIn">Log Out</a>
<a @click="login" v-else>Log In</a>
</template>
<script>
export default {
asyncData({ app }) {
// Use from context
app.$login()
}
computed () {
loggedIn () {
return this.$store.state.oauth.accessToken
}
},
methods: {
login () {
// defaults to redirecting back to the current page
this.$login()
},
logout () {
// customise the redirrect url
const redirectUrl = '/my-target-path'
this.$logout(redirectUrl)
}
}
}
</script>
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.
git clone git@github.com: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
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.
- Many thanks to Evan You and the VueJS team for sustaining such a vibrant and supportive community around Vue JS
- Many thanks also Alex Chopin, SĂ©bastien Chopin, Pooya Parsa and the other Nuxt contributors for creating this awesome library
The module is available as open source under the terms of the MIT License.