Skip to content

Commit

Permalink
Refactor cookie management and remove ngx-cookie-service dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sentemon committed Jan 7, 2025
1 parent 3ef6fb2 commit 4dfc638
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 82 deletions.
14 changes: 7 additions & 7 deletions backend/src/AuthService/AuthService.Api/GraphQL/Mutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public async Task<KeycloakTokenResponse> Register(RegisterDto input, [Service] R

_httpContextAccessor.HttpContext?.Response.Cookies.Append("token", result.Response.AccessToken, new CookieOptions
{
HttpOnly = true,
Secure = true,
Path = "/",
HttpOnly = false,
Secure = false,
SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.FromUnixTimeSeconds(result.Response.ExpiresIn)
});

return result.Response;
Expand All @@ -53,10 +53,10 @@ public async Task<KeycloakTokenResponse> Login(LoginDto input, [Service] LoginCo

_httpContextAccessor.HttpContext?.Response.Cookies.Append("token", result.Response.AccessToken, new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.FromUnixTimeSeconds(result.Response.ExpiresIn)
Path = "/",
HttpOnly = false,
Secure = false,
SameSite = SameSiteMode.Strict
});

return result.Response;
Expand Down
14 changes: 0 additions & 14 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@apollo/client": "^3.11.10",
"apollo-angular": "^8.0.0",
"graphql": "^16.9.0",
"ngx-cookie-service": "^18.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
Expand Down
29 changes: 29 additions & 0 deletions frontend/src/app/apollo.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApolloClientOptions, InMemoryCache, ApolloLink, HttpLink } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';
import { environment } from '../environments/environment';

export function createApolloClientOptions(): ApolloClientOptions<any> {
const httpLink = new HttpLink({
uri: environment.auth_service,
credentials: 'include',
});

const authLink = setContext(() => {
const token = document.cookie
.split('; ')
.find(row => row.startsWith('token='))
?.split('=')[1];
return {
headers: {
Authorization: token ? `Bearer ${token}` : '',
}
};
});

const link = ApolloLink.from([authLink, httpLink]);

return {
link: link,
cache: new InMemoryCache()
};
}
28 changes: 2 additions & 26 deletions frontend/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import {provideHttpClient, withFetch} from '@angular/common/http';
import { provideApollo } from 'apollo-angular';
import { ApolloLink, InMemoryCache} from '@apollo/client/core';
import {setContext} from "@apollo/client/link/context";
import {environment} from "../environments/environment";
import {HttpLink} from "apollo-angular/http";
import {loadDevMessages, loadErrorMessages} from "@apollo/client/dev";
import { createApolloClientOptions } from "./apollo.config";

export const appConfig: ApplicationConfig = {
providers: [
Expand All @@ -20,30 +18,8 @@ export const appConfig: ApplicationConfig = {
loadDevMessages();
loadErrorMessages();
}
const httpLink = inject(HttpLink);

const authLink = setContext(() => {
const token = document.cookie
.split('; ')
.find(row => row.startsWith('token='))
?.split('=')[1];
return {

headers: {
Authorization: token ? `Bearer ${token}` : '',
}
};
});

const link = ApolloLink.from([
authLink,
httpLink.create({ uri: environment.auth_service }),
]);

return {
link: link,
cache: new InMemoryCache(),
};
return createApolloClientOptions();
})
]
};
23 changes: 0 additions & 23 deletions frontend/src/app/core/services/token.service.ts

This file was deleted.

12 changes: 1 addition & 11 deletions frontend/src/app/features/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@ import {Injectable} from '@angular/core';
import {Apollo} from "apollo-angular";
import {map, Observable} from "rxjs";
import {LOGIN, REGISTER} from "../requests/mutations";
import {TokenService} from "../../../core/services/token.service";
import {MutationResponse} from "../responses/mutation.response";
import {QueryResponses} from "../responses/query.responses";
import {IS_AUTHENTICATED} from "../requests/queries";
import {environment} from "../../../../environments/environment";
import {InMemoryCache} from "@apollo/client/core";

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor(private apollo: Apollo, private tokenService: TokenService) {
// apollo.create({
// uri: environment.auth_service,
// cache: new InMemoryCache()
// });
}
constructor(private apollo: Apollo) { }

public login(username: string, password: string): Observable<boolean> {
return this.apollo.mutate<MutationResponse>({
Expand All @@ -30,7 +22,6 @@ export class AuthService {
const token = response.data?.login;

if (token) {
this.tokenService.set(token);
return true;
} else {
console.error("Login failed: no token received.");
Expand All @@ -55,7 +46,6 @@ export class AuthService {
const token = response.data?.register;

if (token) {
this.tokenService.set(token);
return true;
} else {
console.error("Registration failed: no token received.");
Expand Down

0 comments on commit 4dfc638

Please sign in to comment.