FYPBackend / src / main / java / com / cardpaymentsystem / cardplatform / controller / RefreshTokenController.java
RefreshTokenController.java
Raw
package com.cardpaymentsystem.cardplatform.controller;

import com.cardpaymentsystem.cardplatform.datatransferobjects.authdtos.AuthenticationResponse;
import com.cardpaymentsystem.cardplatform.datatransferobjects.authdtos.RefreshTokenRequest;
import com.cardpaymentsystem.cardplatform.exceptions.CardPlatformException;
import com.cardpaymentsystem.cardplatform.services.RefreshTokenService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/refreshtoken")
@AllArgsConstructor
public class RefreshTokenController {

    private final RefreshTokenService refreshTokenService;

    @PostMapping("")
    public AuthenticationResponse refreshToken(@RequestBody String refreshTokenString) throws CardPlatformException {
        AuthenticationResponse authenticationResponse = refreshTokenService.refreshToken(refreshTokenString);
        System.out.println(authenticationResponse.authToken);
        return authenticationResponse;
    }


    @PostMapping("/logout")
    public ResponseEntity<String> logout(@Valid @RequestBody RefreshTokenRequest refreshTokenRequest){
        refreshTokenService.deletRefreshToken(refreshTokenRequest.getRefreshToken());
        return ResponseEntity.status(HttpStatus.OK).body("Refresh Token deleted successfully");
    }

    @PostMapping("/logoutmobile")
    public ResponseEntity<String> logoutmobile(@Valid @RequestBody RefreshTokenRequest refreshTokenRequest){
        refreshTokenService.deletRefreshTokenMobile(refreshTokenRequest.getRefreshToken());
        return ResponseEntity.status(HttpStatus.OK).body("Refresh Token deleted successfully");
    }

}