Created
March 21, 2018 15:31
-
-
Save chabibnr/19dfa6f370f73bc2229a7d5c05f769d4 to your computer and use it in GitHub Desktop.
Implementasi pada aYom
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
'use strict'; | |
/** | |
* | |
* @providesModule @config/api | |
*/ | |
import {Alert} from 'react-native'; | |
const Auth = require('../libraries/AuthLibrary'); | |
const dateTime = require('node-datetime'); | |
module.exports ={ | |
uri: { | |
login: 'api/oauth2/login', | |
token: 'api/oauth2/token', | |
revoke: 'api/oauth2/revoke', | |
post: { | |
list : 'api/post/index?sort=-id&expand=attachment', | |
create: 'api/post/add' | |
} | |
}, | |
code: { | |
VALIDATION : 422, | |
CREATED: 201, | |
UNAUTHORIZED: 401, | |
SUCCESS: 200 | |
}, | |
headers: { | |
'Accept': 'application/json', | |
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
}, | |
headerMultipart: { | |
'Accept': 'application/json', | |
'Content-Type': 'multipart/form-data' | |
}, | |
//host: 'http://192.168.43.139:8080/', | |
host: 'http://192.168.0.27:8080/', | |
client_id: 'testclient', | |
client_secret: 'testpass', | |
http(uri, config) { | |
}, | |
async httpToken(){ | |
let params = JSON.stringify({ | |
refresh_token: await Auth.getRefreshToken(), | |
grant_type: 'refresh_token', | |
client_id: this.client_id, | |
client_secret: this.client_secret, | |
}); | |
console.log(params); | |
return fetch(this.host + this.uri.token, { | |
method: 'POST', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: params | |
}) | |
}, | |
async httpSecure(uri, body, method){ | |
let exp = Number(await Auth.getTokenExpire()); | |
let now = dateTime.create(new Date()); | |
if(now.getTime() > exp){ | |
console.info('Token Refresh'); | |
let responseToken = await this.httpToken() | |
.then((res) => { | |
if(res.status === this.code.SUCCESS){ | |
res.json() | |
.then((res) => { | |
Auth.setToken(res.access_token); | |
Auth.setRefreshToken(res.refresh_token); | |
Auth.setTokenExpire(now.getTime() + (res.expires_in * 1000)); | |
}); | |
return true; | |
}else { | |
throw 'error refresh token' | |
//new Error('err'); | |
//alert('Auth Failed') | |
} | |
}).catch((e) => { | |
return false; | |
}); | |
if(responseToken === false){ | |
Alert.alert('Unauthorize', 'Ops Failed to detect your account, please resignin now!!'); | |
//Auth.setRefreshToken('427f561f47c9f35bc65421312f35bec9538ed4af'); | |
return false; | |
} | |
} | |
let token = await Auth.getToken(); | |
let config = { | |
method: method === undefined ? 'GET' : method, | |
headers: { | |
'Accept': 'application/json', | |
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
'Authorization': 'Bearer ' + token | |
} | |
}; | |
if(body instanceof FormData){ | |
config.headers['Content-type'] = 'multipart/form-data'; | |
config.method = 'POST'; | |
} | |
if(config.method === 'GET'){ | |
config.params = body; | |
}else { | |
config.body = body; | |
} | |
return fetch(this.host + uri, config) | |
.then((res) => { | |
if(res.status === this.code.UNAUTHORIZED){ | |
Alert.alert('Unauthorize', 'Ops Failed to detect your account, please resignin now!!') | |
return false; | |
} | |
return res; | |
}) | |
}, | |
httpMultipart(uri, config){ | |
} | |
}; |
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
'use strict'; | |
import {AsyncStorage} from 'react-native'; | |
let Auth = { | |
async getIsLogin(){ | |
try { | |
let isLogin = await AsyncStorage.getItem("@SESSION:isLogin"); | |
return '1' === isLogin; | |
}catch(err){ | |
return false; | |
} | |
}, | |
async setIsLogin(login){ | |
login = login ? '1' : '0'; | |
return await AsyncStorage.setItem("@SESSION:isLogin", login); | |
}, | |
async getToken(){ | |
try { | |
let token = await AsyncStorage.getItem("@SESSION:token"); | |
return token; | |
}catch(err){ | |
return null; | |
} | |
}, | |
async setToken(token){ | |
return await AsyncStorage.setItem("@SESSION:token", token); | |
}, | |
async getTokenExpire(){ | |
try { | |
let time = await AsyncStorage.getItem("@SESSION:token_expire"); | |
return time; | |
}catch(err){ | |
return null; | |
} | |
}, | |
async setTokenExpire(expire_time){ | |
return await AsyncStorage.setItem("@SESSION:token_expire", String(expire_time)); | |
}, | |
async getRefreshToken(){ | |
try { | |
let token = await AsyncStorage.getItem("@SESSION:refresh_token"); | |
return token; | |
}catch(err){ | |
return null; | |
} | |
}, | |
async setRefreshToken(token){ | |
return await AsyncStorage.setItem("@SESSION:refresh_token", token); | |
}, | |
async getDepartment(){ | |
try { | |
let department = await AsyncStorage.getItem("@SESSION:department"); | |
return department; | |
}catch(err){ | |
return null; | |
} | |
}, | |
async setDepartment(department){ | |
return await AsyncStorage.setItem("@SESSION:department", department); | |
}, | |
async hasDepartment(department){ | |
try { | |
let dept = await AsyncStorage.getItem("@SESSION:department"); | |
return department === dept; | |
}catch(err){ | |
return false; | |
} | |
}, | |
async getName(){ | |
return await AsyncStorage.getItem("@SESSION:name"); | |
}, | |
async setName(name){ | |
return await AsyncStorage.setItem("@SESSION:name", name); | |
}, | |
async getIdentity(){ | |
return await AsyncStorage.getItem("@SESSION:identity"); | |
}, | |
async setIdentity(identity){ | |
return await AsyncStorage.setItem("@SESSION:identity", identity); | |
}, | |
async getData(){ | |
let data = await AsyncStorage.getItem("@SESSION:userData"); | |
try{ | |
data = JSON.parse(data); | |
} catch (err){ | |
data = {}; | |
} | |
return data; | |
}, | |
async setData(data){ | |
if(typeof data !== "object"){ | |
data = {}; | |
} | |
return await AsyncStorage.setItem("@SESSION:userData", JSON.stringify(data)); | |
}, | |
async reset(){ | |
await AsyncStorage.setItem("@SESSION:isLogin", '0'); | |
await AsyncStorage.multiRemove(['@SESSION:token', '@SESSION:name','@SESSION:identity','@SESSION:userData']); | |
} | |
}; | |
module.exports = Auth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment