Skip to content

Commit

Permalink
Implement CookieService
Browse files Browse the repository at this point in the history
  • Loading branch information
sentemon committed Jan 7, 2025
1 parent 4dfc638 commit 906bff8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
9 changes: 5 additions & 4 deletions frontend/src/app/apollo.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { ApolloClientOptions, InMemoryCache, ApolloLink, HttpLink } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';
import { environment } from '../environments/environment';
import {inject} from "@angular/core";
import {CookieService} from "./core/services/cookie.service";

export function createApolloClientOptions(): ApolloClientOptions<any> {
const cookieService = inject(CookieService);

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];
const token = cookieService.get("token");
return {
headers: {
Authorization: token ? `Bearer ${token}` : '',
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/app/core/services/cookie.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@angular/core';

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

constructor() { }

get(key: string): string {
let value = document.cookie
.split('; ')
.find(row => row.startsWith(`${key}=`))
?.split('=')[1];

return value ?? `There is no cookie with key ${key}.`;
}
}
26 changes: 20 additions & 6 deletions frontend/src/app/features/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import {Injectable} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {Apollo} from "apollo-angular";
import {map, Observable} from "rxjs";
import {BehaviorSubject, map, Observable} from "rxjs";
import {LOGIN, REGISTER} from "../requests/mutations";
import {MutationResponse} from "../responses/mutation.response";
import {QueryResponses} from "../responses/query.responses";
import {IS_AUTHENTICATED} from "../requests/queries";
import {CookieService} from "../../../core/services/cookie.service";

@Injectable({
providedIn: 'root'
})
export class AuthService {
private isAuthenticatedSubject = new BehaviorSubject<boolean>(this.checkAuth());
public isAuthenticated$ = this.isAuthenticatedSubject.asObservable();

constructor(private apollo: Apollo) { }

private checkAuth(): boolean {
let cookieService = inject(CookieService);

const token = cookieService.get("token");

return token != "There is no cookie with key token.";
}

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

if (token) {
this.isAuthenticatedSubject.next(true);
return true;
} else {
this.isAuthenticatedSubject.next(false);
console.error("Login failed: no token received.");
return false;
}
Expand Down Expand Up @@ -57,9 +70,10 @@ export class AuthService {


public isAuthenticated(): Observable<boolean> {
return this.apollo.query<QueryResponses>({
query: IS_AUTHENTICATED
}).pipe(
map(response => response.data.isAuthenticated));
// return this.apollo.query<QueryResponses>({
// query: IS_AUTHENTICATED
// }).pipe(
// map(response => response.data.isAuthenticated));
return this.isAuthenticated$;
}
}

0 comments on commit 906bff8

Please sign in to comment.