Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiorowski committed Jan 13, 2024
1 parent 86cd17c commit 298cd26
Show file tree
Hide file tree
Showing 15 changed files with 2,595 additions and 74 deletions.
32 changes: 11 additions & 21 deletions client/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { grpcSafe } from "$lib/safe";
import { usersService } from "$lib/server/grpc";
import { logger, perf } from "$lib/server/logger";
import { createMetadata } from "$lib/server/metadata";
import { Metadata } from "@grpc/grpc-js";
import { redirect } from "@sveltejs/kit";

/** @type {import('@sveltejs/kit').Handle} */
Expand Down Expand Up @@ -35,39 +34,30 @@ export async function handle({ event, resolve }) {

/**
* Check if the user is coming from the oauth flow
* If so, send the token to the backend to create a user
* If so, set a temporary cookie with the token
* On the next request, the new token will be used
*/
const oauth_token = event.url.searchParams.get("oauth_token");
if (oauth_token) {
const metadata = new Metadata();
metadata.add("x-authorization", `bearer ${oauth_token}`);
/** @type {import("$lib/safe").Safe<import("$lib/proto/proto/Id").Id__Output>} */
const token = await new Promise((res) => {
usersService.CreateUser({}, metadata, grpcSafe(res));
});
if (token.error) {
throw redirect(302, "/auth?error=1");
}
event.cookies.set("token", token.data.id, {
let token = event.url.searchParams.get("token");
if (token) {
event.cookies.set("token", token, {
domain: COOKIE_DOMAIN,
path: "/",
// 10 seconds, it's just to create the user and redirect to the dashboard
// after that the token will be refreshed with 7 days max age
// 10 seconds, it should be enough to be read by the backend on the next request
maxAge: 10,
});
throw redirect(302, "/dashboard");
}

if (event.url.pathname === "/") {
throw redirect(302, "/dashboard");
}

const token = event.cookies.get("token") ?? "";
token = event.cookies.get("token") ?? "";
if (!token) {
logger.info("No token");
throw redirect(302, "/auth");
}

if (event.url.pathname === "/") {
throw redirect(302, "/dashboard");
}

const metadata = createMetadata(token);
/** @type {import("$lib/safe").Safe<import("$lib/proto/proto/AuthResponse").AuthResponse__Output>} */
const auth = await new Promise((res) => {
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ services:
PORT: 443
RUST_LOG: info
DATABASE_URL: postgresql://?host=db-users&user=postgres&password=12345&dbname=users
AUTH_URL: http://127.0.0.1:8090
CLIENT_URL: http://127.0.0.1:3000
AUTH_URL: http://127.0.0.1:8090
USERS_URL: http://service-users:443
GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID}
Expand Down
6 changes: 6 additions & 0 deletions proto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ fn main() {
.out_dir("../service-utils/src/")
.compile(&["./main.proto"], &["./"])
.expect("Failed to compile utils protos");

tonic_build::configure()
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.out_dir("../service-auth/src/")
.compile(&["./main.proto"], &["./"])
.expect("Failed to compile utils protos");
}
Loading

0 comments on commit 298cd26

Please sign in to comment.