Skip to content

Commit

Permalink
feat!: change loginWithOAuth API signature
Browse files Browse the repository at this point in the history
BREAKING CHANGE!: stop guessing which flow to use based on content of options.
  • Loading branch information
vegardok committed Jun 23, 2021
1 parent 00dacb4 commit 15f3a28
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 134 deletions.
28 changes: 14 additions & 14 deletions guides/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ After signing in, the browser window will be redirected back to your application
```js
import { CogniteClient, REDIRECT } from '@cognite/sdk';
const client = new CogniteClient({ ... });
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
// default is redirect
// but can be explicitly specified:
Expand All @@ -101,7 +101,7 @@ If you want a different redirect url back to your app after a successful / unsuc
```js
import { CogniteClient, REDIRECT } from '@cognite/sdk';
const client = new CogniteClient({ ... });
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
onAuthenticate: login => {
login.redirect({
Expand Down Expand Up @@ -130,7 +130,7 @@ if (isLoginPopupWindow()) {
return;
}
const client = new CogniteClient({ ... });
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
onAuthenticate: POPUP,
});
Expand All @@ -154,7 +154,7 @@ If you want a different redirect url back to your application after a successful
```js
import { CogniteClient, POPUP } from '@cognite/sdk';
const client = new CogniteClient({ ... });
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
onAuthenticate: login => {
login.popup({
Expand All @@ -173,7 +173,7 @@ This only affect the pop-up window.

To avoid waiting for the first `401`-response to occur you can trigger the authentication flow manually like this:
```js
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
});
await client.authenticate(); // this will also return a boolean based on if the user successfully authenticated or not.
Expand All @@ -183,7 +183,7 @@ await client.authenticate(); // this will also return a boolean based on if the

If you already have a access token you can use it to skip the authentication flow (see this [section](#tokens) on how to get hold of the token). If the token is invalid or timed out the SDK will trigger a standard auth-flow on the first 401-response from CDF.
```js
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
accessToken: 'ACCESS TOKEN FOR THE PROJECT HERE',
});
Expand All @@ -194,7 +194,7 @@ client.loginWithOAuth({

It is possible to skip the authentication like this:
```js
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
onAuthenticate: login => {
login.skip();
Expand All @@ -208,7 +208,7 @@ If you want to use redirect method in the initialization of your app and use the
you can implement something like this:

```js
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
onAuthenticate: login => {
// some check:
Expand All @@ -226,7 +226,7 @@ client.loginWithOAuth({
If you need access to the tokens (access token, id token) from the login flow you can add a callback like this:

```js
client.loginWithOAuth({
client.loginWithOAuth('CDF_OAUTH', {
project: 'YOUR PROJECT NAME HERE',
onTokens: ({accessToken, idToken}) => {
// your logic here
Expand Down Expand Up @@ -269,7 +269,7 @@ After signing in, the browser window will be redirected back to your application

> You might find useful example application using redirect Azure AD auth flow [here](../samples/react/authentication-aad/src/App.js).
> Remember to provide the required environment variables in the `.env` file.
#### Redirect sign in type example

```js
Expand All @@ -278,7 +278,7 @@ const client = new CogniteClient({ ... });

// tenantId parameter can be skipped in order to use,
// https://login.microsoftonline.com/common endpoint to authenticate user
client.loginWithOAuth({
client.loginWithOAuth('AAD_OAUTH', {
cluster: 'cdf-cluster-name',
clientId: 'azure-application-client-id',
tenantId: 'azure-tenant-id'
Expand All @@ -297,7 +297,7 @@ const assets = await client.assets.retrieve({ id: 23232789217132 });
With the call `await client.authenticate()` you'll be redirected to the IdP to sign in.
After you have signed in, you'll be redirected back and `await client.authenticate()` call
will return you `true` as a result of the successful login. It is important
to set project for the `CogniteClient` instance via `client.setProject('project-name')`
to set project for the `CogniteClient` instance via `client.setProject('project-name')`

### Authentication via pop-up

Expand All @@ -313,7 +313,7 @@ const client = new CogniteClient({ ... });

// tenantId parameter can be skipped in order to use,
// https://login.microsoftonline.com/common endpoint to authenticate user
client.loginWithOAuth({
client.loginWithOAuth('AAD_OAUTH', {
cluster: 'cdf-cluster-name',
clientId: 'azure-application-client-id',
tenantId: 'azure-tenant-id',
Expand Down Expand Up @@ -343,7 +343,7 @@ This method works only for Azure AD authentication flow. You can also check whic
import { CogniteClient, AZURE_AUTH_POPUP, AAD_OAUTH } from '@cognite/sdk';
const client = new CogniteClient({ ... });

client.loginWithOAuth({
client.loginWithOAuth('AAD_OAUTH', {
cluster: 'cdf-cluster-name',
clientId: 'azure-application-client-id',
signInType: AZURE_AUTH_POPUP,
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"clean": "rm -rf dist/ docs/",
"test": "jest --config=../../jest.config.js --testPathPattern=/core/",
"test:debug": "node --inspect-brk ../../node_modules/.bin/jest --config=../../jest.config.js --testPathPattern=/core/ --runInBand" ,
"lint": "eslint 'src/**/*.{js,ts}'",
"lint:fix": "yarn lint --fix",
"prepublishOnly": "yarn build",
Expand Down
Loading

0 comments on commit 15f3a28

Please sign in to comment.