Skip to content

Commit

Permalink
Fix oauth_state cookie in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscariremma committed Sep 27, 2024
1 parent 337ced7 commit 348dc08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 24 additions & 11 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
'use client';

import { Suggestions } from "../components/Suggestions";
import { checkLogin } from "../lib/goldapps/auth";
import { redirect } from "next/navigation";
import { useRouter } from "next/navigation";
import { getSuggestions } from "../lib/goldapps/get-suggestions";
import { createGoldappsServerClient } from "../lib/goldapps/client-server";
import {useEffect, useState} from "react";
import {Suggestion} from "../lib/goldapps/types";
import {createGoldappsBrowserClient} from "../lib/goldapps/client-browser";

async function fetchSuggestions() {
const goldappsClient = createGoldappsServerClient();
const redirectUri = await checkLogin(goldappsClient);
if (redirectUri) {
return redirect(redirectUri);
}

return getSuggestions(goldappsClient);
}

export default async function IndexPage() {
const suggestions = await fetchSuggestions();

const router = useRouter();
const fetchSuggestions = async () => {
const goldappsClient = createGoldappsBrowserClient();
const redirectUri = await checkLogin(goldappsClient);
if (redirectUri) {
return router.push(redirectUri);
}

return getSuggestions(goldappsClient);
}

const [suggestions, setSuggestions] = useState<Suggestion[]>([]);
useEffect(() => {
fetchSuggestions().then((suggestions) => {
setSuggestions(suggestions || []);
});
}, []);
return <Suggestions suggestions={suggestions} />;
}
2 changes: 2 additions & 0 deletions frontend/src/lib/goldapps/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AxiosInstance, AxiosResponse } from "axios";
import { cookies } from "next/headers";

// Returns login uri if not logged in
export async function checkLogin(client: AxiosInstance) {
Expand All @@ -13,6 +14,7 @@ export async function checkLogin(client: AxiosInstance) {
return null;
} catch (e) {
const response = (e as any).response as AxiosResponse<string>;

return response.data;
}
}

0 comments on commit 348dc08

Please sign in to comment.