Skip to content

Commit

Permalink
feat: Always return token in auth callback, do callback redirect to FE
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski committed Sep 4, 2024
1 parent b57add0 commit 15c12bf
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 31 deletions.
9 changes: 0 additions & 9 deletions packages/core/types/src/auth/common/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/core/types/src/auth/providers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export interface GithubAuthProviderOptions {
clientId: string
clientSecret: string
callbackUrl: string
successRedirectUrl?: string
}
1 change: 0 additions & 1 deletion packages/core/types/src/auth/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export interface GoogleAuthProviderOptions {
clientId: string
clientSecret: string
callbackUrl: string
successRedirectUrl?: string
}
6 changes: 3 additions & 3 deletions packages/core/types/src/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>

/**
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
protocol: req.protocol,
} as AuthenticationInput

const { success, error, authIdentity, successRedirectUrl } =
await service.validateCallback(auth_provider, authData)
const { success, error, authIdentity } = await service.validateCallback(
auth_provider,
authData
)

const entityIdKey = `${actor_type}_id`
const entityId = authIdentity?.app_metadata?.[entityIdKey] as
Expand Down Expand Up @@ -71,13 +73,6 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}
)

if (successRedirectUrl) {
const url = new URL(successRedirectUrl!)
url.searchParams.append("access_token", token)

return res.redirect(url.toString())
}

return res.json({ token })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ describe("Github auth provider", () => {
{
clientId: "test",
clientSecret: "test",
successRedirectUrl: baseUrl,
callbackUrl: `${baseUrl}/auth/github/callback`,
}
)
Expand Down Expand Up @@ -179,7 +178,6 @@ describe("Github auth provider", () => {

expect(res).toEqual({
success: true,
successRedirectUrl: baseUrl,
authIdentity: {
provider_identities: [
{
Expand Down Expand Up @@ -232,7 +230,6 @@ describe("Github auth provider", () => {

expect(res).toEqual({
success: true,
successRedirectUrl: baseUrl,
authIdentity: {
provider_identities: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export class GithubAuthService extends AbstractAuthModuleProvider {
return {
success,
authIdentity,
successRedirectUrl: this.config_.successRedirectUrl,
}
} catch (error) {
return { success: false, error: error.message }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ describe("Google auth provider", () => {
{
clientId: "test",
clientSecret: "test",
successRedirectUrl: baseUrl,
callbackUrl: `${baseUrl}/auth/google/callback`,
}
)
Expand Down Expand Up @@ -178,7 +177,6 @@ describe("Google auth provider", () => {

expect(res).toEqual({
success: true,
successRedirectUrl: baseUrl,
authIdentity: {
provider_identities: [
{
Expand Down Expand Up @@ -221,7 +219,6 @@ describe("Google auth provider", () => {

expect(res).toEqual({
success: true,
successRedirectUrl: baseUrl,
authIdentity: {
provider_identities: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export class GoogleAuthService extends AbstractAuthModuleProvider {
return {
success,
authIdentity,
successRedirectUrl: this.config_.successRedirectUrl,
}
} catch (error) {
return { success: false, error: error.message }
Expand Down

0 comments on commit 15c12bf

Please sign in to comment.