mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
new selfhosted version
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
import type { AuthContext } from "~/server/middleware/01-authorization";
|
||||
|
||||
async function executeUserLogin(token: string): Promise<[true, AuthContext] | [false, null]> {
|
||||
const user = await $fetch<AuthContext>('/api/user/me', { headers: { 'Authorization': 'Bearer ' + token } });
|
||||
if (!user) return [false, null];
|
||||
if (user.logged == false) return [false, null];
|
||||
return [true, user];
|
||||
}
|
||||
|
||||
async function handleUserLogin(authContext?: AuthContext) {
|
||||
|
||||
if (authContext) return;
|
||||
|
||||
const { token, setToken } = useAccessToken();
|
||||
|
||||
if (!token.value || token.value.length == 0) {
|
||||
setToken(''); // LOGOUT
|
||||
return;
|
||||
}
|
||||
|
||||
const [ok, newContext] = await executeUserLogin(token.value);
|
||||
|
||||
if (!ok) {
|
||||
setToken(''); // LOGOUT
|
||||
return;
|
||||
}
|
||||
|
||||
const { setLoggedUser } = useLoggedUser();
|
||||
setLoggedUser(newContext);
|
||||
|
||||
}
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||
|
||||
if (!to.name) return;
|
||||
|
||||
const { user } = useLoggedUser();
|
||||
|
||||
await handleUserLogin(user.value);
|
||||
|
||||
if (user.value?.logged) {
|
||||
if (to.path == '/login' || to.path == '/register') return '/';
|
||||
} else {
|
||||
if (
|
||||
to.path != '/login' &&
|
||||
to.path != '/register' &&
|
||||
to.path != '/live_demo' &&
|
||||
to.path != '/jwt_login' &&
|
||||
to.path != '/forgot_password'
|
||||
) {
|
||||
console.log('REDIRECT TO LOGIN', to.path);
|
||||
return '/login';
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
46
dashboard/middleware/auth.global.ts
Normal file
46
dashboard/middleware/auth.global.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
|
||||
const allowedAnonRoutes = [
|
||||
'/login',
|
||||
'/register',
|
||||
'/live_demo',
|
||||
'/jwt_login',
|
||||
'/forgot_password',
|
||||
'/reset_password'
|
||||
]
|
||||
|
||||
// const FREE_TRIAL_ENDED = 7999;
|
||||
|
||||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||
const { loggedIn } = useUserSession();
|
||||
|
||||
if (to.path.includes('/shared/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: SELFHOST
|
||||
|
||||
if (loggedIn.value === true) {
|
||||
// const plan = await useAuthFetchSync('/api/user/plan');
|
||||
|
||||
// if (to.path != '/free_trial_ended' && plan.premium_type === FREE_TRIAL_ENDED ) {
|
||||
// return '/free_trial_ended'
|
||||
// }
|
||||
|
||||
// if (to.path == '/free_trial_ended' && plan.premium_type !== FREE_TRIAL_ENDED) {
|
||||
// return '/'
|
||||
// }
|
||||
|
||||
if (to.path == '/login' || to.path == '/register' || to.path == '/forgot_password') return '/';
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
if (!allowedAnonRoutes.includes(to.path)) {
|
||||
return '/login';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user