/
koskv
/
TeamSync-Bot
Обзор
Документация
Войти
/
koskv
/
TeamSync-Bot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/org/example/teamsyncbot/client/auth/TeamSyncAuthClient.java
39 строк
1 KB
Koskv
Add: Get All Users
13 дек 2025, 23:43
13 дек 2025, 23:43
6966893
Код
Авторство
О чём код?
package org.example.teamsyncbot.client.auth; import lombok.AllArgsConstructor; import org.example.teamsyncbot.config.BotProperties; import org.example.teamsyncbot.dto.login.LoginRequest; import org.example.teamsyncbot.dto.login.LoginResponse; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component @AllArgsConstructor public class TeamSyncAuthClient { private final RestTemplate restTemplate; private final BotProperties botProperties; public String loginAndGetToken() { String url = botProperties.getBackendUrl() + "/api/auth/login"; LoginRequest request = new LoginRequest( botProperties.getAuth().getUsername(), botProperties.getAuth().getPassword() ); ResponseEntity<LoginResponse> response = restTemplate.postForEntity(url, request, LoginResponse.class); if (response.getStatusCode() != HttpStatus.OK || response.getBody() == null) { throw new RuntimeException("Ошибка аутентификации: " + response.getStatusCode()); } String fullToken = response.getBody().token(); if (fullToken == null || !fullToken.startsWith("Bearer ")) { throw new RuntimeException("Некорректный токен: " + fullToken); } return fullToken; // "Bearer ..." } }