44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Get,
|
|
Body,
|
|
HttpCode,
|
|
HttpStatus,
|
|
} from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
|
import { AuthService } from './auth.service';
|
|
import { CurrentUser } from './decorators/current-user.decorator';
|
|
import { Public } from './decorators/public.decorator';
|
|
import { User } from '@coursecraft/database';
|
|
import { ExchangeTokenDto } from './dto/exchange-token.dto';
|
|
|
|
@ApiTags('auth')
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private authService: AuthService) {}
|
|
|
|
@Public()
|
|
@Post('exchange')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: 'Exchange Supabase token for API token' })
|
|
async exchangeToken(@Body() dto: ExchangeTokenDto) {
|
|
const user = await this.authService.validateSupabaseToken(dto.supabaseToken);
|
|
return this.authService.generateTokens(user);
|
|
}
|
|
|
|
@Get('me')
|
|
@ApiBearerAuth()
|
|
@ApiOperation({ summary: 'Get current user' })
|
|
async getCurrentUser(@CurrentUser() user: User) {
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
avatarUrl: user.avatarUrl,
|
|
subscriptionTier: user.subscriptionTier,
|
|
createdAt: user.createdAt,
|
|
};
|
|
}
|
|
}
|