feat : jwt upload

This commit is contained in:
leejunghyeok 2024-05-22 15:50:15 +09:00
parent 5f34cc0cff
commit 3e10fc726f
3 changed files with 34 additions and 14 deletions

View File

@ -12,10 +12,12 @@ async function findByToken(userid,token, success, fail) {
await local.get(`/member/info/${userid}`).then(success).catch(fail);
}
async function tokenRegeneration(user, success, fail) {
async function tokenRegeneration(userid,token, success, fail) {
// local.defaults.headers["refreshToken"] = sessionStorage.getItem("refreshToken"); //axios header에 refresh-token 셋팅
local.defaults.headers["refreshToken"] = localStorage.getItem("refreshToken");
await local.post(`/member/refresh`, user).then(success).catch(fail);
local.defaults.headers["refreshToken"] = token
console.log(userid)
console.log(token)
await local.post(`/member/refresh/${userid}`).then(success).catch(fail);
}
async function logout(userid, success, fail) {

View File

@ -7,17 +7,24 @@ import { useMemberStore } from '@/stores/memberStore';
const onlyAuthUser = async (to, from, next) => {
const memberStore = useMemberStore();
const { userInfo, isValidToken } = storeToRefs(memberStore);
const { getUserInfo } = memberStore;
const { userInfo, isLogin } = storeToRefs(memberStore);
const { isValidToken } = memberStore;
let token = sessionStorage.getItem('accessToken');
if (userInfo.value != null && token) {
await getUserInfo(token);
let token = localStorage.getItem("accessToken");
console.log(userInfo)
console.log(isLogin)
// if (userInfo.value != null && token) {
console.log(isLogin.value)
console.log(token)
if (isLogin.value != null && token) {
console.log("이프문들어옴")
await isValidToken(token);
}
if (!isValidToken.value || userInfo.value === null) {
next({ name: 'user-login' });
if (isLogin.value === null) {
console.log("이프문2들어옴")
next({ name: "user-login" });
} else {
console.log("else문 들어옴")
next();
}
};
@ -90,6 +97,10 @@ const router = createRouter({
},
],
},
{
path: '/test',
component: () => import('@/views/TestView.vue'),
},
],
});

View File

@ -30,9 +30,11 @@ export const useMemberStore = defineStore("memberStore", () => {
}
const isValidToken = async (token) => {
const {id} = jwtDecode(token);
findByToken(id,token, () => { }, (error) => {
const { userId } = jwtDecode(token);
console.log(userId,token)
findByToken(userId,token, () => { }, (error) => {
console.log(error);
console.log("tokenRegenerate")
tokenRegenerate();
})
@ -40,13 +42,18 @@ export const useMemberStore = defineStore("memberStore", () => {
const tokenRegenerate = async () => {
const { userId } = jwtDecode(accessToken.value)
const refreshToken = localStorage.getItem('refreshToken');
console.log(userId)
console.log(refreshToken)
await tokenRegeneration(
refreshToken,
userId,refreshToken,
(response) => {
if (response.status === httpStatusCode.CREATE) {
const newAccessToken = response.data["access-token"]
const newRefreshToken = response.data["refresh-token"]
localStorage.setItem("accessToken", newAccessToken)
localStorage.setItem("refreshToken", newRefreshToken)
accessToken.value = newAccessToken;
}
},