Compare commits
9 Commits
mergeauth
...
353da0039f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
353da0039f | ||
|
|
ddd25c47c5 | ||
|
|
b0a45d98ce | ||
|
|
b6cb5652b7 | ||
|
|
fbfaf7bb9f | ||
|
|
2f0aeb948b | ||
|
|
0ad62cbf8d | ||
|
|
5a80f33078 | ||
|
|
57f27d28cd |
1
.env.api
1
.env.api
@@ -2,7 +2,6 @@ NODE_ENV=development
|
||||
PORT=3000
|
||||
|
||||
DATABASE_URL="mysql://platform:platform@db:3306/platform"
|
||||
CENTRAL_DATABASE_URL="mysql://root:asjdnfqTash37faggT@db:3306/central_platform"
|
||||
REDIS_URL="redis://redis:6379"
|
||||
|
||||
# JWT, multi-tenant hints, etc.
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `users` (
|
||||
`id` VARCHAR(191) NOT NULL,
|
||||
`email` VARCHAR(191) NOT NULL,
|
||||
`password` VARCHAR(191) NOT NULL,
|
||||
`firstName` VARCHAR(191) NULL,
|
||||
`lastName` VARCHAR(191) NULL,
|
||||
`role` VARCHAR(191) NOT NULL DEFAULT 'admin',
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `users_email_key`(`email`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
@@ -8,20 +8,6 @@ datasource db {
|
||||
url = env("CENTRAL_DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
email String @unique
|
||||
password String
|
||||
firstName String?
|
||||
lastName String?
|
||||
role String @default("admin") // admin, superadmin
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model Tenant {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
// Central database client
|
||||
const centralPrisma = new CentralPrismaClient();
|
||||
|
||||
async function createAdminUser() {
|
||||
const email = 'admin@example.com';
|
||||
const password = 'admin123';
|
||||
const firstName = 'Admin';
|
||||
const lastName = 'User';
|
||||
|
||||
try {
|
||||
// Check if admin user already exists
|
||||
const existingUser = await centralPrisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
console.log(`User ${email} already exists`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Create admin user in central database
|
||||
const user = await centralPrisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
role: 'superadmin',
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('\nAdmin user created successfully!');
|
||||
console.log('Email:', email);
|
||||
console.log('Password:', password);
|
||||
console.log('User ID:', user.id);
|
||||
} catch (error) {
|
||||
console.error('Error creating admin user:', error);
|
||||
} finally {
|
||||
await centralPrisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
createAdminUser();
|
||||
@@ -1,138 +0,0 @@
|
||||
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { Knex, knex } from 'knex';
|
||||
|
||||
// Central database client
|
||||
const centralPrisma = new CentralPrismaClient();
|
||||
|
||||
async function createTenantUser() {
|
||||
const tenantSlug = 'tenant1';
|
||||
const email = 'user@example.com';
|
||||
const password = 'user123';
|
||||
const firstName = 'Test';
|
||||
const lastName = 'User';
|
||||
|
||||
try {
|
||||
// Get tenant database connection info
|
||||
const tenant = await centralPrisma.tenant.findFirst({
|
||||
where: { slug: tenantSlug },
|
||||
});
|
||||
|
||||
if (!tenant) {
|
||||
console.log(`Tenant ${tenantSlug} not found. Creating tenant...`);
|
||||
|
||||
// Create tenant in central database
|
||||
const newTenant = await centralPrisma.tenant.create({
|
||||
data: {
|
||||
name: 'Default Tenant',
|
||||
slug: tenantSlug,
|
||||
dbHost: 'db',
|
||||
dbPort: 3306,
|
||||
dbName: 'platform',
|
||||
dbUsername: 'platform',
|
||||
dbPassword: 'platform',
|
||||
status: 'active',
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Tenant created:', newTenant.slug);
|
||||
} else {
|
||||
console.log('Tenant found:', tenant.slug);
|
||||
}
|
||||
|
||||
const tenantInfo = tenant || {
|
||||
dbHost: 'db',
|
||||
dbPort: 3306,
|
||||
dbName: 'platform',
|
||||
dbUsername: 'platform',
|
||||
dbPassword: 'platform',
|
||||
};
|
||||
|
||||
// Connect to tenant database (using root for now since tenant password is encrypted)
|
||||
const tenantDb: Knex = knex({
|
||||
client: 'mysql2',
|
||||
connection: {
|
||||
host: tenantInfo.dbHost,
|
||||
port: tenantInfo.dbPort,
|
||||
database: tenantInfo.dbName,
|
||||
user: 'root',
|
||||
password: 'asjdnfqTash37faggT',
|
||||
},
|
||||
});
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await tenantDb('users')
|
||||
.where({ email })
|
||||
.first();
|
||||
|
||||
if (existingUser) {
|
||||
console.log(`User ${email} already exists in tenant ${tenantSlug}`);
|
||||
await tenantDb.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Create user
|
||||
await tenantDb('users').insert({
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
isActive: true,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
console.log(`\nUser created successfully in tenant ${tenantSlug}!`);
|
||||
console.log('Email:', email);
|
||||
console.log('Password:', password);
|
||||
|
||||
// Create admin role if it doesn't exist
|
||||
let adminRole = await tenantDb('roles')
|
||||
.where({ name: 'admin' })
|
||||
.first();
|
||||
|
||||
if (!adminRole) {
|
||||
await tenantDb('roles').insert({
|
||||
name: 'admin',
|
||||
guardName: 'api',
|
||||
description: 'Administrator role with full access',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
adminRole = await tenantDb('roles')
|
||||
.where({ name: 'admin' })
|
||||
.first();
|
||||
|
||||
console.log('Admin role created');
|
||||
}
|
||||
|
||||
// Get the created user
|
||||
const user = await tenantDb('users')
|
||||
.where({ email })
|
||||
.first();
|
||||
|
||||
// Assign admin role to user
|
||||
if (adminRole && user) {
|
||||
await tenantDb('user_roles').insert({
|
||||
userId: user.id,
|
||||
roleId: adminRole.id,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
});
|
||||
|
||||
console.log('Admin role assigned to user');
|
||||
}
|
||||
|
||||
await tenantDb.destroy();
|
||||
} catch (error) {
|
||||
console.error('Error creating tenant user:', error);
|
||||
} finally {
|
||||
await centralPrisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
createTenantUser();
|
||||
@@ -12,7 +12,7 @@ import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { TenantId } from '../tenant/tenant.decorator';
|
||||
|
||||
@Controller('setup/apps')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
//@UseGuards(JwtAuthGuard)
|
||||
export class SetupAppController {
|
||||
constructor(private appBuilderService: AppBuilderService) {}
|
||||
|
||||
|
||||
@@ -42,13 +42,8 @@ export class AuthController {
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('login')
|
||||
async login(@TenantId() tenantId: string, @Body() loginDto: LoginDto) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
}
|
||||
|
||||
async login(@Body() loginDto: LoginDto) {
|
||||
const user = await this.authService.validateUser(
|
||||
tenantId,
|
||||
loginDto.email,
|
||||
loginDto.password,
|
||||
);
|
||||
@@ -62,15 +57,9 @@ export class AuthController {
|
||||
|
||||
@Post('register')
|
||||
async register(
|
||||
@TenantId() tenantId: string,
|
||||
@Body() registerDto: RegisterDto,
|
||||
) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
}
|
||||
|
||||
const user = await this.authService.register(
|
||||
tenantId,
|
||||
registerDto.email,
|
||||
registerDto.password,
|
||||
registerDto.firstName,
|
||||
@@ -79,12 +68,4 @@ export class AuthController {
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('logout')
|
||||
async logout() {
|
||||
// For stateless JWT, logout is handled on client-side
|
||||
// This endpoint exists for consistency and potential future enhancements
|
||||
return { message: 'Logged out successfully' };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ import { ConfigService } from '@nestjs/config';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { JwtStrategy } from './jwt.strategy';
|
||||
import { TenantModule } from '../tenant/tenant.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PassportModule,
|
||||
TenantModule,
|
||||
JwtModule.registerAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { TenantDatabaseService } from '../tenant/tenant-database.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private tenantDbService: TenantDatabaseService,
|
||||
private prisma: PrismaService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async validateUser(
|
||||
tenantId: string,
|
||||
email: string,
|
||||
password: string,
|
||||
): Promise<any> {
|
||||
const tenantDb = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
include: {
|
||||
userRoles: {
|
||||
include: {
|
||||
role: {
|
||||
include: {
|
||||
rolePermissions: {
|
||||
include: {
|
||||
permission: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const user = await tenantDb('users')
|
||||
.where({ email })
|
||||
.first();
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (await bcrypt.compare(password, user.password)) {
|
||||
// Load user roles and permissions
|
||||
const userRoles = await tenantDb('user_roles')
|
||||
.where({ userId: user.id })
|
||||
.join('roles', 'user_roles.roleId', 'roles.id')
|
||||
.select('roles.*');
|
||||
|
||||
const { password: _, ...result } = user;
|
||||
return {
|
||||
...result,
|
||||
tenantId,
|
||||
userRoles,
|
||||
};
|
||||
if (user && (await bcrypt.compare(password, user.password))) {
|
||||
const { password, ...result } = user;
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -61,30 +61,22 @@ export class AuthService {
|
||||
}
|
||||
|
||||
async register(
|
||||
tenantId: string,
|
||||
email: string,
|
||||
password: string,
|
||||
firstName?: string,
|
||||
lastName?: string,
|
||||
) {
|
||||
const tenantDb = await this.tenantDbService.getTenantKnex(tenantId);
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
const [userId] = await tenantDb('users').insert({
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
isActive: true,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
const user = await this.prisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
password: hashedPassword,
|
||||
firstName,
|
||||
lastName,
|
||||
},
|
||||
});
|
||||
|
||||
const user = await tenantDb('users')
|
||||
.where({ id: userId })
|
||||
.first();
|
||||
|
||||
const { password: _, ...result } = user;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -8,30 +8,22 @@ export class TenantDatabaseService {
|
||||
private readonly logger = new Logger(TenantDatabaseService.name);
|
||||
private tenantConnections: Map<string, Knex> = new Map();
|
||||
|
||||
async getTenantKnex(tenantIdOrSlug: string): Promise<Knex> {
|
||||
if (this.tenantConnections.has(tenantIdOrSlug)) {
|
||||
return this.tenantConnections.get(tenantIdOrSlug);
|
||||
async getTenantKnex(tenantId: string): Promise<Knex> {
|
||||
if (this.tenantConnections.has(tenantId)) {
|
||||
return this.tenantConnections.get(tenantId);
|
||||
}
|
||||
|
||||
const centralPrisma = getCentralPrisma();
|
||||
|
||||
// Try to find tenant by ID first, then by slug
|
||||
let tenant = await centralPrisma.tenant.findUnique({
|
||||
where: { id: tenantIdOrSlug },
|
||||
const tenant = await centralPrisma.tenant.findUnique({
|
||||
where: { id: tenantId },
|
||||
});
|
||||
|
||||
if (!tenant) {
|
||||
tenant = await centralPrisma.tenant.findUnique({
|
||||
where: { slug: tenantIdOrSlug },
|
||||
});
|
||||
}
|
||||
|
||||
if (!tenant) {
|
||||
throw new Error(`Tenant ${tenantIdOrSlug} not found`);
|
||||
throw new Error(`Tenant ${tenantId} not found`);
|
||||
}
|
||||
|
||||
if (tenant.status !== 'active') {
|
||||
throw new Error(`Tenant ${tenantIdOrSlug} is not active`);
|
||||
throw new Error(`Tenant ${tenantId} is not active`);
|
||||
}
|
||||
|
||||
// Decrypt password
|
||||
@@ -64,7 +56,7 @@ export class TenantDatabaseService {
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.tenantConnections.set(tenantIdOrSlug, tenantKnex);
|
||||
this.tenantConnections.set(tenantId, tenantKnex);
|
||||
return tenantKnex;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,53 +19,29 @@ export class TenantMiddleware implements NestMiddleware {
|
||||
const hostname = host.split(':')[0]; // Remove port if present
|
||||
const parts = hostname.split('.');
|
||||
|
||||
this.logger.log(`Host header: ${host}, hostname: ${hostname}, parts: ${JSON.stringify(parts)}`);
|
||||
|
||||
// For local development, accept x-tenant-id header
|
||||
// For local development, accept x-tenant-id header as fallback
|
||||
let tenantId = req.headers['x-tenant-id'] as string;
|
||||
let subdomain: string | null = null;
|
||||
|
||||
this.logger.log(`Host header: ${host}, hostname: ${hostname}, parts: ${JSON.stringify(parts)}, x-tenant-id: ${tenantId}`);
|
||||
|
||||
// If x-tenant-id is explicitly provided, use it directly
|
||||
if (tenantId) {
|
||||
this.logger.log(`Using explicit x-tenant-id: ${tenantId}`);
|
||||
(req as any).tenantId = tenantId;
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract subdomain (e.g., "tenant1" from "tenant1.routebox.co")
|
||||
// For production domains with 3+ parts, extract first part as subdomain
|
||||
if (parts.length >= 3) {
|
||||
// Extract subdomain (e.g., "acme" from "acme.routebox.co")
|
||||
if (parts.length > 2) {
|
||||
subdomain = parts[0];
|
||||
// Ignore www subdomain
|
||||
if (subdomain === 'www') {
|
||||
subdomain = null;
|
||||
}
|
||||
}
|
||||
// For development (e.g., tenant1.localhost), also check 2 parts
|
||||
else if (parts.length === 2 && parts[1] === 'localhost') {
|
||||
subdomain = parts[0];
|
||||
}
|
||||
|
||||
this.logger.log(`Extracted subdomain: ${subdomain}`);
|
||||
|
||||
// Get tenant by subdomain if available
|
||||
if (subdomain) {
|
||||
try {
|
||||
const tenant = await this.tenantDbService.getTenantByDomain(subdomain);
|
||||
if (tenant) {
|
||||
tenantId = tenant.id;
|
||||
this.logger.log(
|
||||
`Tenant identified: ${tenant.name} (${tenant.id}) from subdomain: ${subdomain}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn(`No tenant found for subdomain: ${subdomain}`, error.message);
|
||||
// Fall back to using subdomain as tenantId directly if domain lookup fails
|
||||
tenantId = subdomain;
|
||||
this.logger.log(`Using subdomain as tenantId fallback: ${tenantId}`);
|
||||
const tenant = await this.tenantDbService.getTenantByDomain(subdomain);
|
||||
if (tenant) {
|
||||
tenantId = tenant.id;
|
||||
this.logger.log(
|
||||
`Tenant identified: ${tenant.name} (${tenant.id}) from subdomain: ${subdomain}`,
|
||||
);
|
||||
} else {
|
||||
this.logger.warn(`No tenant found for subdomain: ${subdomain}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { Toaster } from 'vue-sonner'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<Toaster position="top-right" :duration="4000" richColors />
|
||||
<NuxtPage />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -16,13 +16,7 @@ import {
|
||||
SidebarRail,
|
||||
} from '@/components/ui/sidebar'
|
||||
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'
|
||||
import { LayoutGrid, Boxes, Settings, Home, ChevronRight, Database, Layers, LogOut } from 'lucide-vue-next'
|
||||
|
||||
const { logout } = useAuth()
|
||||
|
||||
const handleLogout = async () => {
|
||||
await logout()
|
||||
}
|
||||
import { LayoutGrid, Boxes, Settings, Home, ChevronRight, Database, Layers } from 'lucide-vue-next'
|
||||
|
||||
const menuItems = [
|
||||
{
|
||||
@@ -131,9 +125,8 @@ const menuItems = [
|
||||
<SidebarFooter>
|
||||
<SidebarMenu>
|
||||
<SidebarMenuItem>
|
||||
<SidebarMenuButton @click="handleLogout" class="cursor-pointer hover:bg-accent">
|
||||
<LogOut class="h-4 w-4" />
|
||||
<span>Logout</span>
|
||||
<SidebarMenuButton>
|
||||
<span class="text-sm text-muted-foreground">Logged in as user</span>
|
||||
</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
</SidebarMenu>
|
||||
|
||||
@@ -5,34 +5,8 @@ import { Label } from '@/components/ui/label'
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
const router = useRouter()
|
||||
const { toast } = useToast()
|
||||
|
||||
// Cookie for server-side auth check
|
||||
const tokenCookie = useCookie('token')
|
||||
|
||||
// Extract subdomain from hostname (e.g., tenant1.localhost → tenant1)
|
||||
const getSubdomain = () => {
|
||||
if (!import.meta.client) return null
|
||||
const hostname = window.location.hostname
|
||||
const parts = hostname.split('.')
|
||||
|
||||
console.log('Extracting subdomain from:', hostname, 'parts:', parts)
|
||||
|
||||
// For localhost development: tenant1.localhost or localhost
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
return null // Use default tenant for plain localhost
|
||||
}
|
||||
|
||||
// For subdomains like tenant1.routebox.co or tenant1.localhost
|
||||
if (parts.length >= 2 && parts[0] !== 'www') {
|
||||
console.log('Using subdomain:', parts[0])
|
||||
return parts[0] // Return subdomain
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const subdomain = ref(getSubdomain())
|
||||
const tenantId = ref('123')
|
||||
const email = ref('')
|
||||
const password = ref('')
|
||||
const loading = ref(false)
|
||||
@@ -43,18 +17,12 @@ const handleLogin = async () => {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
// Only send x-tenant-id if we have a subdomain
|
||||
if (subdomain.value) {
|
||||
headers['x-tenant-id'] = subdomain.value
|
||||
}
|
||||
|
||||
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/login`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-tenant-id': tenantId.value,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email.value,
|
||||
password: password.value,
|
||||
@@ -68,23 +36,15 @@ const handleLogin = async () => {
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
// Store credentials in localStorage
|
||||
// Store the tenant ID that was used for login
|
||||
const tenantToStore = subdomain.value || data.user?.tenantId || 'tenant1'
|
||||
localStorage.setItem('tenantId', tenantToStore)
|
||||
// Store credentials
|
||||
localStorage.setItem('tenantId', tenantId.value)
|
||||
localStorage.setItem('token', data.access_token)
|
||||
localStorage.setItem('user', JSON.stringify(data.user))
|
||||
|
||||
// Also store token in cookie for server-side auth check
|
||||
tokenCookie.value = data.access_token
|
||||
|
||||
toast.success('Login successful!')
|
||||
|
||||
// Redirect to home
|
||||
router.push('/')
|
||||
} catch (e: any) {
|
||||
error.value = e.message || 'Login failed'
|
||||
toast.error(e.message || 'Login failed')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -105,6 +65,10 @@ const handleLogin = async () => {
|
||||
</div>
|
||||
|
||||
<div class="grid gap-6">
|
||||
<div class="grid gap-2">
|
||||
<Label for="tenantId">Tenant ID</Label>
|
||||
<Input id="tenantId" v-model="tenantId" type="text" placeholder="123" required />
|
||||
</div>
|
||||
<div class="grid gap-2">
|
||||
<Label for="email">Email</Label>
|
||||
<Input id="email" v-model="email" type="email" placeholder="m@example.com" required />
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
export const useApi = () => {
|
||||
const config = useRuntimeConfig()
|
||||
const router = useRouter()
|
||||
const { toast } = useToast()
|
||||
const { isLoggedIn, logout } = useAuth()
|
||||
|
||||
// Use current domain for API calls (same subdomain routing)
|
||||
const getApiBaseUrl = () => {
|
||||
@@ -37,44 +34,13 @@ export const useApi = () => {
|
||||
return headers
|
||||
}
|
||||
|
||||
const handleResponse = async (response: Response) => {
|
||||
if (response.status === 401) {
|
||||
// Unauthorized - not authenticated
|
||||
if (import.meta.client) {
|
||||
logout()
|
||||
toast.error('Your session has expired. Please login again.')
|
||||
router.push('/login')
|
||||
}
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
// Forbidden - not authorized
|
||||
if (import.meta.client) {
|
||||
toast.error('You do not have permission to perform this action.')
|
||||
// Redirect to home if logged in, otherwise to login
|
||||
if (isLoggedIn()) {
|
||||
router.push('/')
|
||||
} else {
|
||||
router.push('/login')
|
||||
}
|
||||
}
|
||||
throw new Error('Forbidden')
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`)
|
||||
}
|
||||
|
||||
return response.json()
|
||||
}
|
||||
|
||||
const api = {
|
||||
async get(path: string) {
|
||||
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
||||
headers: getHeaders(),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async post(path: string, data: any) {
|
||||
@@ -83,7 +49,8 @@ export const useApi = () => {
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async put(path: string, data: any) {
|
||||
@@ -92,7 +59,8 @@ export const useApi = () => {
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
async delete(path: string) {
|
||||
@@ -100,7 +68,8 @@ export const useApi = () => {
|
||||
method: 'DELETE',
|
||||
headers: getHeaders(),
|
||||
})
|
||||
return handleResponse(response)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
|
||||
return response.json()
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
export const useAuth = () => {
|
||||
const tokenCookie = useCookie('token')
|
||||
const authMessageCookie = useCookie('authMessage')
|
||||
const router = useRouter()
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const isLoggedIn = () => {
|
||||
if (!import.meta.client) return false
|
||||
const token = localStorage.getItem('token')
|
||||
const tenantId = localStorage.getItem('tenantId')
|
||||
return !!(token && tenantId)
|
||||
}
|
||||
|
||||
const logout = async () => {
|
||||
if (import.meta.client) {
|
||||
// Call backend logout endpoint
|
||||
try {
|
||||
const token = localStorage.getItem('token')
|
||||
const tenantId = localStorage.getItem('tenantId')
|
||||
|
||||
if (token) {
|
||||
await fetch(`${config.public.apiBaseUrl}/api/auth/logout`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
...(tenantId && { 'x-tenant-id': tenantId }),
|
||||
},
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error)
|
||||
}
|
||||
|
||||
// Clear local storage
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('tenantId')
|
||||
localStorage.removeItem('user')
|
||||
|
||||
// Clear cookie for server-side check
|
||||
tokenCookie.value = null
|
||||
|
||||
// Set flash message for login page
|
||||
authMessageCookie.value = 'Logged out successfully'
|
||||
|
||||
// Redirect to login page
|
||||
router.push('/login')
|
||||
}
|
||||
}
|
||||
|
||||
const getUser = () => {
|
||||
if (!import.meta.client) return null
|
||||
const userStr = localStorage.getItem('user')
|
||||
return userStr ? JSON.parse(userStr) : null
|
||||
}
|
||||
|
||||
return {
|
||||
isLoggedIn,
|
||||
logout,
|
||||
getUser,
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import { toast as sonnerToast } from 'vue-sonner'
|
||||
|
||||
export const useToast = () => {
|
||||
const toast = {
|
||||
success: (message: string) => {
|
||||
sonnerToast.success(message)
|
||||
},
|
||||
error: (message: string) => {
|
||||
sonnerToast.error(message)
|
||||
},
|
||||
info: (message: string) => {
|
||||
sonnerToast.info(message)
|
||||
},
|
||||
warning: (message: string) => {
|
||||
sonnerToast.warning(message)
|
||||
},
|
||||
}
|
||||
|
||||
return { toast }
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
// Allow pages to opt-out of auth with definePageMeta({ auth: false })
|
||||
if (to.meta.auth === false) {
|
||||
return
|
||||
}
|
||||
|
||||
// Public routes that don't require authentication
|
||||
const publicRoutes = ['/login', '/register']
|
||||
|
||||
if (publicRoutes.includes(to.path)) {
|
||||
return
|
||||
}
|
||||
|
||||
const token = useCookie('token')
|
||||
const authMessage = useCookie('authMessage')
|
||||
|
||||
// Routes that don't need a toast message (user knows they need to login)
|
||||
const silentRoutes = ['/']
|
||||
|
||||
// Check token cookie (works on both server and client)
|
||||
if (!token.value) {
|
||||
if (!silentRoutes.includes(to.path)) {
|
||||
authMessage.value = 'Please login to access this page'
|
||||
}
|
||||
return navigateTo('/login')
|
||||
}
|
||||
|
||||
// On client side, also verify localStorage is in sync
|
||||
if (import.meta.client) {
|
||||
const { isLoggedIn } = useAuth()
|
||||
if (!isLoggedIn()) {
|
||||
if (!silentRoutes.includes(to.path)) {
|
||||
authMessage.value = 'Please login to access this page'
|
||||
}
|
||||
return navigateTo('/login')
|
||||
}
|
||||
}
|
||||
})
|
||||
9
frontend/package-lock.json
generated
9
frontend/package-lock.json
generated
@@ -21,8 +21,7 @@
|
||||
"shadcn-nuxt": "^2.3.3",
|
||||
"tailwind-merge": "^2.2.1",
|
||||
"vue": "^3.4.15",
|
||||
"vue-router": "^4.2.5",
|
||||
"vue-sonner": "^1.3.2"
|
||||
"vue-router": "^4.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nuxtjs/color-mode": "^3.3.2",
|
||||
@@ -16037,12 +16036,6 @@
|
||||
"vue": "^3.5.0"
|
||||
}
|
||||
},
|
||||
"node_modules/vue-sonner": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/vue-sonner/-/vue-sonner-1.3.2.tgz",
|
||||
"integrity": "sha512-UbZ48E9VIya3ToiRHAZUbodKute/z/M1iT8/3fU8zEbwBRE11AKuHikssv18LMk2gTTr6eMQT4qf6JoLHWuj/A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/webidl-conversions": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||
|
||||
@@ -27,8 +27,7 @@
|
||||
"shadcn-nuxt": "^2.3.3",
|
||||
"tailwind-merge": "^2.2.1",
|
||||
"vue": "^3.4.15",
|
||||
"vue-router": "^4.2.5",
|
||||
"vue-sonner": "^1.3.2"
|
||||
"vue-router": "^4.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nuxtjs/color-mode": "^3.3.2",
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
<script setup lang="ts">
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
<div class="text-center space-y-6">
|
||||
|
||||
@@ -1,33 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { LayoutGrid } from 'lucide-vue-next'
|
||||
import LoginForm from '@/components/LoginForm.vue'
|
||||
|
||||
// Skip auth middleware for login page
|
||||
definePageMeta({
|
||||
auth: false
|
||||
})
|
||||
|
||||
const { toast } = useToast()
|
||||
|
||||
// Check for auth message from cookie
|
||||
const authMessage = useCookie('authMessage')
|
||||
|
||||
onMounted(() => {
|
||||
if (authMessage.value) {
|
||||
console.log('Displaying auth message: ' + authMessage.value)
|
||||
const message = authMessage.value
|
||||
|
||||
// Show success toast for logout, error for auth failures
|
||||
if (message.toLowerCase().includes('logged out')) {
|
||||
toast.success(message)
|
||||
} else {
|
||||
toast.error(message)
|
||||
}
|
||||
|
||||
// Clear the message after displaying
|
||||
authMessage.value = null
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -17,6 +17,11 @@
|
||||
</div>
|
||||
|
||||
<form @submit.prevent="handleRegister" class="space-y-4">
|
||||
<div class="space-y-2">
|
||||
<Label for="tenantId">Tenant ID</Label>
|
||||
<Input id="tenantId" v-model="tenantId" type="text" required placeholder="123" />
|
||||
</div>
|
||||
|
||||
<div class="space-y-2">
|
||||
<Label for="email">Email</Label>
|
||||
<Input
|
||||
@@ -69,29 +74,10 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
// Skip auth middleware for register page
|
||||
definePageMeta({
|
||||
auth: false
|
||||
})
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
const router = useRouter()
|
||||
|
||||
// Extract subdomain from hostname
|
||||
const getSubdomain = () => {
|
||||
if (!import.meta.client) return null
|
||||
const hostname = window.location.hostname
|
||||
const parts = hostname.split('.')
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
return null
|
||||
}
|
||||
if (parts.length > 1 && parts[0] !== 'www') {
|
||||
return parts[0]
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const subdomain = ref(getSubdomain())
|
||||
const tenantId = ref('123')
|
||||
const email = ref('')
|
||||
const password = ref('')
|
||||
const firstName = ref('')
|
||||
@@ -106,17 +92,12 @@ const handleRegister = async () => {
|
||||
error.value = ''
|
||||
success.value = false
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
if (subdomain.value) {
|
||||
headers['x-tenant-id'] = subdomain.value
|
||||
}
|
||||
|
||||
const response = await fetch(`${config.public.apiBaseUrl}/api/auth/register`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-tenant-id': tenantId.value,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email.value,
|
||||
password: password.value,
|
||||
|
||||
6
package-lock.json
generated
6
package-lock.json
generated
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "neo",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {}
|
||||
}
|
||||
Reference in New Issue
Block a user