tg login
This commit is contained in:
@@ -6,6 +6,8 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
|
||||
import org.springframework.stereotype.Service
|
||||
import space.luminic.finance.configs.AuthException
|
||||
import space.luminic.finance.dtos.UserDTO
|
||||
import space.luminic.finance.models.NotFoundException
|
||||
import space.luminic.finance.models.Token
|
||||
import space.luminic.finance.models.User
|
||||
import space.luminic.finance.repos.UserRepo
|
||||
@@ -44,7 +46,7 @@ class AuthService(
|
||||
return username.toInt()
|
||||
}
|
||||
|
||||
fun login(username: String, password: String): String {
|
||||
fun login(username: String, password: String): String {
|
||||
val user = userRepo.findByUsername(username)
|
||||
?: throw UsernameNotFoundException("Пользователь не найден")
|
||||
return if (passwordEncoder.matches(password, user.password)) {
|
||||
@@ -61,10 +63,12 @@ class AuthService(
|
||||
}
|
||||
}
|
||||
|
||||
fun tgLogin(tgId: String): String {
|
||||
val user =
|
||||
userRepo.findByTgId(tgId) ?: throw UsernameNotFoundException("Пользователь не найден")
|
||||
|
||||
fun tgAuth(tgUser: UserDTO.TelegramAuthDTO): String {
|
||||
val user: User = try {
|
||||
tgLogin(tgUser.id)
|
||||
} catch (e: NotFoundException) {
|
||||
registerTg(tgUser)
|
||||
}
|
||||
val token = jwtUtil.generateToken(user.username)
|
||||
val expireAt = Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 10)
|
||||
tokenService.saveToken(
|
||||
@@ -73,7 +77,22 @@ class AuthService(
|
||||
expiresAt = expireAt.toInstant()
|
||||
)
|
||||
return token
|
||||
}
|
||||
|
||||
fun registerTg(tgUser: UserDTO.TelegramAuthDTO): User {
|
||||
val user = User(
|
||||
username = tgUser.username ?: UUID.randomUUID().toString().split('-')[0],
|
||||
firstName = tgUser.first_name ?: UUID.randomUUID().toString().split('-')[0],
|
||||
tgId = tgUser.id,
|
||||
tgUserName = tgUser.username,
|
||||
photoUrl = tgUser.photo_url,
|
||||
roles = mutableListOf("USER")
|
||||
)
|
||||
return userRepo.create(user)
|
||||
}
|
||||
|
||||
fun tgLogin(tgId: Long): User {
|
||||
return userRepo.findByTgId(tgId) ?: throw NotFoundException("User with provided TG id $tgId not found")
|
||||
}
|
||||
|
||||
fun register(username: String, password: String, firstName: String): User {
|
||||
@@ -85,14 +104,14 @@ class AuthService(
|
||||
firstName = firstName,
|
||||
roles = mutableListOf("USER")
|
||||
)
|
||||
newUser = userRepo.save(newUser)
|
||||
newUser = userRepo.create(newUser)
|
||||
return newUser
|
||||
} else throw IllegalArgumentException("Пользователь уже зарегистрирован")
|
||||
}
|
||||
|
||||
|
||||
@Cacheable(cacheNames = ["tokens"], key = "#token")
|
||||
fun isTokenValid(token: String): User {
|
||||
fun isTokenValid(token: String): User {
|
||||
val tokenDetails = tokenService.getToken(token)
|
||||
when {
|
||||
tokenDetails.status == Token.TokenStatus.ACTIVE && tokenDetails.expiresAt.isAfter(Instant.now()) -> {
|
||||
|
||||
@@ -24,7 +24,7 @@ class UserService(val userRepo: UserRepo) {
|
||||
}
|
||||
|
||||
fun getUserByTelegramId(telegramId: Long): User {
|
||||
return userRepo.findByTgId(telegramId.toString())?: throw NotFoundException("User with telegramId: $telegramId not found")
|
||||
return userRepo.findByTgId(telegramId)?: throw NotFoundException("User with telegramId: $telegramId not found")
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user