Created
April 22, 2020 11:39
-
-
Save shibbirweb/a2a35c1b576e8778ab90d2b9995aef63 to your computer and use it in GitHub Desktop.
Smart way to use API
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 axios from 'axios'; | |
| import store from "../store" | |
| // api version 1 | |
| const baseAPI = axios.create({ | |
| baseURL: `http://yoursite.test/api/v1` | |
| }); | |
| // configure api | |
| let api = () => { | |
| // get token | |
| const token = store.getters["auth/token"]; | |
| // if has token | |
| if (token) { | |
| // set token in authorization | |
| baseAPI.defaults.headers.common['Authorization'] = `Bearer ${token}` | |
| } | |
| return baseAPI; | |
| } | |
| export default api; |
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 api from "../api"; | |
| // register | |
| export const register = (data) => { | |
| return api().post('register', data) | |
| } | |
| // email verify | |
| export const verifyEmail = (url) => { | |
| return api().post(url,{}, { | |
| 'baseURL': '' | |
| }) | |
| } | |
| // login | |
| export const login = (data) => { | |
| return api().post('login', data) | |
| } | |
| // logout | |
| export const logout = () => { | |
| return api().post('logout') | |
| } | |
| // send password reset email | |
| export const sendPasswordResetEmail = (email) => { | |
| return api().post('password/email', {email}) | |
| } | |
| // reset password | |
| export const passwordReset = (data) => { | |
| return api().post('password/reset', data) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment