import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(private readonly jwtService: JwtService) {}

  generateTokens(id: string) {
    const accessToken = this.jwtService.sign({id: id}, { expiresIn: '5m' });
    const refreshToken = this.jwtService.sign({id: id}, { expiresIn: '7d' });

    return { accessToken, refreshToken };
  }

  refreshTokens(id: string) {
    try {
      return this.generateTokens(id);
    } catch (e) {
      throw new UnauthorizedException('Invalid Refresh Token');
    }
  }

  validateToken(token: string) {
    return this.jwtService.verify(token);
  }
}