summaryrefslogtreecommitdiffstats
path: root/ui/src/services
diff options
context:
space:
mode:
authorDessalines <happydooby@gmail.com>2019-03-22 18:42:57 -0700
committerDessalines <happydooby@gmail.com>2019-03-22 18:42:57 -0700
commitc438f0fef15febed0e365a9d412bf3a3394bed1c (patch)
tree070d5ba322226acd5fe0d0a7757cb936462ab2ff /ui/src/services
parentd52c16c123851e5ee8b5a6ee8c106615dab5081c (diff)
Adding login and Register
- Login and Register mostly working. - Starting to work on creating communities.
Diffstat (limited to 'ui/src/services')
-rw-r--r--ui/src/services/UserService.ts51
-rw-r--r--ui/src/services/WebSocketService.ts37
-rw-r--r--ui/src/services/index.ts2
3 files changed, 90 insertions, 0 deletions
diff --git a/ui/src/services/UserService.ts b/ui/src/services/UserService.ts
new file mode 100644
index 00000000..af0d1d15
--- /dev/null
+++ b/ui/src/services/UserService.ts
@@ -0,0 +1,51 @@
+import * as Cookies from 'js-cookie';
+import { User } from '../interfaces';
+import * as jwt_decode from 'jwt-decode';
+import { Subject } from 'rxjs';
+
+export class UserService {
+ private static _instance: UserService;
+ private user: User;
+ public sub: Subject<User> = new Subject<User>();
+
+ private constructor() {
+ let jwt = Cookies.get("jwt");
+ if (jwt) {
+ this.setUser(jwt);
+ } else {
+ console.log('No JWT cookie found.');
+ }
+
+ }
+
+ public login(jwt: string) {
+ Cookies.set("jwt", jwt);
+ console.log("jwt cookie set");
+ this.setUser(jwt);
+ }
+
+ public logout() {
+ this.user = null;
+ Cookies.remove("jwt");
+ console.log("Logged out.");
+ this.sub.next(null);
+ }
+
+ public get loggedIn(): boolean {
+ return this.user !== undefined;
+ }
+
+ public get auth(): string {
+ return Cookies.get("jwt");
+ }
+
+ private setUser(jwt: string) {
+ this.user = jwt_decode(jwt);
+ this.sub.next(this.user);
+ console.log(this.user.username);
+ }
+
+ public static get Instance(){
+ return this._instance || (this._instance = new this());
+ }
+}
diff --git a/ui/src/services/WebSocketService.ts b/ui/src/services/WebSocketService.ts
new file mode 100644
index 00000000..1882b125
--- /dev/null
+++ b/ui/src/services/WebSocketService.ts
@@ -0,0 +1,37 @@
+import { wsUri } from '../env';
+import { LoginForm, RegisterForm, UserOperation, CommunityForm } from '../interfaces';
+import { webSocket } from 'rxjs/webSocket';
+import { Subject } from 'rxjs';
+import { UserService } from './';
+
+export class WebSocketService {
+ private static _instance: WebSocketService;
+ public subject: Subject<{}>;
+
+ private constructor() {
+ this.subject = webSocket(wsUri);
+ console.log(`Connected to ${wsUri}`);
+ }
+
+ public static get Instance(){
+ return this._instance || (this._instance = new this());
+ }
+
+ public login(loginForm: LoginForm) {
+ this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
+ }
+
+ public register(registerForm: RegisterForm) {
+ this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
+ }
+
+ public createCommunity(communityForm: CommunityForm) {
+ this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm, UserService.Instance.auth));
+ }
+
+ private wsSendWrapper(op: UserOperation, data: any, auth?: string) {
+ let send = { op: UserOperation[op], data: data, auth: auth };
+ console.log(send);
+ return send;
+ }
+}
diff --git a/ui/src/services/index.ts b/ui/src/services/index.ts
new file mode 100644
index 00000000..f0f4ccf5
--- /dev/null
+++ b/ui/src/services/index.ts
@@ -0,0 +1,2 @@
+export { UserService } from './UserService';
+export { WebSocketService } from './WebSocketService';