Skip to content

Instantly share code, notes, and snippets.

@garybunofsky
Created February 6, 2018 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garybunofsky/2c6d3589f03e424bc6174b058212e505 to your computer and use it in GitHub Desktop.
Save garybunofsky/2c6d3589f03e424bc6174b058212e505 to your computer and use it in GitHub Desktop.
/* 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