Created
December 19, 2016 19:11
-
-
Save sirgalleto/2631a21543ed9d69883b96db5f86253e to your computer and use it in GitHub Desktop.
Authorization + Rest interactions for react-native
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
import { AsyncStorage } from 'react-native'; | |
import EventEmmiter from 'EventEmitter'; | |
import { apiRouter } from './services'; | |
import { headers, access } from '../constants'; | |
export class Auth extends EventEmmiter{ | |
key = '@gos3s1on'; | |
constructor() { | |
super(); | |
this.storageSession.then((data) => { | |
this.data = JSON.parse(data); | |
}); | |
} | |
get session() { | |
return new Promise((resolve, reject) => { | |
if(this.data) { | |
resolve(this.data); | |
} | |
else { | |
this.storageSession.then((data) => { | |
this.data = JSON.parse(data); | |
resolve(this.data); | |
}); | |
} | |
}); | |
} | |
password(username, password) { | |
let data = new FormData(); | |
return fetch( apiRouter.route('auth/token'), { | |
method: 'post', | |
headers: { | |
'Content-Type': headers.urlencoded, | |
'Accept': headers.accept | |
}, | |
body: this._encode(access.password(username, password)) | |
}).then(function(response) { | |
if(!response.ok) { | |
return Promise.reject(response); | |
} | |
return response.json(); | |
}); | |
} | |
logIn(data) { | |
AsyncStorage.setItem(this.key, JSON.stringify(data)); | |
this.data = data; | |
this.emit('onLogIn', data); | |
} | |
logOut() { | |
AsyncStorage.removeItem(this.key); | |
this.data = null; | |
this.emit('onLogOut'); | |
} | |
get storageSession() { | |
return AsyncStorage.getItem(this.key); | |
} | |
_encode(params) { | |
let encoded = []; | |
for (var property in params) { | |
const encodedKey = encodeURIComponent(property); | |
const encodedValue = encodeURIComponent(params[property]); | |
encoded.push(encodedKey + '=' + encodedValue); | |
} | |
return encoded.join('&'); | |
} | |
} | |
//Return a "singleton" | |
export default auth = new Auth(); |
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
import { headers, access } from '../constants'; | |
import { base64 } from './services'; | |
export default class Http { | |
constructor(auth) { | |
auth.session.then((data) => { | |
this.session = data; | |
}); | |
auth.addListener('onLogIn', (data) => { | |
this.session = data; | |
}); | |
auth.addListener('onLogOut', () => { | |
this.session = null; | |
}); | |
} | |
request(method, url, queryParams, body, auth) { | |
let _headers = { | |
'Accept': headers.json, | |
'Content-Type': headers.json, | |
}; | |
if(auth && this.session) { | |
_headers['Authorization'] = headers.authorization( | |
this.session.token_type, this.session.access_token | |
); | |
} | |
else { | |
_headers['Authorization'] = headers.authorization( | |
access.client.type, base64.encode( | |
`${access.client.id}:${access.client.secret}` | |
) | |
); | |
} | |
let data = { | |
method: method, | |
headers: _headers | |
}; | |
if(method !== 'GET') { | |
data.body = JSON.stringify(body); | |
} | |
return fetch(url, data) | |
.then(this._onSuccess) | |
.catch(this._onError); | |
} | |
_onSuccess(response) { | |
if(!response.ok) { | |
return response.json().then((error) => { | |
console.log(error); | |
return Promise.reject(error); | |
}); | |
} | |
return response.json().then((response) => { | |
return response.data; | |
}); | |
} | |
_onError(error) { | |
return Promise.reject(error); | |
} | |
} |
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
import auth from './Auth'; | |
import Http from './Http'; | |
import { apiRouter } from './services'; | |
export default class Rest extends Http { | |
constructor(path) { | |
super(auth); | |
this.route = apiRouter.route(path); | |
} | |
find(id, query) { | |
return this.request( | |
'GET', | |
this._routeId(id), | |
query, | |
null, | |
true | |
); | |
} | |
insert(data) { | |
return this.request( | |
'POST', | |
this.route, | |
null, | |
data, | |
true | |
); | |
} | |
update(id, data) { | |
return this.request( | |
'PUT', | |
this._routeId(id), | |
null, | |
data, | |
true | |
); | |
} | |
delete(id) { | |
return this.request( | |
'DELETE', | |
this._routeId(id), | |
null, | |
null, | |
true | |
); | |
} | |
_routeId(id) { | |
id = id || ''; | |
return `${this.route}/${id}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment