From 6fab831ef563efbe026a9b7c96bf6a4af55e6784 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Sat, 6 Aug 2022 05:49:39 +0000 Subject: [PATCH] signin is modified --- src/app/core/nats/services/nats.service.ts | 9 ++- .../modules/auth/sign-in/components/index.ts | 3 + .../{ => components}/sign-in.component.html | 10 ++- .../{ => components}/sign-in.component.ts | 78 +++++++++++++------ .../sign-in/resolvers/sign-in.resolver.ts | 44 +++++++++++ .../modules/auth/sign-in/sign-in.module.ts | 5 +- .../modules/auth/sign-in/sign-in.routing.ts | 7 +- .../member/services/identity.service.ts | 34 ++++++++ 8 files changed, 158 insertions(+), 32 deletions(-) create mode 100644 src/app/modules/auth/sign-in/components/index.ts rename src/app/modules/auth/sign-in/{ => components}/sign-in.component.html (95%) rename src/app/modules/auth/sign-in/{ => components}/sign-in.component.ts (59%) create mode 100644 src/app/modules/auth/sign-in/resolvers/sign-in.resolver.ts diff --git a/src/app/core/nats/services/nats.service.ts b/src/app/core/nats/services/nats.service.ts index 149e971..263bdcf 100644 --- a/src/app/core/nats/services/nats.service.ts +++ b/src/app/core/nats/services/nats.service.ts @@ -1,4 +1,5 @@ import { Inject, Injectable, Type } from '@angular/core'; +import { DOCUMENT } from '@angular/common'; import * as jspb from 'google-protobuf'; import * as nats from 'nats.ws'; @@ -8,6 +9,7 @@ import { Client } from 'app/modules/protobuf/models/core/network_pb'; import { ModuleConfig } from '../config/module-config'; import { _MODULE_CONFIG } from '../config/token'; +import { Router } from '@angular/router'; type DeserializeConstructor = { new (): T; @@ -26,7 +28,10 @@ export class NatsService { /** * Constructor */ - constructor(@Inject(_MODULE_CONFIG) private __config: ModuleConfig) {} + constructor( + @Inject(_MODULE_CONFIG) private __config: ModuleConfig, + @Inject(DOCUMENT) private __document: Document + ) {} // ----------------------------------------------------------------------------------------------------- // @ Accessors @@ -67,7 +72,7 @@ export class NatsService { let c = new Client(); c.setClientIp(this.__conn?.info?.client_ip + ''); c.setSessionId(''); - c.setSiteUrl(''); + c.setSiteUrl(this.__document.location.hostname); let _opts: nats.RequestOptions = !!opts ? opts : { timeout: 3000 }; if (!_opts.headers) { diff --git a/src/app/modules/auth/sign-in/components/index.ts b/src/app/modules/auth/sign-in/components/index.ts new file mode 100644 index 0000000..113c1fd --- /dev/null +++ b/src/app/modules/auth/sign-in/components/index.ts @@ -0,0 +1,3 @@ +import { AuthSignInComponent } from './sign-in.component'; + +export const COMPONENTS = [AuthSignInComponent]; diff --git a/src/app/modules/auth/sign-in/sign-in.component.html b/src/app/modules/auth/sign-in/components/sign-in.component.html similarity index 95% rename from src/app/modules/auth/sign-in/sign-in.component.html rename to src/app/modules/auth/sign-in/components/sign-in.component.html index 1356f3b..c706bda 100644 --- a/src/app/modules/auth/sign-in/sign-in.component.html +++ b/src/app/modules/auth/sign-in/components/sign-in.component.html @@ -83,13 +83,17 @@
보안코드* - + 보안코드를 입력하세요. diff --git a/src/app/modules/auth/sign-in/sign-in.component.ts b/src/app/modules/auth/sign-in/components/sign-in.component.ts similarity index 59% rename from src/app/modules/auth/sign-in/sign-in.component.ts rename to src/app/modules/auth/sign-in/components/sign-in.component.ts index 2a7e92e..acb6604 100644 --- a/src/app/modules/auth/sign-in/sign-in.component.ts +++ b/src/app/modules/auth/sign-in/components/sign-in.component.ts @@ -1,11 +1,12 @@ import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; import { FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms'; -import { DomSanitizer } from '@angular/platform-browser'; +import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; 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 { IdentityService } from 'app/modules/polyglot/member/services/identity.service'; +import { CaptchaResponse } from 'app/modules/protobuf/c2se/common/identity_pb'; @Component({ selector: 'auth-sign-in', @@ -22,8 +23,9 @@ export class AuthSignInComponent implements OnInit { }; signInForm!: FormGroup; showAlert: boolean = false; - captchaToken?: string; - imagePath?: any; + + captcha?: CaptchaResponse.Result; + captchaImagePath?: SafeResourceUrl; /** * Constructor @@ -52,18 +54,14 @@ export class AuthSignInComponent implements OnInit { [Validators.required, Validators.email], ], password: ['admin', Validators.required], - captcha: ['', Validators.required], + captchaSecurityCode: ['', Validators.required], }); - this._identityService.captcha().then((result) => { - console.log( - 'success', - result, - result.getSecurityCodeHash(), - result.getImage() - ); - this.imagePath = this._sanitizer.bypassSecurityTrustResourceUrl( - 'data:image/jpg;base64,' + result.getImage() + this._activatedRoute.data.subscribe((data) => { + let captcha: CaptchaResponse.Result = data['captcha']; + this.captcha = captcha; + this.captchaImagePath = this._sanitizer.bypassSecurityTrustResourceUrl( + 'data:image/jpg;base64,' + captcha.getImage() ); }); } @@ -88,21 +86,22 @@ export class AuthSignInComponent implements OnInit { // Hide the alert this.showAlert = false; - // 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. + this._identityService + .signin( + this.captcha?.getSecurityCodeHash() as any, + this.signInForm?.controls['captchaSecurityCode'].value, + this.signInForm?.controls['email'].value, + this.signInForm?.controls['password'].value + ) + .then((res) => { const redirectURL = this._activatedRoute.snapshot.queryParamMap.get('redirectURL') || '/signed-in-redirect'; // Navigate to the redirect url this._router.navigateByUrl(redirectURL); - }, - (response) => { + }) + .catch((e) => { // Re-enable the form this.signInForm?.enable(); @@ -117,7 +116,38 @@ 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/auth/sign-in/resolvers/sign-in.resolver.ts b/src/app/modules/auth/sign-in/resolvers/sign-in.resolver.ts new file mode 100644 index 0000000..9ccdd24 --- /dev/null +++ b/src/app/modules/auth/sign-in/resolvers/sign-in.resolver.ts @@ -0,0 +1,44 @@ +import { Injectable } from '@angular/core'; +import { + ActivatedRouteSnapshot, + Resolve, + Router, + RouterStateSnapshot, +} from '@angular/router'; +import { Observable } from 'rxjs'; + +import { IdentityService } from 'app/modules/polyglot/member/services/identity.service'; +import { CaptchaResponse } from 'app/modules/protobuf/c2se/common/identity_pb'; + +@Injectable({ + providedIn: 'root', +}) +export class CaptchaResolver implements Resolve { + /** + * Constructor + */ + constructor( + private _identityService: IdentityService, + private _router: Router + ) {} + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + + /** + * Resolver + * + * @param route + * @param state + */ + resolve( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): + | Observable + | Promise + | CaptchaResponse.Result { + return this._identityService.captcha(); + } +} diff --git a/src/app/modules/auth/sign-in/sign-in.module.ts b/src/app/modules/auth/sign-in/sign-in.module.ts index c2630aa..eb16ce4 100644 --- a/src/app/modules/auth/sign-in/sign-in.module.ts +++ b/src/app/modules/auth/sign-in/sign-in.module.ts @@ -9,11 +9,12 @@ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { FuseCardModule } from '@fuse/components/card'; import { FuseAlertModule } from '@fuse/components/alert'; import { SharedModule } from 'app/shared/shared.module'; -import { AuthSignInComponent } from 'app/modules/auth/sign-in/sign-in.component'; import { authSignInRoutes } from 'app/modules/auth/sign-in/sign-in.routing'; +import { COMPONENTS } from './components'; + @NgModule({ - declarations: [AuthSignInComponent], + declarations: [...COMPONENTS], imports: [ RouterModule.forChild(authSignInRoutes), MatButtonModule, diff --git a/src/app/modules/auth/sign-in/sign-in.routing.ts b/src/app/modules/auth/sign-in/sign-in.routing.ts index 81d8c26..d7b21fe 100644 --- a/src/app/modules/auth/sign-in/sign-in.routing.ts +++ b/src/app/modules/auth/sign-in/sign-in.routing.ts @@ -1,9 +1,14 @@ import { Route } from '@angular/router'; -import { AuthSignInComponent } from 'app/modules/auth/sign-in/sign-in.component'; +import { AuthSignInComponent } from 'app/modules/auth/sign-in/components/sign-in.component'; + +import { CaptchaResolver } from './resolvers/sign-in.resolver'; export const authSignInRoutes: Route[] = [ { path: '', component: AuthSignInComponent, + resolve: { + captcha: CaptchaResolver, + }, }, ]; diff --git a/src/app/modules/polyglot/member/services/identity.service.ts b/src/app/modules/polyglot/member/services/identity.service.ts index dae1c60..9226abf 100644 --- a/src/app/modules/polyglot/member/services/identity.service.ts +++ b/src/app/modules/polyglot/member/services/identity.service.ts @@ -13,6 +13,9 @@ import { import { SUBJECT_CHECK_USERNAME_FOR_DUPLICATION, SUBJECT_CAPTCHA, + SUBJECT_SIGNIN, + SigninRequest, + SigninResponse, } from 'app/modules/protobuf/c2se/backend/identity_pb'; @Injectable({ @@ -48,6 +51,7 @@ export class IdentityService { }) .catch((e: Error) => { console.log('failed', e.getCode(), e.getMessage(), e.getData()); + reject(e); }); }); } @@ -67,6 +71,36 @@ export class IdentityService { }) .catch((e: Error) => { console.log('failed', e.getCode(), e.getMessage(), e.getData()); + reject(e); + }); + }); + } + + signin( + securityCodeHash: string, + securityCode: string, + username: string, + password: string + ): Promise { + return new Promise((resolve, reject) => { + let req = new SigninRequest(); + req.setSecurityCodeHash(securityCodeHash); + req.setSecurityCode(securityCode); + req.setUsername(username); + req.setPassword(password); + + this.__natsService + .request( + SUBJECT_SIGNIN, + req.serializeBinary(), + SigninResponse.deserializeBinary + ) + .then((result) => { + resolve(result); + }) + .catch((e: Error) => { + console.log('failed', e.getCode(), e.getMessage(), e.getData()); + reject(e); }); }); }