Skip to content

Commit

Permalink
Remove SupabaseUserServiceGoTrueImpl and refactor its functionality.
Browse files Browse the repository at this point in the history
Replaced the SupabaseUserServiceGoTrueImpl with SupabaseUserService for a more streamlined implementation. Updated various parts of the code to reflect the changes, enhancing maintainability and ensuring proper functionality.
  • Loading branch information
tschuehly committed Aug 23, 2024
1 parent 5b153e2 commit d8f0c3e
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import de.tschuehly.htmx.spring.supabase.auth.security.SupabaseAuthenticationPro
import de.tschuehly.htmx.spring.supabase.auth.security.SupabaseJwtVerifier
import de.tschuehly.htmx.spring.supabase.auth.security.SupabaseSecurityConfig
import de.tschuehly.htmx.spring.supabase.auth.service.SupabaseUserService
import de.tschuehly.htmx.spring.supabase.auth.service.SupabaseUserServiceGoTrueImpl
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.gotrue.Auth
import io.github.jan.supabase.gotrue.auth
Expand Down Expand Up @@ -47,7 +46,7 @@ class SupabaseAutoConfiguration(
applicationEventPublisher: ApplicationEventPublisher
): SupabaseUserService {
logger.debug("Registering the SupabaseUserService")
return SupabaseUserServiceGoTrueImpl(
return SupabaseUserService(
supabaseProperties,
goTrueClient,
applicationEventPublisher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SupabaseProperties(
val passwordRecoveryPage: String?,
val unauthenticatedPage: String?,
val unauthorizedPage: String?,
val postLogoutPage: String?,
val sslOnly: Boolean = true,
val public: Public = Public(),
val roles: MutableMap<String, Role> = mutableMapOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package de.tschuehly.htmx.spring.supabase.auth.controller

import de.tschuehly.htmx.spring.supabase.auth.exception.info.MissingCredentialsException.Companion.MissingCredentials
import de.tschuehly.htmx.spring.supabase.auth.service.SupabaseUserService
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -27,35 +26,39 @@ class SupabaseUserController(
) {
checkCredentialsAndExecute(email, password) { checkedEmail, checkedPassword ->
logger.debug("User with the email $checkedEmail is trying to login")
supabaseUserService.loginWithEmail(checkedEmail, checkedPassword, response)
supabaseUserService.loginWithEmail(checkedEmail, checkedPassword)
}
}

@PostMapping("/signup")
fun signUp(
@RequestParam email: String?,
@RequestParam password: String?,
response: HttpServletResponse
@RequestParam password: String?
) {
checkCredentialsAndExecute(email, password) { checkedEmail, checkedPassword ->
logger.debug("User with the email $checkedEmail is trying to signup")
supabaseUserService.signUpWithEmail(checkedEmail, checkedPassword, response)
supabaseUserService.signUpWithEmail(checkedEmail, checkedPassword)
}
}

@PostMapping("/anon")
fun anonSignIn(request: HttpServletRequest, response: HttpServletResponse) {
supabaseUserService.signInAnonymously(request, response)
@PostMapping("/loginAnon")
fun anonSignIn() {
supabaseUserService.signInAnonymously()
}

@PostMapping("/loginAnonWithEmail")
fun anonSignInWithEmail() {

}


@PostMapping("/linkIdentity")
fun linkIdentity(
@RequestParam email: String?,
request: HttpServletRequest, response: HttpServletResponse
@RequestParam email: String?
) {
if (email != null) {
logger.debug("User with the email $email is linking an Anonymous User")
supabaseUserService.linkAnonToIdentity(email, request, response)
supabaseUserService.linkAnonToIdentity(email)
} else {
MissingCredentials.EMAIL_MISSING.throwExc()
}
Expand Down Expand Up @@ -87,34 +90,34 @@ class SupabaseUserController(

password.isNullOrBlank() ->
MissingCredentials.PASSWORD_MISSING.throwExc()

else ->
function(email.trim(), password.trim())
}
}

@PostMapping("/jwt")
fun authorizeWithJwtOrResetPassword(request: HttpServletRequest, response: HttpServletResponse) {
supabaseUserService.handleClientAuthentication(request, response)
fun authorizeWithJwtOrResetPassword() {
supabaseUserService.handleClientAuthentication()
}

@GetMapping("/logout")
fun logout(request: HttpServletRequest, response: HttpServletResponse) {
supabaseUserService.logout(request, response)
fun logout() {
supabaseUserService.logout()
}

@PutMapping("/setRoles")
@ResponseBody
fun setRoles(
@RequestParam
roles: List<String>?,
request: HttpServletRequest,
@RequestParam
userId: String,
) {
if (userId == "") {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "UserId required")
}
supabaseUserService.setRolesWithRequest(request, userId, roles)
supabaseUserService.setRolesWithRequest(userId, roles)
}

@PostMapping("/sendPasswordResetEmail")
Expand All @@ -129,11 +132,7 @@ class SupabaseUserController(

@PostMapping("/updatePassword")
@ResponseBody
fun updatePassword(
request: HttpServletRequest,
@RequestParam
password: String
) {
supabaseUserService.updatePassword(request, password)
fun updatePassword(@RequestParam password: String) {
supabaseUserService.updatePassword( password)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.tschuehly.htmx.spring.supabase.auth.htmx

import de.tschuehly.htmx.spring.supabase.auth.exception.HxCurrentUrlHeaderNotFound
import io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxRequestHeader
import io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponseHeader
import io.github.wimdeblauwe.htmx.spring.boot.mvc.HxSwapType
import jakarta.servlet.http.Cookie
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.web.context.request.RequestContextHolder
Expand All @@ -17,6 +19,9 @@ object HtmxUtil {
setHeader(HtmxResponseHeader.HX_RETARGET.getValue(), cssSelector)
}

fun getCurrentUrl() = getRequest().getHeader("HX-Current-URL")
?: throw HxCurrentUrlHeaderNotFound()

fun retargetToId(id: String) {
val request: HttpServletRequest = getRequest()
if (request.getHeader(HtmxRequestHeader.HX_REQUEST.getValue()) != null) {
Expand All @@ -41,10 +46,18 @@ object HtmxUtil {
getResponse().setHeader(headerName, headerValue)
}

fun setHeader(htmxResponseHeader: HtmxResponseHeader, headerValue: String?) {
getResponse().setHeader(htmxResponseHeader.value, headerValue)
}

fun isHtmxRequest(): Boolean {
return getRequest().getHeader(HtmxRequestHeader.HX_REQUEST.value) == "true"
}

fun getCookie(name: String): Cookie? {
return getRequest().cookies?.find { it.name == name }
}

fun getResponse(): HttpServletResponse {
return (RequestContextHolder.getRequestAttributes() as? ServletRequestAttributes)?.response
?: throw RuntimeException("No response found in RequestContextHolder")
Expand All @@ -55,5 +68,4 @@ object HtmxUtil {
?: throw RuntimeException("No response found in RequestContextHolder")
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import de.tschuehly.htmx.spring.supabase.auth.types.SupabaseUser
import org.springframework.security.authentication.AnonymousAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder

class SupabaseSecurityContextHolder {
companion object {
@JvmStatic
fun getAuthenticatedUser(): SupabaseUser? {
val authentication = SecurityContextHolder.getContext().authentication
if (authentication !is AnonymousAuthenticationToken) {
return (authentication as SupabaseAuthenticationToken).principal
}
return null
object SupabaseSecurityContextHolder {
fun getAuthenticatedUser(): SupabaseUser? {
val authentication = SecurityContextHolder.getContext().authentication
if (authentication !is AnonymousAuthenticationToken) {
return (authentication as SupabaseAuthenticationToken).principal
}
return null
}

}
Loading

0 comments on commit d8f0c3e

Please sign in to comment.