| 17 | |
| 18 | @Controller('dyan/auth') |
| 19 | export class AuthController { |
| 20 | constructor( |
| 21 | private authService: AuthService, |
| 22 | private jwtService: JwtService, |
| 23 | ) {} |
| 24 | |
| 25 | @Get('me') |
| 26 | async me(@Req() req: Request) { |
| 27 | const token = req.cookies?.['dyan-token']; |
| 28 | console.log('[me] Received request with token:', token); |
| 29 | |
| 30 | if (!token) { |
| 31 | console.warn('[me] No token found in cookies'); |
| 32 | throw new UnauthorizedException('No token found'); |
| 33 | } |
| 34 | |
| 35 | try { |
| 36 | const payload = await this.jwtService.verifyAsync<{ userId: string }>( |
| 37 | token, |
| 38 | ); |
| 39 | console.log('[me] Decoded JWT payload:', payload); |
| 40 | |
| 41 | const user = await prisma.user.findUnique({ |
| 42 | where: { id: payload.userId }, |
| 43 | select: { email: true, isAdmin: true }, |
| 44 | }); |
| 45 | |
| 46 | if (!user) { |
| 47 | console.warn('[me] No user found for email:', payload.userId); |
| 48 | throw new UnauthorizedException('User not found'); |
| 49 | } |
| 50 | |
| 51 | console.log('[me] User found:', user); |
| 52 | return user; |
| 53 | } catch (error) { |
| 54 | console.error('[me] Error verifying token or fetching user:', error); |
| 55 | throw new UnauthorizedException('Invalid token'); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Post('request') |
| 60 | async requestLogin(@Body('email') email: string) { |
| 61 | if (!email || !email.includes('@')) { |
| 62 | throw new HttpException('Invalid email', HttpStatus.BAD_REQUEST); |
| 63 | } |
| 64 | |
| 65 | const existingUser = await prisma.user.findFirst(); |
| 66 | |
| 67 | let user = await prisma.user.findUnique({ where: { email } }); |
| 68 | |
| 69 | if (!user) { |
| 70 | // first user becomes admin |
| 71 | user = await prisma.user.create({ |
| 72 | data: { email, isAdmin: !existingUser }, |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | return this.authService.sendMagicLink(email); |
nothing calls this directly
no outgoing calls
no test coverage detected