Skip to content

Commit

Permalink
scaffold login redirect and auth service... need to setup httpInterce…
Browse files Browse the repository at this point in the history
…ptor, guards, routes, login page, etc
  • Loading branch information
snimmagadda1 committed Dec 17, 2024
1 parent 18d52f4 commit 709216c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 71 deletions.
6 changes: 3 additions & 3 deletions src/app/components/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DatePipe } from '@angular/common';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { RouterLink } from '@angular/router';
import { DbService } from '../../services';
import { AuthService } from '../../services/auth.service';

@Component({
selector: 'app-header',
Expand Down Expand Up @@ -150,10 +150,10 @@ export class HeaderComponent {
isTouched = true;
hovered = false;

constructor(private readonly dbService: DbService) {}
constructor(private readonly authService: AuthService) {}

sync() {
this.dbService.replicationState.reSync();
this.authService.initiateGithubAuth();
}

onMouseOver() {
Expand Down
27 changes: 27 additions & 0 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { environment } from '../../environments/environment';

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

constructor(private http: HttpClient) {}

initiateGithubAuth() {
// Instead of making an HTTP request, redirect the browser
window.location.href = environment.baseUrl + '/auth/github';
}

// logout(): Observable<any> {
// return this.http.post('/api/auth/logout', {}).pipe(
// tap(() => {
// this.isAuthenticatedSubject.next(false);
// }),
// );
// }
}
127 changes: 61 additions & 66 deletions src/app/services/db.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Injectable, Injector, isDevMode } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {
addRxPlugin,
createRxDatabase,
Expand All @@ -12,7 +11,7 @@ import {
toTypedRxJsonSchema,
} from 'rxdb';
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
import { replicateRxCollection, RxReplicationState } from 'rxdb/plugins/replication';
import { RxReplicationState } from 'rxdb/plugins/replication';

const EVENT_SCHEMA_LITERAL = {
version: 0,
Expand Down Expand Up @@ -139,55 +138,55 @@ export async function initDatabase(injector: Injector) {
throw new Error('initDatabase() injector missing');
}

const httpClient = injector.get(HttpClient);
// const httpClient = injector.get(HttpClient);

await _createDb().then(db => (DB_INSTANCE = db));

const replicationState = await replicateRxCollection({
collection: DB_INSTANCE.events,
replicationIdentifier: 'feined-http-replication',
live: true,
push: {
async handler(changeRows): Promise<{ _deleted: boolean }[]> {
const rawResponse = await httpClient
.post('https://feined-server.s11a.com/events-rpl/0/push', changeRows, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.toPromise();
return rawResponse as { _deleted: boolean }[];
},
},
pull: {
// TODO: types
/*
rxdb server format
"checkpoint": {
"id": "event-2",
"lwt": 1730498389296.01
}
*/
async handler(checkpointOrNull: any, batchSize) {
const updatedAt = checkpointOrNull ? checkpointOrNull.lwt : 0;
const id = checkpointOrNull ? checkpointOrNull.id : '';
const response = await httpClient
.get(
`https://feined-server.s11a.com/events-rpl/0/pull?lwt=${updatedAt}&id=${id}&limit=${batchSize}`,
)
.toPromise();
const data = response as any;
return {
documents: data.documents,
checkpoint: data.checkpoint,
};
},
},
// TODO: pullstream
});

REPLICATION_STATE = replicationState;
// const replicationState = await replicateRxCollection({
// collection: DB_INSTANCE.events,
// replicationIdentifier: 'feined-http-replication',
// live: true,
// push: {
// async handler(changeRows): Promise<{ _deleted: boolean }[]> {
// const rawResponse = await httpClient
// .post('https://feined-server.s11a.com/events-rpl/0/push', changeRows, {
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// },
// })
// .toPromise();
// return rawResponse as { _deleted: boolean }[];
// },
// },
// pull: {
// // TODO: types
// /*
// rxdb server format
// "checkpoint": {
// "id": "event-2",
// "lwt": 1730498389296.01
// }
// */
// async handler(checkpointOrNull: any, batchSize) {
// const updatedAt = checkpointOrNull ? checkpointOrNull.lwt : 0;
// const id = checkpointOrNull ? checkpointOrNull.id : '';
// const response = await httpClient
// .get(
// `https://feined-server.s11a.com/events-rpl/0/pull?lwt=${updatedAt}&id=${id}&limit=${batchSize}`,
// )
// .toPromise();
// const data = response as any;
// return {
// documents: data.documents,
// checkpoint: data.checkpoint,
// };
// },
// },
// // TODO: pullstream
// });

// REPLICATION_STATE = replicationState;

// TODO: block clients that havent synced in X time
// https://rxdb.info/replication.html#awaitinitialreplication-and-awaitinsync-should-not-be-used-to-block-the-application
Expand All @@ -199,24 +198,20 @@ export async function initDatabase(injector: Injector) {
})
export class DbService {
constructor() {
// emits each document that was received from the remote
this.replicationState.received$.subscribe(doc => console.log('**** Rpl receieved ****', doc));

// emits each document that was send to the remote
this.replicationState.sent$.subscribe(doc => console.log(`**** Rpl sent ****`, doc));

// emits all errors that happen when running the push- & pull-handlers.
this.replicationState.error$.subscribe(error => console.error(`**** Rpl error ****`, error));

// emits true when the replication was canceled, false when not.
this.replicationState.canceled$.subscribe(bool =>
console.error(`**** Rpl canceled ${bool} ****`),
);

// emits true when a replication cycle is running, false when not.
this.replicationState?.active$.subscribe(bool =>
console.log(`**** Rpl cycle running ${bool} ****`),
);
// // emits each document that was received from the remote
// this.replicationState.received$.subscribe(doc => console.log('**** Rpl receieved ****', doc));
// // emits each document that was send to the remote
// this.replicationState.sent$.subscribe(doc => console.log(`**** Rpl sent ****`, doc));
// // emits all errors that happen when running the push- & pull-handlers.
// this.replicationState.error$.subscribe(error => console.error(`**** Rpl error ****`, error));
// // emits true when the replication was canceled, false when not.
// this.replicationState.canceled$.subscribe(bool =>
// console.error(`**** Rpl canceled ${bool} ****`),
// );
// // emits true when a replication cycle is running, false when not.
// this.replicationState?.active$.subscribe(bool =>
// console.log(`**** Rpl cycle running ${bool} ****`),
// );
}

get db() {
Expand Down
4 changes: 4 additions & 0 deletions src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const environment = {
production: true,
baseUrl: 'https://feined-server.s11a.com',
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const enviroment = {
export const environment = {
baseUrl: 'http://localhost:8080',
}
};

0 comments on commit 709216c

Please sign in to comment.