-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apollo): add middleware for ember-apollo-client
- Loading branch information
Showing
15 changed files
with
403 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { assert } from "@ember/debug"; | ||
import { | ||
dependencySatisfies, | ||
macroCondition, | ||
importSync, | ||
} from "@embroider/macros"; | ||
import { handleUnauthorized } from "ember-simple-auth-oidc"; | ||
|
||
let apolloMiddleware; | ||
|
||
if (macroCondition(dependencySatisfies("@apollo/client", ">=3.0.0"))) { | ||
const { setContext } = importSync("@apollo/client/link/context"); | ||
const { onError } = importSync("@apollo/client/link/error"); | ||
|
||
apolloMiddleware = (httpLink, session) => { | ||
const authMiddleware = setContext(async (_, context) => { | ||
await session.refreshAuthentication.perform(); | ||
|
||
return { | ||
...context, | ||
headers: { | ||
...context.headers, | ||
...session.headers, | ||
}, | ||
}; | ||
}); | ||
|
||
const authAfterware = onError((error) => { | ||
if (error.networkError && error.networkError.statusCode === 401) { | ||
handleUnauthorized(session); | ||
} | ||
}); | ||
|
||
return authMiddleware.concat(authAfterware).concat(httpLink); | ||
}; | ||
} else { | ||
apolloMiddleware = () => | ||
assert( | ||
"@apollo/client >= 3.0.0 must be installed in order to use the apollo middleware" | ||
); | ||
} | ||
|
||
export default apolloMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,2 @@ | ||
import { getOwner } from "@ember/application"; | ||
|
||
import { getConfig } from "ember-simple-auth-oidc/config"; | ||
import getAbsoluteUrl from "ember-simple-auth-oidc/utils/absoluteUrl"; | ||
|
||
export const handleUnauthorized = (session) => { | ||
if (session.isAuthenticated) { | ||
session.set("data.nextURL", location.href.replace(location.origin, "")); | ||
session.invalidate(); | ||
|
||
const owner = getOwner(session); | ||
|
||
if ( | ||
owner.resolveRegistration("config:environment").environment !== "test" | ||
) { | ||
location.replace(getAbsoluteUrl(getConfig(owner).afterLogoutUri || "")); | ||
} | ||
} | ||
}; | ||
|
||
export default { handleUnauthorized }; | ||
export { default as handleUnauthorized } from "ember-simple-auth-oidc/unauthorized"; | ||
export { default as apolloMiddleware } from "ember-simple-auth-oidc/apollo"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getOwner } from "@ember/application"; | ||
|
||
import { getConfig } from "ember-simple-auth-oidc/config"; | ||
import getAbsoluteUrl from "ember-simple-auth-oidc/utils/absoluteUrl"; | ||
|
||
export default function handleUnauthorized(session) { | ||
if (session.isAuthenticated) { | ||
session.set("data.nextURL", location.href.replace(location.origin, "")); | ||
session.invalidate(); | ||
|
||
const owner = getOwner(session); | ||
|
||
if ( | ||
owner.resolveRegistration("config:environment").environment !== "test" | ||
) { | ||
location.replace(getAbsoluteUrl(getConfig(owner).afterLogoutUri || "")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Controller from "@ember/controller"; | ||
import { action } from "@ember/object"; | ||
import { queryManager } from "ember-apollo-client"; | ||
import { gql } from "graphql-tag"; | ||
|
||
export default class ApolloController extends Controller { | ||
@queryManager apollo; | ||
|
||
@action | ||
triggerUnauthenticated(e) { | ||
this.apollo.mutate({ | ||
mutation: gql` | ||
mutation { | ||
mutate { | ||
clientMutationId | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
e.preventDefault(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Route from "@ember/routing/route"; | ||
import { queryManager } from "ember-apollo-client"; | ||
import { gql } from "graphql-tag"; | ||
|
||
export default class ApolloRoute extends Route { | ||
@queryManager apollo; | ||
|
||
model() { | ||
return this.apollo.watchQuery({ | ||
query: gql` | ||
query { | ||
items { | ||
id | ||
name | ||
} | ||
} | ||
`, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { inject as service } from "@ember/service"; | ||
import ApolloService from "ember-apollo-client/services/apollo"; | ||
import { apolloMiddleware } from "ember-simple-auth-oidc"; | ||
|
||
export default class CustomApolloService extends ApolloService { | ||
@service session; | ||
|
||
link() { | ||
const httpLink = super.link(); | ||
|
||
return apolloMiddleware(httpLink, this.session); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<ul> | ||
{{#each @model.items as |item|}} | ||
<li>{{item.name}}</li> | ||
{{/each}} | ||
</ul> | ||
|
||
<button type="button" {{on "click" this.triggerUnauthenticated}}>Trigger 401</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.