From c52970500018efc83da2d134fedeb1aa06fb77d6 Mon Sep 17 00:00:00 2001 From: iliyangermanov Date: Sun, 1 Dec 2024 00:12:24 +0200 Subject: [PATCH] Working Google login --- README.md | 10 +++++++++- .../src/main/kotlin/ivy/learn/api/common/ApiUtils.kt | 2 +- .../main/kotlin/ivy/learn/data/database/Database.kt | 8 +++++++- .../kotlin/ivy/learn/data/database/tables/Sessions.kt | 2 +- .../kotlin/ivy/learn/domain/auth/GoogleOAuthUseCase.kt | 6 +++++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b81474c8..0e06e9ed 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,21 @@ Learn programming by thinking. You need to have a PostgreSQL database. -``` +```zshrc brew install postgresql brew services start postgresql@14 +psql -U postgres -c "CREATE DATABASE ivy_learn;" psql -d ivy_learn -c "CREATE USER postgres WITH PASSWORD 'password';" psql -d ivy_learn -c "ALTER USER postgres WITH SUPERUSER;" ``` +**(optional) Drop local database:** + +```zshrc +psql -U postgres -c "DROP DATABASE ivy_learn;" +psql -U postgres -c "CREATE DATABASE ivy_learn;" +``` + **Environment Variables** ```zshrc diff --git a/server/src/main/kotlin/ivy/learn/api/common/ApiUtils.kt b/server/src/main/kotlin/ivy/learn/api/common/ApiUtils.kt index cbd374db..b4896e01 100644 --- a/server/src/main/kotlin/ivy/learn/api/common/ApiUtils.kt +++ b/server/src/main/kotlin/ivy/learn/api/common/ApiUtils.kt @@ -66,7 +66,7 @@ suspend inline fun RoutingContext.handleRequest( handler(call) } } catch (e: Throwable) { - Either.Left(ServerError.Unknown("Unexpected error occurred: $e")) + Either.Left(ServerError.Unknown("Unexpected error occurred.")) } result.onLeft { error -> respondError(error) diff --git a/server/src/main/kotlin/ivy/learn/data/database/Database.kt b/server/src/main/kotlin/ivy/learn/data/database/Database.kt index bdb504a9..c02ebbbc 100644 --- a/server/src/main/kotlin/ivy/learn/data/database/Database.kt +++ b/server/src/main/kotlin/ivy/learn/data/database/Database.kt @@ -5,6 +5,8 @@ import arrow.core.raise.catch import arrow.core.raise.either import ivy.learn.config.DatabaseConfig import ivy.learn.data.database.tables.Analytics +import ivy.learn.data.database.tables.Sessions +import ivy.learn.data.database.tables.Users import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.transactions.transaction @@ -30,7 +32,11 @@ class Database { private fun createDbSchema(database: Database): Either = catch({ transaction { - SchemaUtils.create(Analytics) + SchemaUtils.create( + Users, + Sessions, + Analytics, + ) } Either.Right(database) }) { diff --git a/server/src/main/kotlin/ivy/learn/data/database/tables/Sessions.kt b/server/src/main/kotlin/ivy/learn/data/database/tables/Sessions.kt index abdfc597..7371b8fd 100644 --- a/server/src/main/kotlin/ivy/learn/data/database/tables/Sessions.kt +++ b/server/src/main/kotlin/ivy/learn/data/database/tables/Sessions.kt @@ -7,7 +7,7 @@ import org.jetbrains.exposed.sql.kotlin.datetime.timestamp object Sessions : Table() { val token = varchar("token", length = 128).uniqueIndex() - val userId = Analytics.reference( + val userId = reference( name = "user_id", refColumn = Users.id, onDelete = ReferenceOption.CASCADE, diff --git a/server/src/main/kotlin/ivy/learn/domain/auth/GoogleOAuthUseCase.kt b/server/src/main/kotlin/ivy/learn/domain/auth/GoogleOAuthUseCase.kt index a1b850c2..a23580d3 100644 --- a/server/src/main/kotlin/ivy/learn/domain/auth/GoogleOAuthUseCase.kt +++ b/server/src/main/kotlin/ivy/learn/domain/auth/GoogleOAuthUseCase.kt @@ -11,10 +11,12 @@ import io.ktor.http.* import ivy.learn.config.ServerConfiguration import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import org.slf4j.Logger class GoogleOAuthUseCase( private val config: ServerConfiguration, private val httpClient: HttpClient, + private val logger: Logger, ) { suspend fun verify( @@ -29,7 +31,9 @@ class GoogleOAuthUseCase( email = userInfoResponse.email, names = userInfoResponse.name, profilePictureUrl = userInfoResponse.picture, - ) + ).also { + + } } /*