feat: setting auth cookie and index page logic
This commit is contained in:
parent
ef6646e004
commit
bf1fc33469
|
@ -1,5 +1,7 @@
|
|||
ThisBuild / scalaVersion := "3.3.0"
|
||||
|
||||
fork := true
|
||||
|
||||
libraryDependencies += "com.lihaoyi" %% "upickle" % "3.1.2"
|
||||
libraryDependencies += "com.lihaoyi" %% "requests" % "0.8.0"
|
||||
libraryDependencies += "com.lihaoyi" %% "cask" % "0.9.1"
|
||||
|
|
|
@ -4,27 +4,79 @@ import AuthService._
|
|||
import example.pocketbase.Api
|
||||
import upickle.default._
|
||||
import example.pocketbase.Models._
|
||||
import java.time.Instant
|
||||
|
||||
case class AuthService()(implicit cc: castor.Context, log: cask.Logger)
|
||||
extends cask.Routes {
|
||||
|
||||
@cask.get("/")
|
||||
def getIndex(request: cask.Request) = {
|
||||
println("hellololo")
|
||||
val authCookieOpt = request.cookies.get(authCookieName)
|
||||
println(s"getting index with auth cookie: $authCookieOpt")
|
||||
|
||||
authCookieOpt match {
|
||||
case None =>
|
||||
cask.Redirect("/login")
|
||||
println("cookie was None")
|
||||
val redirectingHtml = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; url='/login'" />
|
||||
</head>
|
||||
<body>
|
||||
<p>You will be redirected to w3docs.com soon!</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
cask.Response(
|
||||
redirectingHtml,
|
||||
200,
|
||||
Seq(
|
||||
"Content-Type" -> "text/html;charset=UTF-8",
|
||||
"Cache-Control" -> "Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"
|
||||
)
|
||||
)
|
||||
case Some(authCookie) =>
|
||||
val jwt = authCookie.value
|
||||
???
|
||||
val refreshedSession = pocketbaseApi.refreshSession(jwt)
|
||||
refreshedSession match {
|
||||
case Right(freshAuth) =>
|
||||
val indexHtml = s"""
|
||||
<h1>Ok, good</h1>
|
||||
<p>user should be already created, current jwt : ${freshAuth.token}</p>
|
||||
<p>the account is on ${freshAuth.record.email} and ${freshAuth.record.username}</p>
|
||||
"""
|
||||
cask.Response(
|
||||
indexHtml,
|
||||
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"),
|
||||
cookies =
|
||||
Seq(cask.Cookie(name = authCookieName, value = freshAuth.token))
|
||||
)
|
||||
case Left(err) =>
|
||||
val indexHtml = """
|
||||
<h1>Ok, not good at all</h1>
|
||||
<p>For some reason error during authorization</p>
|
||||
<p>You can try again <a href="/login">here</a>
|
||||
"""
|
||||
cask.Response(
|
||||
indexHtml,
|
||||
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"),
|
||||
cookies = Seq(
|
||||
cask.Cookie(
|
||||
name = authCookieName,
|
||||
value = "",
|
||||
expires = Instant.EPOCH
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@cask.get("/login")
|
||||
def getLoginPage() = {
|
||||
// render auth page with the available oauth providers
|
||||
println("landed on login page")
|
||||
val authOptions = pocketbaseApi.listAuthMethods()
|
||||
|
||||
val options = s"got following auth opitons: $authOptions"
|
||||
|
@ -47,7 +99,9 @@ case class AuthService()(implicit cc: castor.Context, log: cask.Logger)
|
|||
cask.Response(
|
||||
html,
|
||||
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"),
|
||||
cookies = Seq(cask.Cookie(name = oauthVerifiersCookieName, value = yoyo))
|
||||
cookies = Seq(
|
||||
cask.Cookie(name = oauthVerifiersCookieName, value = yoyo, path = "/")
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
|
@ -88,16 +142,43 @@ case class AuthService()(implicit cc: castor.Context, log: cask.Logger)
|
|||
val okMessageFirst = resultOpt match {
|
||||
case None =>
|
||||
// i guess with the SSR i'll need to return message about unsuccessful auth?
|
||||
s"""
|
||||
val unsuccessHtml = s"""
|
||||
<h1>Auth unsuccessful</h1>
|
||||
"""
|
||||
cask.Response(
|
||||
unsuccessHtml,
|
||||
headers = Seq("Content-Type" -> "text/html;charset=UTF-8")
|
||||
)
|
||||
case Some(result) =>
|
||||
// this is already fully successful auth
|
||||
s"""
|
||||
<h1>Ok, good</h1>
|
||||
<p>user should be already created, current jwt : ${result.token}</p>
|
||||
<p>the account is on ${result.record.email} and ${result.record.username}</p>
|
||||
val redirectingHtml = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; url='/login'" />
|
||||
</head>
|
||||
<body>
|
||||
<p>Successful authorization, you will be redirected to main page soon.</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
cask.Response(
|
||||
redirectingHtml,
|
||||
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"),
|
||||
cookies = Seq(
|
||||
cask.Cookie(
|
||||
name = authCookieName,
|
||||
value = result.token,
|
||||
path = "/"
|
||||
),
|
||||
cask.Cookie(
|
||||
name = oauthVerifiersCookieName,
|
||||
value = "",
|
||||
expires = Instant.EPOCH,
|
||||
path = "/"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -112,18 +193,7 @@ and delete the state\verifiers cookie
|
|||
but then what? i guess call for redirect to root page again?
|
||||
which should trigger auth check and main page render?
|
||||
*/
|
||||
import java.time.Instant
|
||||
cask.Response(
|
||||
okMessageFirst,
|
||||
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"),
|
||||
cookies = Seq(
|
||||
cask.Cookie(
|
||||
name = oauthVerifiersCookieName,
|
||||
value = "",
|
||||
expires = Instant.now()
|
||||
)
|
||||
)
|
||||
)
|
||||
okMessageFirst
|
||||
}
|
||||
|
||||
initialize()
|
||||
|
|
|
@ -7,7 +7,7 @@ import requests.Response
|
|||
|
||||
final case class Api(pocketbaseUrl: String, usersCollection: String = "users") {
|
||||
def refreshSession(jwt: String): Either[Error, AuthReply] = {
|
||||
val path = s"/api/collections/${pocketbaseUrl}/auth-refresh"
|
||||
val path = s"/api/collections/${usersCollection}/auth-refresh"
|
||||
|
||||
val refreshResult = requests.post(
|
||||
url = pocketbaseUrl ++ path,
|
||||
|
|
Loading…
Reference in New Issue