-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend auth use JWT instead of cookie
- Loading branch information
1 parent
800e423
commit 31c3a60
Showing
4 changed files
with
97 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useAuthStore } from '@/stores/auth'; | ||
|
||
export const fetchWrapper = { | ||
get: request('GET'), | ||
post: request('POST'), | ||
put: request('PUT'), | ||
delete: request('DELETE') | ||
}; | ||
|
||
function request(method) { | ||
return (url, body) => { | ||
const requestOptions = { | ||
method, | ||
headers: authHeader(url) | ||
}; | ||
if (body) { | ||
requestOptions.headers['Content-Type'] = 'application/json'; | ||
requestOptions.body = JSON.stringify(body); | ||
} | ||
console.log("url", url, "optoins", requestOptions) | ||
return fetch(url, requestOptions).then(handleResponse); | ||
} | ||
} | ||
|
||
// helper functions | ||
|
||
function authHeader(url) { | ||
// return auth header with jwt if user is logged in and request is to the api url | ||
const { token } = useAuthStore(); | ||
const isLoggedIn = !!token?.token; | ||
const isApiUrl = url.startsWith(import.meta.env.VITE_APP_API_URL); | ||
alert(`Isapi:${isApiUrl} isloggedin:${isLoggedIn}`) | ||
if (isLoggedIn && isApiUrl) { | ||
return { Authorization: `Bearer ${token.token}` }; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
function handleResponse(response) { | ||
return response.text().then(text => { | ||
const data = text && JSON.parse(text); | ||
|
||
if (!response.ok) { | ||
// const { user, logout } = useAuthStore(); | ||
// if ([401, 403].includes(response.status) && user) { | ||
// // auto logout if 401 Unauthorized or 403 Forbidden response returned from api | ||
// logout(); | ||
// } | ||
|
||
const error = (data && data.message) || response.statusText; | ||
return Promise.reject(error); | ||
} | ||
|
||
return data; | ||
}); | ||
} |
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
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