diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-11-04 10:53:59 +0700 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-11-04 10:53:59 +0700 |
| commit | e93103fa78d610737e56b1b99056acdd84a9adde (patch) | |
| tree | c807a25ee1c9a5e2aaa915d562074e22e04cda04 /src/helpers/apiOdoo.js | |
| parent | e4b035a1471bd70872bb12012442367933fc8852 (diff) | |
Auth with token method
Diffstat (limited to 'src/helpers/apiOdoo.js')
| -rw-r--r-- | src/helpers/apiOdoo.js | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/helpers/apiOdoo.js b/src/helpers/apiOdoo.js index 559c31c3..aff27bc3 100644 --- a/src/helpers/apiOdoo.js +++ b/src/helpers/apiOdoo.js @@ -1,14 +1,31 @@ +import { getCookie, setCookie } from 'cookies-next'; + const axios = require('axios'); +const renewToken = async () => { + let res = await axios.get(process.env.DB_HOST + '/api/token'); + setCookie('token', res.data.result); + return res.data.result; +}; + +const getToken = async () => { + let token = getCookie('token'); + if (token == undefined) token = await renewToken(); + return token; +}; + const getOdoo = async (url) => { try { - let res = await axios.get(process.env.DB_HOST + url); + let token = await getToken(); + let res = await axios.get(process.env.DB_HOST + url, {headers: {Authorization: token}}); + if (res.data.status.code == 401) { + await renewToken(); + return getOdoo(url); + } return res.data.result || []; } catch (error) { console.log(error); } } -export { - getOdoo, -};
\ No newline at end of file +export { getOdoo };
\ No newline at end of file |
