Created
February 6, 2018 23:05
-
-
Save garybunofsky/2c6d3589f03e424bc6174b058212e505 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* globals localStorage */ | |
const OktaAuth = require('@okta/okta-auth-js') | |
const authClient = new OktaAuth({url: 'https://dev-156391-admin.oktapreview.com'}) | |
export default { | |
login (email, pass, cb) { | |
cb = arguments[arguments.length - 1] | |
if (localStorage.token) { | |
if (cb) cb(true) | |
this.onChange(true) | |
return | |
} | |
return authClient.signIn({ | |
username: email, | |
password: pass | |
}).then(response => { | |
if (response.status === 'SUCCESS') { | |
console.log(response.sessionToken) | |
localStorage.token = response.token | |
authClient.token.getWithoutPrompt({ | |
clientId: '0oaduemimaXn79sxv0h7', | |
responseType: ['id_token', 'token'], | |
scopes: ['openid', 'profile', 'email'], | |
sessionToken: response.sessionToken, | |
redirectUri: 'http://localhost:8080' | |
}).then(function (tokenOrTokens) { | |
console.log('tokenOrTokens') | |
console.log(tokenOrTokens) | |
}).catch(function (err) { | |
console.log('err') | |
console.log(err) | |
}) | |
// if (cb) cb(true) | |
// this.onChange(true) | |
} | |
}).fail(err => { | |
console.error(err.message) | |
if (cb) cb(false) | |
this.onChange(false) | |
}) | |
}, | |
getToken () { | |
return localStorage.token | |
}, | |
logout (cb) { | |
delete localStorage.token | |
if (cb) cb() | |
this.onChange(false) | |
return authClient.signOut() | |
}, | |
loggedIn () { | |
return !!localStorage.token | |
}, | |
onChange () { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment