feat: Add github authentication provider (#8980)

* feat: Add github authentication provider

* feat: Change callback to always return a token, expect callbackUrl to point to FE

* fix: Return login redirect URLas a 200 response
This commit is contained in:
Stevche Radevski
2024-09-04 13:14:00 +02:00
committed by GitHub
parent fb832072a4
commit af4f8811bd
23 changed files with 717 additions and 46 deletions

View File

@@ -30,15 +30,49 @@ export class Auth {
}
login = async (
actor: string,
method: string,
payload: HttpTypes.AdminSignInWithEmailPassword | Record<string, unknown>
) => {
// There will either be token or location returned from the backend.
const { token, location } = await this.client.fetch<{
token?: string
location?: string
}>(`/auth/${actor}/${method}`, {
method: "POST",
body: payload,
})
// In the case of an oauth login, we return the redirect location to the caller.
// They can decide if they do an immediate redirect or put it in an <a> tag.
if (location) {
return { location }
}
// By default we just set the token in memory, if configured to use sessions we convert it into session storage instead.
if (this.config?.auth?.type === "session") {
await this.client.fetch("/auth/session", {
method: "POST",
headers: { Authorization: `Bearer ${token}` },
})
} else {
this.client.setToken(token as string)
}
return token
}
// The callback expects all query parameters from the Oauth callback to be passed to the backend, and the provider is in charge of parsing and validating them
callback = async (
actor: "customer" | "user",
method: "emailpass",
payload: HttpTypes.AdminSignInWithEmailPassword
query?: Record<string, unknown>
) => {
const { token } = await this.client.fetch<{ token: string }>(
`/auth/${actor}/${method}`,
`/auth/${actor}/${method}/callback`,
{
method: "POST",
body: payload,
method: "GET",
query,
}
)