authentication implementation
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const { public: { apiBase } } = useRuntimeConfig();
|
||||
try {
|
||||
const data = await $fetch(`${apiBase}/auth/login-step-two`, {
|
||||
method: 'POST',
|
||||
body,
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
});
|
||||
setCookie(event, 'token', data.token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
maxAge: 60 * 60 * 24 * 7, // 1 week
|
||||
path: '/'
|
||||
})
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.log('server err :', err);
|
||||
return err
|
||||
}
|
||||
|
||||
})
|
||||
@@ -0,0 +1,18 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const { public: { apiBase } } = useRuntimeConfig();
|
||||
try {
|
||||
const data = await $fetch(`${apiBase}/auth/login`, {
|
||||
method: 'POST',
|
||||
body,
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
});
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.log(err.data);
|
||||
return err
|
||||
}
|
||||
|
||||
})
|
||||
@@ -0,0 +1,12 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const token = getCookie(event, 'token')
|
||||
|
||||
|
||||
setCookie(event, 'token', '', {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
maxAge: new Date(0),
|
||||
path: '/'
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,29 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
|
||||
const { public: { apiBase } } = useRuntimeConfig();
|
||||
const token = getCookie(event, 'token');
|
||||
|
||||
try {
|
||||
const data = await $fetch(`${apiBase}/user`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
return data;
|
||||
|
||||
} catch (error) {
|
||||
// if (error.statusCode == 401) {
|
||||
// setCookie(event, 'token', '', {
|
||||
// httpOnly: true,
|
||||
// secure: true,
|
||||
// maxAge: new Date(0),
|
||||
// path: '/'
|
||||
// })
|
||||
// }
|
||||
console.log(error);
|
||||
return error;
|
||||
}
|
||||
|
||||
})
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"extends": "../.nuxt/tsconfig.server.json"
|
||||
}
|
||||
Reference in New Issue
Block a user