Tony Le
Tony Le

Reputation: 31

NextJS + Typescript built failed with @auth0/nextjs-auth0

I'm using Typescript for NextJS and I cannot build the application because it has some issues with the auth0/nextjs-auth0

This is the issue. If I install this, it will keep checking for issues within the auth0/nextjs-auth0 packages.

Here is the error https://i.sstatic.net/L7sB7.jpg

This is my tsconfig.json

    {
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "es6",
      "es7",
      "esnext",
      "dom"
    ],
    "allowJs": true, /* Allow javascript files to be compiled. */// "checkJs": true,                       /* Report errors in .js files. */
    "jsx": "preserve",                  /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "removeComments": false,
    "strict": true, /* Enable all strict type-checking options. */
    "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
    "strictNullChecks": true, /* Enable strict null checks. */
    "strictFunctionTypes": true, /* Enable strict checking of function types. */
    "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
    "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
    "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. *//* Additional Checks */
    "noUnusedLocals": true, /* Report errors on unused locals. */
    "noUnusedParameters": true, /* Report errors on unused parameters. */
    "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
    "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. *//* Module Resolution Options */
    "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "baseUrl": ".",                    /* Type declaration files to be included in compilation. */
    "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                 /* Specify the location where debugger should locate map files instead of generated locations. */
    "inlineSourceMap": true,
    "inlineSources": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "declaration": true,
    "declarationDir": "./node_modules/@auth0/nextjs-auth0/src/auth0-session",
    "declarationMap": true
  },
  "include": [
    "pages"
  ],
  "exclude": [
    "node_modules"
  ]
}

Upvotes: 1

Views: 2145

Answers (7)

AsiaLakay
AsiaLakay

Reputation: 1

I followed the nextjs documentation on github and didn't expect this compile error. I deleted the node modules and package.json and ran npm install. The thing that got rid of the error was switching the original import statement from the nextjs github to this:

import { useUser } from '@auth0/nextjs-auth0/client';

Found it on the npm documentation page. https://www.npmjs.com/package/@auth0/nextjs-auth0

Good luck!

Upvotes: 0

Jobajuba
Jobajuba

Reputation: 1282

Try next auth instead, it's more fluid. Define your provider in your Server.js

import NextAuth from 'next-auth'
import AppleProvider from 'next-auth/providers/apple'
import FacebookProvider from 'next-auth/providers/facebook'
import GoogleProvider from 'next-auth/providers/google'
import EmailProvider from 'next-auth/providers/email'

export default NextAuth({
  providers: [
    // OAuth authentication providers...
    AppleProvider({
      clientId: process.env.APPLE_ID,
      clientSecret: process.env.APPLE_SECRET
    }),
    FacebookProvider({
      clientId: process.env.FACEBOOK_ID,
      clientSecret: process.env.FACEBOOK_SECRET
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET
    }),
    // Passwordless / email sign in
    EmailProvider({
      server: process.env.MAIL_SERVER,
      from: 'NextAuth.js <[email protected]>'
    }),
  ]
})

App.js

import { SessionProvider } from "next-auth/react"

export default function App({
  Component, pageProps: { session, ...pageProps }
}) {
  return (
    <SessionProvider session={session}>
      <Component {...pageProps}/>
    </SessionProvider>
  )
}

Index.js

import { useSession, signIn, signOut } from "next-auth/react"

export default function Component() {
  const { data: session } = useSession()
  if(session) {
    return <>
      Signed in as {session.user.email} <br/>
      <button onClick={() => signOut()}>Sign out</button>
    </>
  }
  return <>
    Not signed in <br/>
    <button onClick={() => signIn()}>Sign in</button>
  </>
}

Create a .env.local and add you provider client ids and secrets.

Upvotes: 0

Alexandra Bri
Alexandra Bri

Reputation: 1

Maybe it will be helpful for someone having the same problem. In my case it was a wrong import (autocomplete), which took the Session from src directory.

import { Session } from '@auth0/nextjs-auth0/src/session';

Upvotes: 0

Yilmaz
Yilmaz

Reputation: 49681

delete the node-modules and package.lock.json, then npm install. If issue is not resolved use this version:

"@auth0/nextjs-auth0": "^1.2.0",

I am using this package in my portfolio webpage, and i did not get any issue. It might be a bug in the new version

Upvotes: 0

Suneel K
Suneel K

Reputation: 84

Check your "tsconfig.json" file for compilation option "exclude". If it does not exist, just add it and exclude "node_modules".

// tsconfig.json
{
  "compilerOptions": {
  ...
  "exclude": [
    "node_modules", 
  ]
}

Upvotes: 0

illia chill
illia chill

Reputation: 2066

I recommend you to take a look at the official example. I had similar problems with auth0 embed. Don't forget you need to embed your <Component> in app.js to make auth0 work on pages + using withAuthRequired

import { UserProvider } from '@auth0/nextjs-auth0';

export default function App({ Component, pageProps }) {
  const { user } = pageProps;
  return (
      <UserProvider user={user}>
        <Component {...pageProps} />
      </UserProvider>
  );
}

Upvotes: 0

Zaphod
Zaphod

Reputation: 568

Try what the error message says, by running:

npm i --save-dev @types/url-join

If that does not work, then try deleting node_modules and then run npm install or yarn

Upvotes: 1

Related Questions