Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wobsoriano/nuxt-next-auth
Experimental authentication module for Nuxt 2 using NextAuth.
https://github.com/wobsoriano/nuxt-next-auth
authentication next-auth nextjs nuxt
Last synced: 24 days ago
JSON representation
Experimental authentication module for Nuxt 2 using NextAuth.
- Host: GitHub
- URL: https://github.com/wobsoriano/nuxt-next-auth
- Owner: wobsoriano
- License: mit
- Created: 2021-08-01T18:45:29.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-17T02:56:18.000Z (almost 3 years ago)
- Last Synced: 2024-10-02T08:52:36.167Z (about 1 month ago)
- Topics: authentication, next-auth, nextjs, nuxt
- Language: JavaScript
- Homepage: https://nuxt-next-auth-demo.vercel.app/
- Size: 493 KB
- Stars: 25
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Nuxt NextAuth
Experimental authentication module for Nuxt using [NextAuth](https://next-auth.js.org/).
Demo: https://nuxt-next-auth-demo.vercel.app/
- [Usage](#usage)
- [Requirements](#requirements)
- [Setup](#setup)
- [Use in your application](#use-in-your-application)
- [Configuration](#configuration)
- [Using with TypeScript](#using-with-typescript)
- [Example Code](#example-code)
- [Develop](#develop)
- [Credits](#credits)
- [License](#license)## Usage
### Requirements
- [Nuxt SSR](https://nuxtjs.org/docs/2.x/concepts/server-side-rendering)
- [Vuex store](https://nuxtjs.org/guide/vuex-store)
- [Composition API](https://composition-api.nuxtjs.org/) - automatically installed### Setup
1. Install the dependency:
```bash
yarn add nuxt-next-auth
```2. Add to your `nuxt.config.js` and configure `next-auth` [options](https://next-auth.js.org/configuration/options):
```js
import Providers from 'next-auth/providers';export default {
buildModules: ['@nuxtjs/composition-api/module'],
modules: [
'nuxt-next-auth/module'
],nextAuth: {
// Configure one or more authentication providers here.
// https://next-auth.js.org/configuration/providers
providers: [
Providers.GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET
})
],
// A database is optional, but required to persist accounts in a database.
// https://next-auth.js.org/configuration/databases
database: process.env.DATABASE_URL,
}
}
```### Use in your application
- All methods from the [NextAuth.js client library](https://next-auth.js.org/getting-started/client) can be imported in the `nuxt-next-auth` module
or accessed via global `$nextAuth` plugin:```js
// Options API
export default {
mounted () {
this.$nextAuth.getSession()
this.$nextAuth.getCsrfToken()
this.$nextAuth.getProviders()
this.$nextAuth.signIn()
this.$nextAuth.signOut()
}
}// Composition API
import { useSession } from 'nuxt-next-auth' // can import other methods tooconst [session, loading] = useSession()
```- To persist the session in the Vuex store, add this to your actions in `store/index.js`:
```js
export const actions = {
async nuxtServerInit({ dispatch }, { req }) {
await dispatch('auth/getSession', { req })
}
}
``````js
// nuxt-next-auth uses auth as module name
export default {
mounted () {
const { session } = this.$store.state.auth
}
}
```- Create a middleware for auth routes:
```js
// middleware/auth.js
export default ({ store, redirect }) => {
if (!store.state.auth.session) {
return redirect('/')
}
}// any-secure-page.vue
export default {
middleware: ['auth']
}
```### Configuration
- [Options](https://next-auth.js.org/configuration/options)
- [Providers](https://next-auth.js.org/providers/overview)
- [Databases](https://next-auth.js.org/configuration/databases)
- [Pages](https://next-auth.js.org/configuration/pages)
- [Callbacks](https://next-auth.js.org/configuration/callbacks)
- [Events](https://next-auth.js.org/configuration/events)### Using with TypeScript
Add `nuxt-next-auth` to the `compilerOptions.types` section of your project's `tsconfig.json` file:
```json
{
"compilerOptions": {
"types": [
"nuxt-next-auth",
]
},
}
```### Example code
```html
Signed in as {{ session.user.email }}
Sign out
Not signed in
Sign in
import { defineComponent } from '@nuxtjs/composition-api'
import { signIn, signOut, useSession } from 'nuxt-next-auth'export default defineComponent({
setup() {
const [session, loading] = useSession()return {
session,
loading,
signIn,
signOut
}
}
})```
## Develop
```bash
git clone https://github.com/wobsoriano/nuxt-next-auth.git
cd nuxt-next-auth
yarn
yarn test
```### Running locally
To run the fixture Nuxt app (`/test/fixture`) locally, make sure to:
```bash
cp test/fixture/.env.example test/fixture/.env
```
and populate with your real values. Then, run:
```
yarn dev
```
To boot the app locally.### Credits
- [NextAuth.js](https://next-auth.js.org/) - Authentication for Next.js
- [Auth Module](https://github.com/nuxt-community/auth-module) - Zero-boilerplate authentication support for Nuxt.js
- [nuxt-oauth](https://github.com/SohoHouse/nuxt-oauth) - Simple OAuth2 integration for your Nuxt app### License
[MIT License](http://opensource.org/licenses/MIT).