diff --git a/package-lock.json b/package-lock.json index bab4ecb..a0de540 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "crypto-js": "3.3.0", "google-protobuf": "^3.20.1", "highlight.js": "11.4.0", + "js-base64": "^3.7.2", "lodash-es": "4.17.21", "moment": "^2.29.4", "nats.ws": "^1.8.1", @@ -9311,6 +9312,11 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, + "node_modules/js-base64": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.2.tgz", + "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -21775,6 +21781,11 @@ } } }, + "js-base64": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.2.tgz", + "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 0bc1d5a..92c3f50 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "crypto-js": "3.3.0", "google-protobuf": "^3.20.1", "highlight.js": "11.4.0", + "js-base64": "^3.7.2", "lodash-es": "4.17.21", "moment": "^2.29.4", "nats.ws": "^1.8.1", diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 97e1020..4cdfe4c 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -2,7 +2,8 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { catchError, Observable, of, switchMap, throwError } from 'rxjs'; import { AuthUtils } from 'app/core/auth/auth.utils'; -import { UserService } from 'app/core/user/user.service'; +import { WebSessionStorageService } from 'app/core/web-storage/services/web-session-storage.service'; +import { IdentityService } from 'app/modules/polyglot/member/services/identity.service'; @Injectable() export class AuthService { @@ -13,7 +14,8 @@ export class AuthService { */ constructor( private _httpClient: HttpClient, - private _userService: UserService + private _identityService: IdentityService, + private __webSessionStorageService: WebSessionStorageService ) {} // ----------------------------------------------------------------------------------------------------- @@ -58,27 +60,44 @@ export class AuthService { * * @param credentials */ - signIn(credentials: { email: string; password: string }): Observable { - // Throw error, if the user is already logged in - if (this._authenticated) { - return throwError('User is already logged in.'); + signIn( + securityCodeHash: string, + credentials: { + email: string; + password: string; + captchaSecurityCode: string; } + ): Promise { + return new Promise((resolve, reject) => { + // Throw error, if the user is already logged in + if (this._authenticated) { + return reject('User is already logged in.'); + } - return this._httpClient.post('api/auth/sign-in', credentials).pipe( - switchMap((response: any) => { - // Store the access token in the local storage - this.accessToken = response.accessToken; + this._identityService + .signin( + securityCodeHash, + credentials.captchaSecurityCode, + credentials.email, + credentials.password + ) + .then((res) => { + this.__webSessionStorageService.set( + 'accessToken', + res.getAccessToken() + ); - // Set the authenticated flag to true - this._authenticated = true; + // Store the access token in the local storage + this.accessToken = res.getAccessToken(); - // Store the user on the user service - this._userService.user = response.user; - - // Return a new observable with the response - return of(response); - }) - ); + // Set the authenticated flag to true + this._authenticated = true; + resolve(); + }) + .catch((e) => { + reject(e); + }); + }); } /** @@ -102,9 +121,6 @@ export class AuthService { // Set the authenticated flag to true this._authenticated = true; - // Store the user on the user service - this._userService.user = response.user; - // Return true return of(true); }) @@ -171,6 +187,7 @@ export class AuthService { } // If the access token exists and it didn't expire, sign in using it - return this.signInUsingToken(); + // return this.signInUsingToken(); + return of(true); } } diff --git a/src/app/core/nats/services/nats.service.ts b/src/app/core/nats/services/nats.service.ts index 3f18205..09cceb3 100644 --- a/src/app/core/nats/services/nats.service.ts +++ b/src/app/core/nats/services/nats.service.ts @@ -3,6 +3,7 @@ import { DOCUMENT } from '@angular/common'; import * as jspb from 'google-protobuf'; import * as nats from 'nats.ws'; +import { Base64 } from 'js-base64'; import { HEADER_CLIENT } from 'app/modules/protobuf/c2se/core/network_pb'; import { Client } from 'app/modules/protobuf/models/core/network_pb'; @@ -59,9 +60,9 @@ export class NatsService { this.__conn = conn; this.__webSessionStorageService - .watch('SESSION_ID') - .subscribe((sessionId) => { - console.log('sessionId in NatsService', sessionId); + .watch('accessToken') + .subscribe((accessToken) => { + console.log('accessToken in NatsService', accessToken); }); resolve(); @@ -81,7 +82,7 @@ export class NatsService { return new Promise((resolve, reject) => { let c = new Client(); c.setClientIp(this.__conn?.info?.client_ip + ''); - c.setSessionId(this.__webSessionStorageService.get('SESSION_ID')); + c.setAccessToken(this.__webSessionStorageService.get('accessToken')); c.setSiteUrl(this.__document.location.hostname); let _opts: nats.RequestOptions = !!opts ? opts : { timeout: 3000 }; @@ -89,10 +90,9 @@ export class NatsService { _opts.headers = nats.headers(); } - var decoder = new TextDecoder('utf8'); _opts.headers.append( HEADER_CLIENT, - btoa(decoder.decode(c.serializeBinary())) + Base64.fromUint8Array(c.serializeBinary()) ); this.__conn?.request(subject, req, _opts).then((msg) => { diff --git a/src/app/modules/auth/sign-in/components/sign-in.component.ts b/src/app/modules/auth/sign-in/components/sign-in.component.ts index 507afca..bd1ea18 100644 --- a/src/app/modules/auth/sign-in/components/sign-in.component.ts +++ b/src/app/modules/auth/sign-in/components/sign-in.component.ts @@ -5,8 +5,6 @@ import { ActivatedRoute, Router } from '@angular/router'; import { fuseAnimations } from '@fuse/animations'; import { FuseAlertType } from '@fuse/components/alert'; import { AuthService } from 'app/core/auth/auth.service'; -import { WebSessionStorageService } from 'app/core/web-storage/services/web-session-storage.service'; -import { IdentityService } from 'app/modules/polyglot/member/services/identity.service'; import { CaptchaResponse } from 'app/modules/protobuf/c2se/common/identity_pb'; @Component({ @@ -36,8 +34,6 @@ export class AuthSignInComponent implements OnInit { private _authService: AuthService, private _formBuilder: FormBuilder, private _router: Router, - private _identityService: IdentityService, - private __webSessionStorageService: WebSessionStorageService, private _sanitizer: DomSanitizer ) {} @@ -85,16 +81,17 @@ export class AuthSignInComponent implements OnInit { // Hide the alert this.showAlert = false; - this._identityService - .signin( + // Sign in + this._authService + .signIn( this.captcha?.getSecurityCodeHash() as any, - this.signInForm?.controls['captchaSecurityCode'].value, - this.signInForm?.controls['email'].value, - this.signInForm?.controls['password'].value + this.signInForm?.value ) - .then((res) => { - this.__webSessionStorageService.set('SESSION_ID', res.getSessionId()); - + .then(() => { + // Set the redirect url. + // The '/signed-in-redirect' is a dummy url to catch the request and redirect the user + // to the correct page after a successful sign in. This way, that url can be set via + // routing file and we don't have to touch here. const redirectURL = this._activatedRoute.snapshot.queryParamMap.get('redirectURL') || '/signed-in-redirect'; @@ -118,37 +115,5 @@ export class AuthSignInComponent implements OnInit { // Show the alert this.showAlert = true; }); - - // Sign in - // this._authService.signIn(this.signInForm?.value).subscribe( - // () => { - // // Set the redirect url. - // // The '/signed-in-redirect' is a dummy url to catch the request and redirect the user - // // to the correct page after a successful sign in. This way, that url can be set via - // // routing file and we don't have to touch here. - // const redirectURL = - // this._activatedRoute.snapshot.queryParamMap.get('redirectURL') || - // '/signed-in-redirect'; - - // // Navigate to the redirect url - // this._router.navigateByUrl(redirectURL); - // }, - // (response) => { - // // Re-enable the form - // this.signInForm?.enable(); - - // // Reset the form - // this.signInNgForm?.resetForm(); - - // // Set the alert - // this.alert = { - // type: 'error', - // message: 'Wrong email or password', - // }; - - // // Show the alert - // this.showAlert = true; - // } - // ); } } diff --git a/src/app/modules/polyglot/member/services/identity.service.ts b/src/app/modules/polyglot/member/services/identity.service.ts index 9226abf..5c25b48 100644 --- a/src/app/modules/polyglot/member/services/identity.service.ts +++ b/src/app/modules/polyglot/member/services/identity.service.ts @@ -50,7 +50,7 @@ export class IdentityService { console.log('success', result, result.getDuplicated()); }) .catch((e: Error) => { - console.log('failed', e.getCode(), e.getMessage(), e.getData()); + console.log('failed', e); reject(e); }); }); @@ -70,7 +70,7 @@ export class IdentityService { resolve(result); }) .catch((e: Error) => { - console.log('failed', e.getCode(), e.getMessage(), e.getData()); + console.log('failed', e); reject(e); }); }); @@ -99,7 +99,7 @@ export class IdentityService { resolve(result); }) .catch((e: Error) => { - console.log('failed', e.getCode(), e.getMessage(), e.getData()); + console.log('failed', e); reject(e); }); }); diff --git a/src/app/modules/protobuf/c2se/backend/identity_pb.d.ts b/src/app/modules/protobuf/c2se/backend/identity_pb.d.ts index 233362b..ba64192 100644 --- a/src/app/modules/protobuf/c2se/backend/identity_pb.d.ts +++ b/src/app/modules/protobuf/c2se/backend/identity_pb.d.ts @@ -94,8 +94,8 @@ export namespace SigninResponse { }; export class Result extends jspb.Message { - getSessionId(): string; - setSessionId(value: string): void; + getAccessToken(): string; + setAccessToken(value: string): void; serializeBinary(): Uint8Array; toObject(includeInstance?: boolean): Result.AsObject; @@ -117,7 +117,7 @@ export namespace SigninResponse { export namespace Result { export type AsObject = { - sessionId: string; + accessToken: string; }; } } diff --git a/src/app/modules/protobuf/c2se/backend/identity_pb.js b/src/app/modules/protobuf/c2se/backend/identity_pb.js index 6a0f5fe..08fa815 100644 --- a/src/app/modules/protobuf/c2se/backend/identity_pb.js +++ b/src/app/modules/protobuf/c2se/backend/identity_pb.js @@ -537,7 +537,7 @@ if (jspb.Message.GENERATE_TO_OBJECT) { function (includeInstance, msg) { var f, obj = { - sessionId: jspb.Message.getFieldWithDefault(msg, 1, ""), + accessToken: jspb.Message.getFieldWithDefault(msg, 1, ""), }; if (includeInstance) { @@ -580,7 +580,7 @@ proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.deserializeBinaryF switch (field) { case 1: var value = /** @type {string} */ (reader.readString()); - msg.setSessionId(value); + msg.setAccessToken(value); break; default: reader.skipField(); @@ -614,17 +614,17 @@ proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.prototype.serializ proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.serializeBinaryToWriter = function (message, writer) { var f = undefined; - f = message.getSessionId(); + f = message.getAccessToken(); if (f.length > 0) { writer.writeString(1, f); } }; /** - * optional string session_id = 1; + * optional string access_token = 1; * @return {string} */ -proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.prototype.getSessionId = +proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.prototype.getAccessToken = function () { return /** @type {string} */ ( jspb.Message.getFieldWithDefault(this, 1, "") @@ -635,7 +635,7 @@ proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.prototype.getSessi * @param {string} value * @return {!proto.bet.beteran.c2se.backend.identity.SigninResponse.Result} returns this */ -proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.prototype.setSessionId = +proto.bet.beteran.c2se.backend.identity.SigninResponse.Result.prototype.setAccessToken = function (value) { return jspb.Message.setProto3StringField(this, 1, value); }; diff --git a/src/app/modules/protobuf/c2se/frontend/identity_pb.d.ts b/src/app/modules/protobuf/c2se/frontend/identity_pb.d.ts index 0737d52..68b97d9 100644 --- a/src/app/modules/protobuf/c2se/frontend/identity_pb.d.ts +++ b/src/app/modules/protobuf/c2se/frontend/identity_pb.d.ts @@ -94,8 +94,8 @@ export namespace SigninResponse { }; export class Result extends jspb.Message { - getSessionId(): string; - setSessionId(value: string): void; + getAccessToken(): string; + setAccessToken(value: string): void; serializeBinary(): Uint8Array; toObject(includeInstance?: boolean): Result.AsObject; @@ -117,7 +117,7 @@ export namespace SigninResponse { export namespace Result { export type AsObject = { - sessionId: string; + accessToken: string; }; } } diff --git a/src/app/modules/protobuf/c2se/frontend/identity_pb.js b/src/app/modules/protobuf/c2se/frontend/identity_pb.js index 43406c9..beaeec3 100644 --- a/src/app/modules/protobuf/c2se/frontend/identity_pb.js +++ b/src/app/modules/protobuf/c2se/frontend/identity_pb.js @@ -537,7 +537,7 @@ if (jspb.Message.GENERATE_TO_OBJECT) { function (includeInstance, msg) { var f, obj = { - sessionId: jspb.Message.getFieldWithDefault(msg, 1, ""), + accessToken: jspb.Message.getFieldWithDefault(msg, 1, ""), }; if (includeInstance) { @@ -580,7 +580,7 @@ proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.deserializeBinary switch (field) { case 1: var value = /** @type {string} */ (reader.readString()); - msg.setSessionId(value); + msg.setAccessToken(value); break; default: reader.skipField(); @@ -614,17 +614,17 @@ proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.prototype.seriali proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.serializeBinaryToWriter = function (message, writer) { var f = undefined; - f = message.getSessionId(); + f = message.getAccessToken(); if (f.length > 0) { writer.writeString(1, f); } }; /** - * optional string session_id = 1; + * optional string access_token = 1; * @return {string} */ -proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.prototype.getSessionId = +proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.prototype.getAccessToken = function () { return /** @type {string} */ ( jspb.Message.getFieldWithDefault(this, 1, "") @@ -635,7 +635,7 @@ proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.prototype.getSess * @param {string} value * @return {!proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result} returns this */ -proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.prototype.setSessionId = +proto.bet.beteran.c2se.frontend.identity.SigninResponse.Result.prototype.setAccessToken = function (value) { return jspb.Message.setProto3StringField(this, 1, value); }; diff --git a/src/app/modules/protobuf/models/core/network_pb.d.ts b/src/app/modules/protobuf/models/core/network_pb.d.ts index f2324e4..7036afe 100644 --- a/src/app/modules/protobuf/models/core/network_pb.d.ts +++ b/src/app/modules/protobuf/models/core/network_pb.d.ts @@ -12,10 +12,10 @@ export class Client extends jspb.Message { getSiteUrl(): string; setSiteUrl(value: string): void; - hasSessionId(): boolean; - clearSessionId(): void; - getSessionId(): string; - setSessionId(value: string): void; + hasAccessToken(): boolean; + clearAccessToken(): void; + getAccessToken(): string; + setAccessToken(value: string): void; serializeBinary(): Uint8Array; toObject(includeInstance?: boolean): Client.AsObject; @@ -39,6 +39,6 @@ export namespace Client { export type AsObject = { clientIp: string; siteUrl: string; - sessionId: string; + accessToken: string; }; } diff --git a/src/app/modules/protobuf/models/core/network_pb.js b/src/app/modules/protobuf/models/core/network_pb.js index 2769830..e63eef2 100644 --- a/src/app/modules/protobuf/models/core/network_pb.js +++ b/src/app/modules/protobuf/models/core/network_pb.js @@ -80,7 +80,7 @@ if (jspb.Message.GENERATE_TO_OBJECT) { obj = { clientIp: jspb.Message.getFieldWithDefault(msg, 1, ""), siteUrl: jspb.Message.getFieldWithDefault(msg, 2, ""), - sessionId: jspb.Message.getFieldWithDefault(msg, 3, ""), + accessToken: jspb.Message.getFieldWithDefault(msg, 3, ""), }; if (includeInstance) { @@ -131,7 +131,7 @@ proto.bet.beteran.core.network.Client.deserializeBinaryFromReader = function ( break; case 3: var value = /** @type {string} */ (reader.readString()); - msg.setSessionId(value); + msg.setAccessToken(value); break; default: reader.skipField(); @@ -226,10 +226,10 @@ proto.bet.beteran.core.network.Client.prototype.hasSiteUrl = function () { }; /** - * optional string session_id = 3; + * optional string access_token = 3; * @return {string} */ -proto.bet.beteran.core.network.Client.prototype.getSessionId = function () { +proto.bet.beteran.core.network.Client.prototype.getAccessToken = function () { return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 3, "")); }; @@ -237,7 +237,7 @@ proto.bet.beteran.core.network.Client.prototype.getSessionId = function () { * @param {string} value * @return {!proto.bet.beteran.core.network.Client} returns this */ -proto.bet.beteran.core.network.Client.prototype.setSessionId = function ( +proto.bet.beteran.core.network.Client.prototype.setAccessToken = function ( value ) { return jspb.Message.setField(this, 3, value); @@ -247,7 +247,7 @@ proto.bet.beteran.core.network.Client.prototype.setSessionId = function ( * Clears the field making it undefined. * @return {!proto.bet.beteran.core.network.Client} returns this */ -proto.bet.beteran.core.network.Client.prototype.clearSessionId = function () { +proto.bet.beteran.core.network.Client.prototype.clearAccessToken = function () { return jspb.Message.setField(this, 3, undefined); }; @@ -255,7 +255,7 @@ proto.bet.beteran.core.network.Client.prototype.clearSessionId = function () { * Returns whether this field is set. * @return {boolean} */ -proto.bet.beteran.core.network.Client.prototype.hasSessionId = function () { +proto.bet.beteran.core.network.Client.prototype.hasAccessToken = function () { return jspb.Message.getField(this, 3) != null; };