-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added axiosInstance which appends VITE_API_URL to requests. this is b…
…ecause proxying only works in dev
- Loading branch information
OomsOoms
committed
Oct 2, 2024
1 parent
ea4ca29
commit 20926ad
Showing
10 changed files
with
175 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
API_DOMAIN=http://localhost:8000 | ||
VITE_API_URL=http://localhost:8000 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
28 changes: 17 additions & 11 deletions
28
client/src/features/authentication/services/loginService.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import axiosInstance from "../../../utils/axios-instance"; | ||
|
||
export const login = async (username, password) => { | ||
try { | ||
const response = await fetch('/api/auth/login', { | ||
method: 'POST', | ||
const response = await axiosInstance.post('/api/auth/login', { | ||
username, | ||
password | ||
}, { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username, password }), | ||
credentials: 'include' // Include credentials (cookies) | ||
withCredentials: true | ||
}); | ||
|
||
if (response.ok) { | ||
console.log(response.status); | ||
if (response.status === 200) { | ||
window.location.href = '/'; | ||
} else { | ||
const data = await response.json(); | ||
return data; | ||
return response.data; | ||
} | ||
} catch (error) { | ||
console.error('There was a problem with the fetch operation:', error); | ||
throw error; | ||
console.error('There was a problem with the axios operation:', error); | ||
if (error.response) { | ||
return error.response.data; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; | ||
}; |
28 changes: 18 additions & 10 deletions
28
client/src/features/authentication/services/registerService.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import axiosInstance from "../../../utils/axios-instance"; | ||
|
||
export const register = async (username, email, password, hCaptchaToken) => { | ||
try { | ||
const response = await fetch('/api/users', { | ||
method: 'POST', | ||
const response = await axiosInstance.post('/api/users', { | ||
username, | ||
email, | ||
password, | ||
hCaptchaToken | ||
}, { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username, email, password, hCaptchaToken }), | ||
credentials: 'include' // Include credentials (cookies) | ||
withCredentials: true | ||
}); | ||
|
||
if (response.ok) { | ||
return { ok: true, message: 'Registration successful' }; | ||
if (response.status === 201) { | ||
window.location.href = '/'; | ||
} else { | ||
const data = await response.json(); | ||
return { ok: false, errors: data.errors }; | ||
return response.data; | ||
} | ||
} catch (error) { | ||
console.error('There was a problem with the fetch operation:', error); | ||
return { ok: false, errors: [{ path: 'server', msg: 'Server error' }] }; | ||
console.error('There was a problem with the axios operation:', error); | ||
if (error.response) { | ||
return { ok: false, errors: error.response.data.errors }; | ||
} else { | ||
return { ok: false, errors: [{ path: 'server', msg: 'Server error' }] }; | ||
} | ||
} | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Proxying only works in dev so i need to do this | ||
import axios from 'axios'; | ||
|
||
const axiosInstance = axios.create({ baseURL: import.meta.env.VITE_API_URL }); | ||
axiosInstance.interceptors.request.use( | ||
config => { | ||
console.log(config); | ||
return config; | ||
}, | ||
error => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default axiosInstance; |
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
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