Extend AuthContextInterface type in Typescript

Hi! How can I extend the AuthContextInterface? I’ve tried the default typescript way, aka declaration merging using the following code:

declare module '@redwood/auth' {
	const useAuth: () => AuthContextInterface;
	interface AuthContextInterface {
		signUp(options: Record<string, string>): Promise<unknown>;
	}
}

but it’s not working!

Hi @cfanoulis. Could you explain your use case and what you are looking to do? There may be another way or if we can understand why you need to extended can make that happen.

Thanks.

Hi, I’m using DbAuth and as far as I have understood, I need to extend the AuthContextInterface in order to have the correct typings for my user model. Is there any other way to provide an interface for our user models?

Disclaimer: I haven’t messed around with the project I’m ripping this out of in a week or so, no idea if what I’m about to post is all you’ll need. Here is the full project for reference if not. Feel free to ping me back if anything is confusing.

The roles section isn’t something I spent too much time on, either. I think it’s still going to default to Redwood’s list of strings and not use the object I’m providing.

So I’m adding a redwoodjs.d.ts file to web/types/:

import type { Account, Organization, Role } from '@prisma/client'

// Technically, this interface is at the root of my project, used by both sides.
export interface ICurrentUser extends Account {
  organization: Organization
  roles: Role[]
}

// I think you're just missing the import to '@redwoodjs/auth'?
import '@redwoodjs/auth'
declare module '@redwoodjs/auth' {
  export interface CurrentUser extends ICurrentUser {}
}

And then I’m adding it to web/tsconfig.json:

{
  "compilerOptions": {
    ...
  },
  "include": [
    "src",
    "./types/redwoodjs.d.ts", <- Here she is
    ...
  ]
}

Which will result in the following, in VSCode:

I didn’t spend too much time on the interface - so you can see how hashedPassword and salt are included. You could get around that by changing interface ICurrentUser extends Account to something like interface ICurrentUser extends Omit<Account, 'hashedPassword' | 'salt'>.

Hope it helps!

1 Like

Yup, that seems to have worked! Thanks!