feat: setting auth cookie and index page logic

This commit is contained in:
efim 2023-07-23 20:05:27 +00:00
parent ef6646e004
commit bf1fc33469
3 changed files with 94 additions and 22 deletions

View File

@ -1,5 +1,7 @@
ThisBuild / scalaVersion := "3.3.0" ThisBuild / scalaVersion := "3.3.0"
fork := true
libraryDependencies += "com.lihaoyi" %% "upickle" % "3.1.2" libraryDependencies += "com.lihaoyi" %% "upickle" % "3.1.2"
libraryDependencies += "com.lihaoyi" %% "requests" % "0.8.0" libraryDependencies += "com.lihaoyi" %% "requests" % "0.8.0"
libraryDependencies += "com.lihaoyi" %% "cask" % "0.9.1" libraryDependencies += "com.lihaoyi" %% "cask" % "0.9.1"

View File

@ -4,27 +4,79 @@ import AuthService._
import example.pocketbase.Api import example.pocketbase.Api
import upickle.default._ import upickle.default._
import example.pocketbase.Models._ import example.pocketbase.Models._
import java.time.Instant
case class AuthService()(implicit cc: castor.Context, log: cask.Logger) case class AuthService()(implicit cc: castor.Context, log: cask.Logger)
extends cask.Routes { extends cask.Routes {
@cask.get("/") @cask.get("/")
def getIndex(request: cask.Request) = { def getIndex(request: cask.Request) = {
println("hellololo")
val authCookieOpt = request.cookies.get(authCookieName) val authCookieOpt = request.cookies.get(authCookieName)
println(s"getting index with auth cookie: $authCookieOpt")
authCookieOpt match { authCookieOpt match {
case None => 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) => case Some(authCookie) =>
val jwt = authCookie.value 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") @cask.get("/login")
def getLoginPage() = { def getLoginPage() = {
// render auth page with the available oauth providers // render auth page with the available oauth providers
println("landed on login page")
val authOptions = pocketbaseApi.listAuthMethods() val authOptions = pocketbaseApi.listAuthMethods()
val options = s"got following auth opitons: $authOptions" val options = s"got following auth opitons: $authOptions"
@ -47,7 +99,9 @@ case class AuthService()(implicit cc: castor.Context, log: cask.Logger)
cask.Response( cask.Response(
html, html,
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"), 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 { val okMessageFirst = resultOpt match {
case None => case None =>
// i guess with the SSR i'll need to return message about unsuccessful auth? // i guess with the SSR i'll need to return message about unsuccessful auth?
s""" val unsuccessHtml = s"""
<h1>Auth unsuccessful</h1> <h1>Auth unsuccessful</h1>
""" """
cask.Response(
unsuccessHtml,
headers = Seq("Content-Type" -> "text/html;charset=UTF-8")
)
case Some(result) => case Some(result) =>
// this is already fully successful auth // this is already fully successful auth
s""" val redirectingHtml = """
<h1>Ok, good</h1> <!DOCTYPE html>
<p>user should be already created, current jwt : ${result.token}</p> <html>
<p>the account is on ${result.record.email} and ${result.record.username}</p> <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? but then what? i guess call for redirect to root page again?
which should trigger auth check and main page render? which should trigger auth check and main page render?
*/ */
import java.time.Instant okMessageFirst
cask.Response(
okMessageFirst,
headers = Seq("Content-Type" -> "text/html;charset=UTF-8"),
cookies = Seq(
cask.Cookie(
name = oauthVerifiersCookieName,
value = "",
expires = Instant.now()
)
)
)
} }
initialize() initialize()

View File

@ -7,7 +7,7 @@ import requests.Response
final case class Api(pocketbaseUrl: String, usersCollection: String = "users") { final case class Api(pocketbaseUrl: String, usersCollection: String = "users") {
def refreshSession(jwt: String): Either[Error, AuthReply] = { 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( val refreshResult = requests.post(
url = pocketbaseUrl ++ path, url = pocketbaseUrl ++ path,