mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 09:55:15 +00:00
1 line
41 KiB
JSON
1 line
41 KiB
JSON
|
{"ast":null,"code":"import axios from 'axios';\nimport jwt from 'jwt-decode';\nimport { toast } from 'react-toastify';\n\nfunction getHeaders() {\n const user = JSON.parse(localStorage.getItem('user'));\n\n if (user && user.jwt) {\n var unixtime = Math.round(new Date().getTime() / 1000);\n\n if (user.exp < unixtime) {\n // Trigger refresh\n localStorage.removeItem('user');\n window.location.reload(); // toast.warning(\"Session expired. Please log in again\")\n // window.location.reload();\n\n return {};\n }\n\n return {\n Authorization: 'Bearer ' + user.jwt\n };\n } else {\n return {};\n }\n}\n\nfunction getUserUuid() {\n // TODO: move this to use Context values instead.\n const user = JSON.parse(localStorage.getItem('user'));\n\n if (user && user.uuid) {\n return user.uuid;\n } else {\n return '';\n }\n}\n\nfunction handleErrorResp(error) {\n if (error.response) {\n if (error.response.status === 401) {\n toast.error('Unauthorized');\n } else if (error.response.status === 429) {\n toast.error('Rate limited. Please try again shortly');\n } else {\n toast.error('An unknown error has occurred');\n }\n } else {\n toast.error('Failed to connect to API');\n }\n\n return {};\n}\n\nexport const PostLogin = formValues => {\n return axios.post(process.env.REACT_APP_API_URL + \"/api/v1/login\", formValues).then(response => {\n if (response.data.token) {\n let expandedUser = jwt(response.data.token);\n let user = {\n jwt: response.data.token,\n uuid: expandedUser.sub,\n exp: expandedUser.exp,\n username: expandedUser.username,\n admin: expandedUser.admin,\n mod: expandedUser.mod,\n refresh_token: expandedUser.refresh_token,\n refresh_exp: expandedUser.refresh_exp\n };\n toast.success('Successfully logged in.');\n return user;\n } else {\n toast.error(response.data.error ? response.data.error : 'An Unknown Error has occurred');\n return null;\n }\n }).catch(error => {\n if (error.response === 401) {\n toast.error('Unauthorized');\n } else if (error.response === 429) {\n toast.error('Rate limited. Please try again shortly');\n } else {\n toast.error('Failed to connect');\n }\n\n return Promise.resolve();\n });\n};\n_c = PostLogin;\nexport const PostRefreshToken = refreshToken => {\n return axios.post(process.env.REACT_APP_API_URL + \"/api/v1/refresh\", {\n token: refreshToken\n }).then(response => {\n if (response.data.token) {\n let expandedUser = jwt(response.data.token);\n let user = {\n jwt: response.data.token,\n uuid: expandedUser.sub,\n exp: expandedUser.exp,\n username: expandedUser.username,\n admin: expandedUser.admin,\n mod: expandedUser.mod,\n refresh_token: expandedUser.refresh_token,\n refresh_exp: expandedUser.refresh_exp\n };\n return user;\n } else {\n toast.error(response.data.error ? response.data.error : 'An Unknown Error has occurred');\n return null;\n }\n }).catch(error => {\n if (error.response === 401) {\n toast.error('Unauthorized');\n } else if (error.response === 429) {\n toast.error('Rate limited. Please try again shortly');\n } else {\n toast.error('Failed to connect');\n }\n\n return Promise.resolve();\n });\n};\n_c2 = PostRefreshToken;\nexport const PostRegister = formValues => {\n return axios.post(process.env.REACT_APP_API_URL + \"/api/v1/register\", formValues).then(response => {\n if (response.data.message) {\n toast.success(response.data.message);\n return Promise.resolve();\n } else {\n toast.error(response.data.error ? response.data.error : 'An Unknown Error has occurred');\n return Promise.resolve();\n }\n }).catch(error => {\n handleErrorResp(error);\n return Promise.resolve();\n });\n};\n_c3 = PostRegister;\nexport const PostResetPassword = formValues => {\n return axios.post(process.env.REACT_APP_A
|