firebase / firebase/firebase-js-sdk

Inaccurate return type on signInWithEmailAndPassword

Open
#9,142 6 comments 0 reactions 0 assignees View on GitHub
api: auth needs-attention question Repro Needed stack:Other
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

Linux 6.15.2

### Environment (if applicable)

Chromium 137.0.7151.104

### Firebase SDK Version

11.9.1

### Firebase SDK Product(s)

Functions

### Project Tooling

Vite 7.0.0 with TS 5.8.3

### Detailed Problem Description

We're logging a user in with `signInWithEmailAndPassword` and using the `email`, `accessToken`, and `refreshToken` returned on the `user` object. This is working as expected. The issue is that property `accessToken` does not exist on type `User`:
![Image](https://github.com/user-attachments/assets/dfc6d057-4500-4a97-a4a2-41c9c6809fe3)

Here are the settings we are instantiating the app with:
```
const firebaseConfig: FirebaseOptions = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
```

Adding a log in to check the response returned by `signInWithEmailAndPassword` shows this:

![Image](https://github.com/user-attachments/assets/222d42ce-9e11-4f2d-9b97-e237859aeb33)

![Image](https://github.com/user-attachments/assets/8cd7d761-c893-4414-9178-feb55f17a437)

So, there is definitely an accessToken returned when the type says there is not, so we have a type conflict between what is documented and what is actually happening.

As a workaround, I am typecasting the following:
```
// TODO improve type safety by getting Firebase to update their types as accessToken is not present on the types
// but is present in the response.
const {user} = await signInWithEmailAndPassword(auth, email, password) as unknown as {user: User & OAuthCredential}
```

This works, but if the SDK gets an update that changes anything on that function my app could break.

### Steps and code to reproduce issue

Set up a firebas project with the latest SDK (v11.9.1 as of now)

Use the following TS code:

```
import {FirebaseOptions, initializeApp} from "firebase/app";
import {getAuth, signInWithEmailAndPassword} from "firebase/auth";

const firebaseConfig: FirebaseOptions = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

async function test() {
const {user} = await signInWithEmailAndPassword(auth, email, password);
user.accessToken; // should see type error here
}
test().then()
```

To check the functionality of the code above, just insert a valid auth object, email and password.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.