Skip to content

Commit

Permalink
Merge pull request #7 from reindexio/auth0
Browse files Browse the repository at this point in the history
Add support for Auth0 login and `loginWithToken`
  • Loading branch information
fson committed Feb 25, 2016
2 parents 4f86258 + aaca66d commit d40faa5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"babel-core": "^5.8.25",
"babel-eslint": "^3.1.15",
"babel-loader": "^5.1.4",
"escope": "^3.3.0",
"eslint": "^0.23",
"eslint-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0",
Expand Down
34 changes: 34 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import ReindexRelayNetworkLayer from './ReindexRelayNetworkLayer';
const TOKEN_KEY = 'REINDEX_TOKEN';

const SUPPORTED_PROVIDERS = {
auth0: {
windowFeatures: { width: 320, height: 568 },
},
facebook: {
windowFeatures: { width: 500, height: 467 },
},
Expand All @@ -29,6 +32,17 @@ const SUPPORTED_PROVIDERS = {
},
};

const loginWithTokenQuery = `
mutation LoginWithTokenMutation($input: ReindexLoginWithTokenInput!) {
loginWithToken(input: $input) {
user {
id
}
token
}
}
`;

function stringifyWindowFeatures(windowFeatures) {
return Object.keys(windowFeatures)
.map((key) => `${key}=${windowFeatures[key]}`)
Expand Down Expand Up @@ -94,6 +108,26 @@ class Reindex extends EventEmitter {
});
}

async loginWithToken(provider, token) {
invariant(
SUPPORTED_PROVIDERS[provider],
'Reindex.loginWithToken(...): unknown provider "%s"',
provider,
);
const input = {
token,
provider,
};
const { data, errors } = await this.query(loginWithTokenQuery, { input });
if (errors) {
throw new Error(errors[0].message);
}
const response = data.loginWithToken;
this.setToken(response.token);
this.emit('login', response);
return response;
}

logout() {
this.setToken(null);
this.emit('logout');
Expand Down

0 comments on commit d40faa5

Please sign in to comment.