Skip to content

Instantly share code, notes, and snippets.

@cliftonc
Last active June 8, 2016 20:10
Show Gist options
  • Save cliftonc/1ef41ab3ff08780ab90ac9898be6c5e5 to your computer and use it in GitHub Desktop.
Save cliftonc/1ef41ab3ff08780ab90ac9898be6c5e5 to your computer and use it in GitHub Desktop.
bhlha
import axios from 'axios';
import endpoints from 'module-tsl-endpoints';
import environment from 'module-tsl-environment';
const registrationBaseUrl = endpoints.getSync('registration', environment);
const headers = (config) => ({
Accept: 'application/json',
contentType: 'application/json',
'x-tslauthapp': `${config.id}:${config.secret}` });
// NOTE: Only use getIdentityFromLocation if you KNOW that the endpoint you're calling returns a location header.
export default (config) => {
const getIdentityFromLocation = (res) => axios.get(`${res.headers.location}`, { headers: headers(config) })
const getIdentityFromLocationOauth = (res) => {
const isRegistration = res.statusCode === 200;
return axios.get(`${res.headers.location}`, { headers: headers(config) }).then((res) => {
res.isRegistration = isRegistration;
return res;
})
}
return {
registerUser(user) {
return axios.post(`${registrationBaseUrl}/identities`, user, { headers: headers(config), timeout: 2000 })
.then(getIdentityFromLocation);
},
registerUserViaOauth(user) {
return axios.post(`${registrationBaseUrl}/oauth/identities`, user, { headers: headers(config), timeout: 2000 })
.then(getIdentityFromLocationOauth);
},
findUser(identifier) {
return axios.get(`${registrationBaseUrl}/identities?identifier=${identifier}`, { headers: headers(config) });
},
getIdentity(userId) {
return axios.get(`${registrationBaseUrl}/identities/${userId}`, { headers: headers(config) });
},
forgotPassword(userId) {
return axios.post(`${registrationBaseUrl}/identities/${userId}/forgot-password`, null, { headers: headers(config) });
},
resetPassword(userId, token, newPassword) {
return axios.post(`${registrationBaseUrl}/identities/${userId}/reset-password`, { token, newPassword }, { headers: headers(config) })
.then(getIdentityFromLocation);
},
checkResetPassword(userId, token) {
return axios.get(`${registrationBaseUrl}/identities/${userId}/check-reset-password?token=${token}`, { headers: headers(config) });
},
confirmEmail(userId, token) {
return axios.post(`${registrationBaseUrl}/identities/${userId}/confirm-email`, { token }, { headers: headers(config) })
.then(getIdentityFromLocation);
},
updateEmail(userId, email) {
return axios.put(`${registrationBaseUrl}/identities/${userId}`, { email }, { headers: headers(config) })
.then(getIdentityFromLocation);
},
updateSiteCountry(userId, siteCountry) {
return axios.put(`${registrationBaseUrl}/identities/${userId}`, { siteCountry }, { headers: headers(config) });
},
updateAccount(userId, userData) {
return axios.put(`${registrationBaseUrl}/identities/${userId}`, userData, { headers: headers(config) })
.then(getIdentityFromLocation);
},
resendConfirmationEmail(userId) {
return axios.post(`${registrationBaseUrl}/identities/${userId}/resend-confirmation-email`, {}, { headers: headers(config) })
.then(getIdentityFromLocation);
},
verifyCredentials(credentials) {
return axios.post(`${registrationBaseUrl}/verify/credentials`, credentials, { headers: headers(config) })
.then(getIdentityFromLocation);
},
auditingImpersonation(userId, modifiedBy) {
return axios.post(`${registrationBaseUrl}/identities/${userId}/impersonate`, { modifiedBy, description: 'Impersonated' }, { headers: headers(config) });
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment