question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define this.$auth.user type

See original GitHub issue

I’m using typescript and I need to change this.$auth.user to correct type. It’s default typed as Record<string, unknown> | null from @nuxtjs/auth-next/dist/index.d.ts. How can I overwrite this type definition?

I tried to create an index.d.ts file in my src/types directory with the following content, but it doesn’t work

// Still reading default type
import { Auth } from '@nuxtjs/auth-next/dist'
import { Collaborator } from '~/interfaces/Collaborator'

declare module '@nuxtjs/auth-next' {
  interface Vue {
    $auth: Auth & { user: Collaborator }
  }
}
declare module 'vue/types/vue' {
  interface Vue {
    $auth: Auth & { user: Collaborator }
  }
}

The latter approach gives me this error message:

TS2717: Subsequent property declarations must have the same type.
Property '$auth' must be of type 'Auth', but here has type 'Auth & { user: Collaborator; }'.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
mao-shonencommented, May 13, 2021

I’m a beginner in typescript, I’m not sure if it’s wrong, but it’s working well in my project

// index.d.ts
import { Auth as NuxtAuth } from '@nuxtjs/auth-next'

export interface User {
  id: string
  name: string
}

declare module 'vue/types/vue' {
  interface Auth extends NuxtAuth {
    user: User & typeof NuxtAuth.prototype.user
  }
}

declare module '@nuxt/types' {
  interface Auth extends NuxtAuth {
    user: User & typeof NuxtAuth.prototype.user
  }
}

declare module 'vuex/types/index' {
  interface Auth extends NuxtAuth {
    user: User & typeof NuxtAuth.prototype.user
  }
}
5reactions
Myzel394commented, Aug 11, 2021

The solution above didn’t work for me. Here’s what worked for me:

import { Auth as NuxtAuth } from "@nuxtjs/auth-next"
import User from "~/types/user"

declare module "vue/types/vue" {
    interface Vue {
        // @ts-ignore
        $auth: NuxtAuth & {
            user: User
        }
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

authentication - Nuxt Auth - User Data not set - Stack Overflow
As a response I get the token and then the user data is delivered. However, this.$Auth.loggedIn is false and this.$Auth.user is undefined ....
Read more >
On this page - Nuxt Auth
This object contains details about authenticated user such as name. You can access it using either $auth or Vuex. // Access using $auth...
Read more >
Auth | JavaScript SDK | Firebase JavaScript API reference
When you set the tenant ID of an Auth instance, all future sign-in/sign-up operations will pass this tenant ID and sign in or...
Read more >
Authentication - Laravel - The PHP Framework For Web Artisans
At its core, Laravel's authentication facilities are made up of "guards" and "providers". Guards define how users are authenticated for each request. For ......
Read more >
AuthComponent - 3.10 - CakePHP Cookbook
Generally, this is done through a username and password, that are checked against a known list of users. In CakePHP, there are several...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found