Skip to content

Commit a4b41d0

Browse files
authored
Untangle frontend from token cookie management (#9)
1 parent fee6537 commit a4b41d0

12 files changed

Lines changed: 139 additions & 175 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ services:
3838
### Required for Production environment
3939
| Key | Description | Example |
4040
| - | - | - |
41-
| `PUBLIC_URL` | Domain under which pepp is deployed | `https://stapel.example.com` |
41+
| `PUBLIC_URL` | Domain under which stapel is deployed | `https://stapel.example.com` |
4242
| `SMTP_HOST` | E-Mail provider | `smtp.example.com` |
4343
| `SMTP_USER` | The user to log into the SMTP Server | `alice@example.com` |
4444
| `SMTP_PASSWORD` | The password to log into the SMTP Server | - |
@@ -69,7 +69,7 @@ npm run dev
6969
```bash
7070
cd server
7171
go generate ./...
72-
go run server.go
72+
ENV=Development go run server.go
7373
```
7474

7575
## Contributions

frontend/components/providers/auth-provider.tsx

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
22

3-
import { deleteCookie, getCookie, setCookie } from "@/lib/cookie";
43
import {
54
useIsActiveSessionQuery,
65
useLogoutMutation,
@@ -27,51 +26,32 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
2726
const router = useRouter();
2827

2928
const [isAuthenticated, setIsAuthenticated] = useState(false);
30-
const [token, setToken] = useState<string | undefined>();
3129

32-
const { error: sessionError, data: sessionData } = useIsActiveSessionQuery({
33-
skip: !token,
34-
variables: { token: token! },
35-
});
30+
const { data: sessionData, refetch: sessionRefetch } =
31+
useIsActiveSessionQuery();
3632

37-
if (sessionError) {
38-
toast.error(
39-
`Bei der Überprüfung der Session ist ein Fehler aufgetreten: ${sessionError.message}`
40-
);
41-
}
33+
useEffect(() => {
34+
if (searchParams.get("l") === "1") {
35+
router.push("/");
36+
sessionRefetch();
37+
}
38+
}, [searchParams]);
4239

4340
useEffect(() => {
4441
if (sessionData) {
4542
setIsAuthenticated(sessionData.isActiveSession);
46-
if (sessionData.isActiveSession) setCookie("token", token!, 7);
4743
}
4844
}, [sessionData]);
4945

50-
useEffect(() => {
51-
const tokenCookie = getCookie("token");
52-
if (tokenCookie) {
53-
setToken(tokenCookie);
54-
}
55-
56-
const tokenSearchParam = searchParams.get("token");
57-
if (tokenSearchParam) {
58-
setToken(tokenSearchParam);
59-
router.push("/");
60-
}
61-
}, [searchParams]);
62-
6346
const [triggerLogout, { error: logoutError }] = useLogoutMutation();
6447

6548
if (logoutError) {
6649
toast.error(`Beim abmelden ist ein Fehler aufgetreten: ${logoutError}`);
6750
}
6851

6952
function logout() {
70-
if (!token) return;
71-
triggerLogout({ variables: { token: token } });
72-
deleteCookie("token");
73-
setToken(undefined);
74-
setIsAuthenticated(false);
53+
triggerLogout();
54+
sessionRefetch()
7555
}
7656

7757
return (

frontend/lib/gql/generated/graphql.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type Mutation = {
3636
__typename: 'Mutation';
3737
createDeck: Scalars['String']['output'];
3838
deleteDeck: Scalars['String']['output'];
39-
logout: Scalars['String']['output'];
39+
logout: Scalars['Boolean']['output'];
4040
setValid: Scalars['String']['output'];
4141
updateDeck: Scalars['String']['output'];
4242
};
@@ -52,12 +52,6 @@ export type MutationDeleteDeckArgs = {
5252
hash: Scalars['String']['input'];
5353
};
5454

55-
56-
export type MutationLogoutArgs = {
57-
token: Scalars['String']['input'];
58-
};
59-
60-
6155
export type MutationSetValidArgs = {
6256
hash: Scalars['String']['input'];
6357
};
@@ -92,11 +86,6 @@ export type QueryDecksArgs = {
9286
year?: InputMaybe<Scalars['Int']['input']>;
9387
};
9488

95-
96-
export type QueryIsActiveSessionArgs = {
97-
token: Scalars['String']['input'];
98-
};
99-
10089
export type CreateDeckMutationVariables = Exact<{
10190
meta: NewDeck;
10291
file: Scalars['Upload']['input'];
@@ -127,12 +116,10 @@ export type SetValidMutationVariables = Exact<{
127116

128117
export type SetValidMutation = { setValid: string };
129118

130-
export type LogoutMutationVariables = Exact<{
131-
token: Scalars['String']['input'];
132-
}>;
119+
export type LogoutMutationVariables = Exact<{ [key: string]: never; }>;
133120

134121

135-
export type LogoutMutation = { logout: string };
122+
export type LogoutMutation = { logout: boolean };
136123

137124
export type DecksQueryVariables = Exact<{
138125
search?: InputMaybe<Scalars['String']['input']>;
@@ -144,9 +131,7 @@ export type DecksQueryVariables = Exact<{
144131

145132
export type DecksQuery = { decks: Array<{ __typename: 'Deck', subject: string, module: string, moduleAlt: string, examiners: string, language: string, semester: string, year: number, hash: string, fileType: string, isValid: boolean }> | null };
146133

147-
export type IsActiveSessionQueryVariables = Exact<{
148-
token: Scalars['String']['input'];
149-
}>;
134+
export type IsActiveSessionQueryVariables = Exact<{ [key: string]: never; }>;
150135

151136

152137
export type IsActiveSessionQuery = { isActiveSession: boolean };
@@ -177,7 +162,7 @@ export type CreateDeckMutationFn = typeof useCreateDeckMutation
177162
* },
178163
* });
179164
*/
180-
export function useCreateDeckMutation(baseOptions?: Apollo.MutationFunctionOptions<CreateDeckMutation, CreateDeckMutationVariables>) {
165+
export function useCreateDeckMutation(baseOptions?: Apollo.MutationHookOptions<CreateDeckMutation, CreateDeckMutationVariables>) {
181166
const options = {...defaultOptions, ...baseOptions}
182167
return Apollo.useMutation<CreateDeckMutation, CreateDeckMutationVariables>(CreateDeckDocument, options);
183168
}
@@ -279,8 +264,8 @@ export type SetValidMutationHookResult = ReturnType<typeof useSetValidMutation>;
279264
export type SetValidMutationResult = Apollo.MutationResult<SetValidMutation>;
280265
export type SetValidMutationOptions = Apollo.MutationFunctionOptions<SetValidMutation, SetValidMutationVariables>;
281266
export const LogoutDocument = gql`
282-
mutation Logout($token: String!) {
283-
logout(token: $token)
267+
mutation Logout {
268+
logout
284269
}
285270
`;
286271
export type LogoutMutationFn = typeof useLogoutMutation
@@ -298,7 +283,6 @@ export type LogoutMutationFn = typeof useLogoutMutation
298283
* @example
299284
* const [logoutMutation, { data, loading, error }] = useLogoutMutation({
300285
* variables: {
301-
* token: // value for 'token'
302286
* },
303287
* });
304288
*/
@@ -362,8 +346,8 @@ export type DecksLazyQueryHookResult = ReturnType<typeof useDecksLazyQuery>;
362346
export type DecksSuspenseQueryHookResult = ReturnType<typeof useDecksSuspenseQuery>;
363347
export type DecksQueryResult = Apollo.QueryResult<DecksQuery, DecksQueryVariables>;
364348
export const IsActiveSessionDocument = gql`
365-
query IsActiveSession($token: String!) {
366-
isActiveSession(token: $token)
349+
query IsActiveSession {
350+
isActiveSession
367351
}
368352
`;
369353

@@ -379,11 +363,10 @@ export const IsActiveSessionDocument = gql`
379363
* @example
380364
* const { data, loading, error } = useIsActiveSessionQuery({
381365
* variables: {
382-
* token: // value for 'token'
383366
* },
384367
* });
385368
*/
386-
export function useIsActiveSessionQuery(baseOptions: Apollo.QueryHookOptions<IsActiveSessionQuery, IsActiveSessionQueryVariables> & ({ variables: IsActiveSessionQueryVariables; skip?: boolean; } | { skip: boolean; }) ) {
369+
export function useIsActiveSessionQuery(baseOptions?: Apollo.QueryHookOptions<IsActiveSessionQuery, IsActiveSessionQueryVariables>) {
387370
const options = {...defaultOptions, ...baseOptions}
388371
return Apollo.useQuery<IsActiveSessionQuery, IsActiveSessionQueryVariables>(IsActiveSessionDocument, options);
389372
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
mutation Logout($token: String!) {
2-
logout(token: $token)
1+
mutation Logout {
2+
logout
33
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
query IsActiveSession($token: String!) {
2-
isActiveSession(token: $token)
1+
query IsActiveSession {
2+
isActiveSession
33
}

frontend/lib/graphql.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import { ApolloClient, ApolloLink, InMemoryCache } from '@apollo/client';
2-
import UploadHttpLink from 'apollo-upload-client/UploadHttpLink.mjs';
3-
import { getCookie } from './cookie';
4-
5-
const authLink = new ApolloLink((operation, forward) => {
6-
const token = getCookie('token');
7-
operation.setContext(({ headers = {} }) => ({
8-
headers: { ...headers, TOKEN: token ?? '' },
9-
}));
10-
return forward(operation);
11-
});
1+
import { ApolloClient, InMemoryCache } from "@apollo/client";
2+
import UploadHttpLink from "apollo-upload-client/UploadHttpLink.mjs";
123

134
const uploadLink = new UploadHttpLink({
14-
uri: '/graphql',
15-
credentials: 'include',
5+
uri: "/graphql",
6+
credentials: "include",
167
});
178

189
export const client = new ApolloClient({
19-
link: authLink.concat(uploadLink),
10+
link: uploadLink,
2011
cache: new InMemoryCache(),
2112
});

frontend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "cards",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"private": true,
55
"scripts": {
66
"dev": "next dev --turbopack",
77
"build": "next build --turbopack",
88
"start": "next start",
9-
"lint": "eslint"
9+
"lint": "eslint",
10+
"codegen": "graphql-codegen --config codegen.ts"
1011
},
1112
"dependencies": {
1213
"@apollo/client": "^4.0.7",

0 commit comments

Comments
 (0)