Initialize Nuxt.js project with Docker deployment setup
- Add core Nuxt.js application structure with TypeScript - Include Docker configuration and deployment guide - Set up project scaffolding with pages, composables, and middleware - Add environment configuration and Git ignore rules
This commit is contained in:
64
composables/useAuth.ts
Normal file
64
composables/useAuth.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import type { AuthState } from '~/utils/types';
|
||||
|
||||
export const useAuth = () => {
|
||||
const authState = useState<AuthState>('auth.state', () => ({
|
||||
authenticated: false,
|
||||
user: null,
|
||||
groups: [],
|
||||
}));
|
||||
|
||||
const login = () => {
|
||||
return navigateTo('/api/auth/login');
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
try {
|
||||
await $fetch('/api/auth/logout', { method: 'POST' });
|
||||
authState.value = {
|
||||
authenticated: false,
|
||||
user: null,
|
||||
groups: [],
|
||||
};
|
||||
await navigateTo('/login');
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error);
|
||||
await navigateTo('/login');
|
||||
}
|
||||
};
|
||||
|
||||
const checkAuth = async () => {
|
||||
try {
|
||||
const response = await $fetch<AuthState>('/api/auth/session');
|
||||
authState.value = response;
|
||||
return response.authenticated;
|
||||
} catch (error) {
|
||||
console.error('Auth check error:', error);
|
||||
authState.value = {
|
||||
authenticated: false,
|
||||
user: null,
|
||||
groups: [],
|
||||
};
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const isAdmin = computed(() => {
|
||||
return authState.value.groups?.includes('admin') || false;
|
||||
});
|
||||
|
||||
const hasRole = (role: string) => {
|
||||
return authState.value.groups?.includes(role) || false;
|
||||
};
|
||||
|
||||
return {
|
||||
authState: readonly(authState),
|
||||
user: computed(() => authState.value.user),
|
||||
authenticated: computed(() => authState.value.authenticated),
|
||||
groups: computed(() => authState.value.groups),
|
||||
isAdmin,
|
||||
hasRole,
|
||||
login,
|
||||
logout,
|
||||
checkAuth,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user