Created
July 12, 2016 18:22
-
-
Save macnibblet/4f34c69f4c7b4bcd9ea550df4b1d55eb to your computer and use it in GitHub Desktop.
This file contains 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
var React = require('react-native'); | |
var { | |
NetInfo | |
} = React; | |
var Events = require('react-native-simple-events'); | |
var auth; | |
var backendDomain; | |
var onLogin; | |
var onLogout; | |
function doLogout () { | |
auth = undefined; | |
onLogout(); | |
} | |
exports.getAuth = function () { | |
return auth; | |
} | |
exports.logout = function () { | |
doLogout(); | |
} | |
exports.refreshAuth = function (onSuccess, onError) { | |
// Note: This one does not throw errors directly, it just calls the supplied onError. That way, not everything needs to listen to authentication-error. | |
NetInfo.isConnected.fetch().done((isConnected) => { | |
if (isConnected) { | |
var formData = new FormData(); | |
formData.append('grant_type', 'refresh_token'); | |
formData.append('refresh_token', auth.refresh_token); | |
fetch(backendDomain + "/oauth/token", { | |
method: 'POST', | |
body: formData | |
}).then((response) => { | |
switch (response.status) { | |
case 200: | |
var email = auth.email; | |
var data = JSON.parse(response._bodyText); | |
auth = { | |
email: email, | |
id: data.owner_id, | |
access_token: data.access_token, | |
refresh_token: data.refresh_token | |
} | |
onSuccess(); | |
break; | |
case 400: | |
case 401: | |
doLogout(); // If the tokenrefresh authfails, log us out | |
break; | |
default: | |
onError('ERROR_TECHNICAL'); | |
break; | |
} | |
}).catch((error) => { | |
onError('ERROR_TECHNICAL'); | |
}); | |
} else { | |
onError('ERROR_NOTONLINE'); | |
} | |
}); | |
} | |
exports.authenticate = function (email, password) { | |
NetInfo.isConnected.fetch().done((isConnected) => { | |
if (isConnected) { | |
var formData = new FormData(); | |
formData.append('grant_type', 'password'); | |
formData.append('username', email); | |
formData.append('password', password); | |
fetch(backendDomain + "/oauth/token", { | |
method: 'POST', | |
body: formData | |
}) | |
.then((response) => response.json()) | |
console.log(response.text()); | |
switch (response.status) { | |
case 200: | |
var data = JSON.parse(response._bodyText); | |
auth = { | |
email: email, | |
id: data.owner_id, | |
access_token: data.access_token, | |
refresh_token: data.refresh_token | |
} | |
onLogin(); | |
break; | |
case 400: | |
case 401: | |
Events.trigger('authentication-error', {key: 'ERROR_LOGIN_NOAUTH'}); | |
break; | |
default: | |
Events.trigger('authentication-error', {key: 'ERROR_TECHNICAL'}); | |
break; | |
} | |
}).catch((error) => { | |
Events.trigger('authentication-error', {key: 'ERROR_TECHNICAL'}); | |
}); | |
} else { | |
Events.trigger('authentication-error', {key: 'ERROR_NOTONLINE'}); | |
} | |
}); | |
} | |
exports.init = function (_backendDomain, _onLogin, _onLogout) { | |
onLogin = _onLogin; | |
onLogout = _onLogout; | |
backendDomain = _backendDomain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment