package com.skr.mdm.controller;

import com.skr.mdm.authentication.user.UserTokenHandler;
import com.skr.mdm.dto.TokenDTO;
import com.skr.mdm.enums.ResultCodeEnum;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author nfq
 * @date 2020/7/16 17:32
 */
@RestController
@Api(value = "用户认证", tags = "用户认证")
@RequestMapping("/user")
public class UserAuthenticationController {

    @Autowired
    private UserTokenHandler userTokenHandler;

    @ApiOperation(value = "刷新令牌服务")
    @PostMapping("/refresh/token")
    public ResponseResult refreshToken(@RequestParam @ApiParam(value = "刷新令牌", required = true) String refresh_token,
                                       @RequestParam @ApiParam(value = "访问令牌", required = true) String jwt_token) {

        TokenDTO tokenDTO = userTokenHandler.refreshToken(jwt_token, refresh_token);
        if (tokenDTO == null) {
            return ResponseResult.failure(ResultCodeEnum.REFRESH_TOKEN_EXPIRE);
        }
        return ResponseResult.success(tokenDTO);
    }

}
