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
+38 -4
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,
}
)
@@ -33,15 +33,6 @@ export type AuthenticationResponse = {
* specified location.
*/
location?: string
/**
* Some authentication providers support redirecting to a specified URL on
* success. In those cases, the URL to redirect to is set in this field.
*
* So, if `success` is true, there's no `location` set, and this field
* is set, you can redirect to this URL.
*/
successRedirectUrl?: string
}
/**
+7
View File
@@ -13,6 +13,13 @@ export interface AuthIdentityProviderService {
provider_metadata?: Record<string, unknown>
user_metadata?: Record<string, unknown>
}) => Promise<AuthIdentityDTO>
update: (
entity_id: string,
data: {
provider_metadata?: Record<string, unknown>
user_metadata?: Record<string, unknown>
}
) => Promise<AuthIdentityDTO>
}
/**
@@ -0,0 +1,5 @@
export interface GithubAuthProviderOptions {
clientId: string
clientSecret: string
callbackUrl: string
}
@@ -1,6 +1,5 @@
export interface GoogleAuthProviderOptions {
clientID: string
clientId: string
clientSecret: string
callbackURL: string
successRedirectUrl?: string
callbackUrl: string
}
@@ -1,2 +1,3 @@
export * from "./emailpass"
export * from "./google"
export * from "./github"
+3 -3
View File
@@ -46,12 +46,12 @@ export interface IAuthModuleService extends IModuleService {
*/
authenticate(
provider: string,
providerData: AuthenticationInput,
providerData: AuthenticationInput
): Promise<AuthenticationResponse>
register(
provider: string,
providerData: AuthenticationInput,
providerData: AuthenticationInput
): Promise<AuthenticationResponse>
/**
@@ -76,7 +76,7 @@ export interface IAuthModuleService extends IModuleService {
* `req` is an instance of the `MedusaRequest` object:
*
* ```ts
* const { success, authIdentity, error, successRedirectUrl } =
* const { success, authIdentity, error } =
* await authModuleService.validateCallback("google", {
* url: req.url,
* headers: req.headers,