Code Monkey home page Code Monkey logo

next-auth-sanity's Introduction

Welcome to next-auth-sanity πŸ‘‹

Version License: MIT

NextAuth Adapter and Provider for Sanity

Overview

Features

  • Saving users and account in Sanity
  • Email Provider Support
  • Retrieving of full linked provider information for a user
  • 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.

Privacy and user information

Storing people's user credentials is always a big responsibility. Make sure you understand the risks and inform your users accordingly. This adapter store the user information with the _id on the user.Β path. In other words, these documents can't be queried without authentication, even if your dataset is set to be public. That also means that these documents are available for everyone that's part of your Sanity project.

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

you can install this package in your studio project and use the schemas like this

import createSchema from 'part:@sanity/base/schema-creator';

import schemaTypes from 'all:part:@sanity/base/schema-type';
import { user, account, verificationRequest } from 'next-auth-sanity/schemas';

export default createSchema({
  name: 'default',
  types: schemaTypes.concat([user, account, verificationRequest])
});

or copy paste the schemas

// user - required

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 - required

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' }
    }
  ]
};
// verification-request - only if you use email provider

export default {
  name: 'verification-request',
  title: 'Verification Request',
  type: 'document',
  fields: [
    {
      name: 'identifier',
      title: 'Identifier',
      type: 'string'
    },
    {
      name: 'token',
      title: 'Token',
      type: 'string'
    },
    {
      name: 'expires',
      title: 'Expires',
      type: 'date'
    }
  ]
};

Sign Up and Sign In With Credentials

Setup

API Route

// pages/api/sanity/signUp.ts
import { signUpHandler } from 'next-auth-sanity';
import { client } from 'your/sanity/client';

export default signUpHandler(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 [email protected]

🀝 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

next-auth-sanity's People

Contributors

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