Skip to content

Commit

Permalink
Merge 82563d8 into 1b49e24
Browse files Browse the repository at this point in the history
  • Loading branch information
shekoufa committed Mar 6, 2018
2 parents 1b49e24 + 82563d8 commit acdc533
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
26 changes: 24 additions & 2 deletions src/actions.js
Expand Up @@ -14,9 +14,31 @@ export const loginFailure = error => ({
error
})

export const login = config => ({
export const login = (
{
url,
client,
redirect,
scope,
responseType = 'token',
accessTokenKey = 'access_token',
parseQuery = false,
width = 400,
height = 400
}
) => ({
type: LOGIN_REQUEST,
config
config: {
url,
client,
redirect,
scope,
responseType,
accessTokenKey,
parseQuery,
width,
height
}
})

export const logout = () => ({
Expand Down
24 changes: 19 additions & 5 deletions src/oauth2.js
Expand Up @@ -2,10 +2,24 @@ import querystring from 'query-string'
import cuid from 'cuid'
import openPopup from './util/popup'

const listenForCredentials = (popup, state, resolve, reject) => {
const listenForCredentials = (popup, state, resolve, reject, config) => {
let hash
let credentialsFound = true
try {
hash = popup.location.hash
if (config.parseQuery) {
credentialsFound = false
const parsedQuery = querystring.parse(popup.location.search)
if (parsedQuery[config.accessTokenKey]) {
hash = hash ? hash + '&access_token=' + parsedQuery[config.accessTokenKey] : '#access_token=' + parsedQuery[config.accessTokenKey]
}
if (parsedQuery.state) {
hash = hash ? hash + '&state=' + parsedQuery.state : '#state=' + parsedQuery.state
}
if (hash.includes('access_token') && hash.includes('state')) {
credentialsFound = true
}
}
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
/* eslint-disable no-console */
Expand All @@ -14,7 +28,7 @@ const listenForCredentials = (popup, state, resolve, reject) => {
}
}

if (hash) {
if (hash && credentialsFound) {
popup.close()

const response = querystring.parse(hash.substr(1))
Expand All @@ -37,15 +51,15 @@ const listenForCredentials = (popup, state, resolve, reject) => {
} else if (popup.closed) {
reject('Authentication was cancelled.')
} else {
setTimeout(() => listenForCredentials(popup, state, resolve, reject), 100)
setTimeout(() => listenForCredentials(popup, state, resolve, reject, config), 100)
}
}

const authorize = config => {
const state = cuid()
const query = querystring.stringify({
state,
response_type: 'token',
response_type: config.responseType,
client_id: config.client,
scope: config.scope,
redirect_uri: config.redirect
Expand All @@ -56,7 +70,7 @@ const authorize = config => {
const popup = openPopup(url, 'oauth2', width, height)

return new Promise((resolve, reject) =>
listenForCredentials(popup, state, resolve, reject)
listenForCredentials(popup, state, resolve, reject, config)
)
}

Expand Down

0 comments on commit acdc533

Please sign in to comment.