Giter Site home page Giter Site logo

nateschickler0 / next-auth-capacitor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from choutkamartin/next-auth-capacitor

0.0 0.0 0.0 78 KB

Example on how to make Next Auth work with Capacitor using workarounds and custom SessionProvider

Home Page: https://next-auth-capacitor.vercel.app

JavaScript 4.78% TypeScript 82.70% CSS 12.52%

next-auth-capacitor's Introduction

How to make Next Auth work with Capacitor

Before you start

This is an example of a production use. It doesn't work straight out of the box on localhost, as the application that runs on on Vercel doesn't allow requests from localhost. (See choutkamartin#4)

This is an example of how would you authenticate user from your built Android application against a Next.js application running on a distant server.

What is this?

If you have a Next.js application, you may know you can run it natively on Android or iOS using Capacitor.

Next Auth however doesn't work straight out the box. This repository shows that you can use some workarounds to make it work.

Basic Capacitor knowledge required.

Tips

  1. Use chrome://inspect/#devices to inspect the webview of your application. You can inspect cookies of your app this way.
  2. Don't know much about Capacitor? Read this https://devdactic.com/nextjs-and-capacitor

Caveats

iOS and the forbidden usage of https scheme

iOS as a system doesn't allow the usage of https scheme, which is something we rely on in this repository, as we want our application to have a hostname with the https scheme

Read more at: https://forum.ionicframework.com/t/https-for-the-app-server-protocol-instead-of-capacitor-on-ios/200116/2

What you need to do:

1. Change capacitor.config.ts to:

server: {
    hostname: `mob.next-auth-capacitor.vercel.app`, // We need to change hostname to subdomain of our domain the API is hosted on
    androidScheme: "https", // HTTPS should be set preferably
},

We need to set hostname and androidScheme to https so we can share our cookies with our application (domain - subdomain cookies sharing).

2. Change Next API config (headers) to enable CORS

You must change mainly these keys: Access-Control-Allow-Credentials and Access-Control-Allow-Origin so the application can connect to the server.

async headers() {
    return [
      {
        source: "/:path*",
        headers: [
          { key: "Access-Control-Allow-Credentials", value: "true" },
          {
            key: "Access-Control-Allow-Origin",
            value: "https://mob.next-auth-capacitor.vercel.app",
          },
          {
            key: "Access-Control-Allow-Methods",
            value: "GET,OPTIONS,PATCH,DELETE,POST,PUT",
          },
          {
            key: "Access-Control-Allow-Headers",
            value:
              "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
          },
        ],
      },
    ];
  },

3. Create own OAuth flow

See utils/helper.ts

4. Create own UseSession provider

See utils/session.tsx

5. Change _app.tsx SessionProvider to provider you just created

See pages/_app.tsx

6. Modify Next Auth config

See pages/api/auth/[...nextauth].ts


Based on a discussion: nextauthjs/next-auth#4446 Thanks to @creativiii

next-auth-capacitor's People

Contributors

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