Skip to content

Commit

Permalink
feat: invoke callback on onAuthStateChange (#627)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Run callback when `onAuthStateChange` is first invoked with an
`INITIAL_SESSION` event
* Fixes #313
  • Loading branch information
kangmingtay committed Mar 22, 2023
1 parent 7d75c08 commit 832d168
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,9 +941,26 @@ export default class GoTrueClient {

this.stateChangeEmitters.set(id, subscription)

this.emitInitialSession(id)

return { data: { subscription } }
}

private async emitInitialSession(id: string): Promise<void> {
try {
const {
data: { session },
error,
} = await this.getSession()
if (error) throw error

this.stateChangeEmitters.get(id)?.callback('INITIAL_SESSION', session)
} catch (err) {
this.stateChangeEmitters.get(id)?.callback('INITIAL_SESSION', null)
console.error(err)
}
}

/**
* Sends a password reset request to an email address.
* @param email The email address of the user.
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Provider =
export type AuthChangeEventMFA = 'MFA_CHALLENGE_VERIFIED'

export type AuthChangeEvent =
| 'INITIAL_SESSION'
| 'PASSWORD_RECOVERY'
| 'SIGNED_IN'
| 'SIGNED_OUT'
Expand Down

0 comments on commit 832d168

Please sign in to comment.