Implement authentication phase with Cidaas OAuth2 integration
- Add authentication middleware to protect routes - Create API endpoints for login, logout, registration, and user info - Develop UI components for login and registration forms - Integrate VeeValidate for form validation - Update environment configuration for Cidaas settings - Add i18n support for English and German languages - Enhance Tailwind CSS for improved styling of auth components - Document authentication flow and testing procedures
This commit is contained in:
91
app/composables/useAuth.ts
Normal file
91
app/composables/useAuth.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
// composables/useAuth.ts
|
||||
|
||||
/**
|
||||
* Authentication composable
|
||||
*
|
||||
* Wrapper around nuxt-auth-utils useUserSession() with convenience methods
|
||||
*
|
||||
* Usage:
|
||||
* const { user, loggedIn, login, logout } = useAuth()
|
||||
*/
|
||||
|
||||
export function useAuth() {
|
||||
const { loggedIn, user, clear, fetch } = useUserSession()
|
||||
|
||||
/**
|
||||
* Login with email
|
||||
* Initiates OAuth2 flow
|
||||
*/
|
||||
async function login(email: string) {
|
||||
try {
|
||||
// Call login endpoint to get redirect URL
|
||||
const { redirectUrl } = await $fetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: { email },
|
||||
})
|
||||
|
||||
// Redirect to Cidaas
|
||||
navigateTo(redirectUrl, { external: true })
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register new user
|
||||
*/
|
||||
async function register(data: {
|
||||
email: string
|
||||
password: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
}) {
|
||||
try {
|
||||
const result = await $fetch('/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
})
|
||||
|
||||
return result
|
||||
} catch (error) {
|
||||
console.error('Registration failed:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout
|
||||
* Clears session and redirects to homepage
|
||||
*/
|
||||
async function logout() {
|
||||
try {
|
||||
await $fetch('/api/auth/logout', { method: 'POST' })
|
||||
await clear() // Clear client-side state
|
||||
navigateTo('/') // Redirect to homepage
|
||||
} catch (error) {
|
||||
console.error('Logout failed:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh user data from server
|
||||
*/
|
||||
async function refreshUser() {
|
||||
try {
|
||||
await fetch() // Re-fetch session from server
|
||||
} catch (error) {
|
||||
console.error('Refresh user failed:', error)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
loggedIn,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
refreshUser,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user