Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip(auth): Get fresh token before making request #1577

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/auth/src/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export class AuthProvider extends React.Component<
constructor(props: AuthProviderProps) {
super(props)
this.rwClient = createAuthClient(props.client, props.type)

// Add observer if the auth client supports it
// @TODO: Do we need to worry about cancelling the event handler?
// I don't think AuthProvider ever unmounts
if (this.rwClient.onTokenChange) {
this.rwClient.onTokenChange(() => {
this.getToken()
})
}
}

async componentDidMount() {
Expand All @@ -115,14 +124,15 @@ export class AuthProvider extends React.Component<
}

getCurrentUser = async (): Promise<Record<string, unknown>> => {
const authToken = await this.getToken()
const response = await window.fetch(
`${window.__REDWOOD__API_PROXY_PATH}/graphql`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
'auth-provider': this.rwClient.type,
authorization: `Bearer ${this.state.authToken}`,
authorization: `Bearer ${authToken}`,
},
body: JSON.stringify({
query:
Expand Down
1 change: 1 addition & 0 deletions packages/auth/src/authClients/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ export const firebase = (client: Firebase): AuthClient => {
},
getToken: async () => client.auth().currentUser?.getIdToken() ?? null,
getUserMetadata: async () => client.auth().currentUser,
onTokenChange: client.auth().onIdTokenChanged,
}
}
1 change: 1 addition & 0 deletions packages/auth/src/authClients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface AuthClient {
getUserMetadata(): Promise<null | SupportedUserMetadata>
client: SupportedAuthClients
type: SupportedAuthTypes
onTokenChange?(callback: () => void): void
}

export const createAuthClient = (
Expand Down