NextAuth Sanity

By Federico Minaya

NextAuth Adapter and Provider for Sanity

Welcome to next-auth-sanity 👋

Version License: MIT

NextAuth Adapter and Provider for Sanity

Overview

Features

  • Saving users and account in Sanity
  • Retrieving of full linked provider information for a user
  • Stale While Revalidate
  • Auth with Credentials
  • Hash Credentials Passwords with Argon2

Database sessions

Database sessions are not implemented, this adapter relies on usage of JSON Web Tokens for stateless session management.

Recommendations

you must make the dataset private otherwise the data of your users will be public for everyone

sanity dataset visibility set <datasetName> private

Requirements

  • Sanity Token for Read+Write

Installation

yarn

yarn add next-auth-sanity

npm

npm i next-auth-sanity

Usage

Full Example

import NextAuth, { NextAuthOptions } from 'next-auth';
import Providers from 'next-auth/providers';
import { NextApiRequest, NextApiResponse } from 'next';
import { SanityAdapter, SanityCredentials } from 'next-auth-sanity';
import { client } from 'your/sanity/client';

const options: NextAuthOptions = {
  providers: [
    Providers.GitHub({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET
    }),
    SanityCredentials({ client }) // only if you use sign in with credentials
  ],
  session: {
    jwt: true
  },
  adapter: SanityAdapter({ client })
};

export default (req: NextApiRequest, res: NextApiResponse) =>
  NextAuth(req, res, options);

Sanity Schemas

// user

export default {
  name: 'user',
  title: 'User',
  type: 'document',
  fields: [
    {
      name: 'name',
      title: 'Name',
      type: 'string'
    },
    {
      name: 'email',
      title: 'Email',
      type: 'string'
    },
    {
      name: 'image',
      title: 'Image',
      type: 'url'
    },
    {
      // this is only if you use credentials provider
      name: 'password',
      type: 'string',
      hidden: true
    }
  ]
};
// account

export default {
  name: 'account',
  title: 'Account',
  type: 'document',
  fields: [
    {
      name: 'providerType',
      type: 'string'
    },
    {
      name: 'providerId',
      type: 'string'
    },
    {
      name: 'providerAccountId',
      type: 'string'
    },
    {
      name: 'refreshToken',
      type: 'string'
    },
    {
      name: 'accessToken',
      type: 'string'
    },
    {
      name: 'accessTokenExpires',
      type: 'string'
    },
    {
      name: 'user',
      title: 'User',
      type: 'reference',
      to: { type: 'user' }
    }
  ]
};

Sign Up and Sign In With Credentials

Setup

API Route

// pages/api/sanity/signUp.ts

import { signUpHandler } from 'next-auth-sanity';
import { NextApiRequest, NextApiResponse } from 'next';
import { client } from 'your/sanity/client';

export default (req: NextApiRequest, res: NextApiResponse) =>
  signUpHandler({ req, res, client });

Client

import { signUp } from 'next-auth-sanity/client';
import { signIn } from 'next-auth/client';

const user = await signUp({
  email,
  password,
  name
});

await signIn('credentials', {
  redirect: false,
  email,
  password
});

Author

👤 Fedeya elfedeminaya@gmail.com

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

Install command

npm i next-auth-sanity

Contributor