implemented authentication with sidebase/nuxt-auth
This commit is contained in:
@ -1,63 +0,0 @@
|
||||
import { useAuthStore } from '~/store/auth';
|
||||
|
||||
|
||||
export default defineEventHandler (async (event) => {
|
||||
// Check if user is not authenticated and trying to access a page other than /login
|
||||
let isAuthenticated = false;
|
||||
|
||||
if (event.path !== '/login' && event.path !== '/api/login') {
|
||||
|
||||
//const reqUsername = getHeader(event, "Authorization");
|
||||
//const reqCookie = getHeader(event, "Cookie");
|
||||
const reqUsername = getCookie(event, 'user');
|
||||
const reqToken = getCookie(event, 'token');
|
||||
//console.log('getHeader: ', reqCookie);
|
||||
console.log('getCookie user: ', reqUsername);
|
||||
console.log('getCookie token: ', reqToken);
|
||||
|
||||
const auth = useAuthStore();
|
||||
const authUsername = auth.username;
|
||||
const authToken = auth.token;
|
||||
|
||||
console.log('auth user: ', authUsername);
|
||||
console.log('auth token: ', authToken);
|
||||
|
||||
if (authUsername == reqUsername && authToken == reqToken) {
|
||||
isAuthenticated = true;
|
||||
}
|
||||
|
||||
|
||||
if (!isAuthenticated) {
|
||||
await sendRedirect(event, '/login');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/*function isAuthenticated(event) {
|
||||
// Implement authentication logic
|
||||
/*const auth = useAuthStore();
|
||||
const authUsername = auth.username;
|
||||
const authToken = auth.token;
|
||||
|
||||
const reqUsername = getHeader(event, 'Authorization');
|
||||
const reqCookie = getHeader(event, 'Cookie');
|
||||
const reqToken = useCookie('token');
|
||||
console.log('getHeader: ', reqCookie);
|
||||
console.log('useCookie: ', reqToken);
|
||||
|
||||
if (authUsername.equals(reqUsername) ) {
|
||||
return true;
|
||||
}*/
|
||||
|
||||
/*return false;
|
||||
// Return true if authenticated, false otherwise
|
||||
}*/
|
||||
|
||||
|
||||
/*export default defineNuxtRouteMiddleware((to) => {
|
||||
const auth = useAuthStore();
|
||||
const authUsername = auth.username;
|
||||
const authToken = auth.token;
|
||||
|
||||
const reqUsername =
|
||||
})*/
|
||||
@ -1,61 +0,0 @@
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import serversideConfig from '../../serversideConfig';
|
||||
import https from 'https';
|
||||
import { useAuthStore } from '~/store/auth';
|
||||
|
||||
let loginSuccessful = false;
|
||||
let sessionToken = 'token';
|
||||
let errorMsg = '';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
loginSuccessful = false;
|
||||
const agent = new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: "*",
|
||||
},
|
||||
httpsAgent: agent
|
||||
});
|
||||
|
||||
if (event.path.startsWith("/api/login")) {
|
||||
|
||||
const body = await readBody(event)
|
||||
|
||||
// get user object from backend
|
||||
try {
|
||||
let res = await axiosInstance.post(`https://${serversideConfig.url}:${serversideConfig.port}/login`, {
|
||||
username: body.username,
|
||||
password: body.password,
|
||||
});
|
||||
sessionToken = res.data.token;
|
||||
const auth = useAuthStore();
|
||||
auth.createNewSession(res.data.user, sessionToken);
|
||||
loginSuccessful = true;
|
||||
} catch (err) {
|
||||
if (axios.isAxiosError(err)) {
|
||||
const axiosError = err as AxiosError;
|
||||
|
||||
if (axiosError.response) {
|
||||
// Axios error
|
||||
console.error(axiosError.response.data.message);
|
||||
errorMsg = axiosError.response.data.message;
|
||||
} else if (axiosError.request) {
|
||||
// If error was caused by the request
|
||||
console.error(axiosError.request);
|
||||
} else {
|
||||
// Other errors
|
||||
console.error('Error', axiosError.message);
|
||||
}
|
||||
} else {
|
||||
// No AxiosError
|
||||
console.error('Error', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export { loginSuccessful, sessionToken, errorMsg };
|
||||
Reference in New Issue
Block a user